English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

What class do we read numbers from System.in, and what package is in that class?

Is it java.lang? Or java.util? Whats the difference? Im so so confused ><

Thanks, i need this asap please, im new to programming, trying to figure it out, a small explanation will be great if possible...

Thanks.

2007-08-16 22:52:44 · 3 answers · asked by Anonymous in Computers & Internet Programming & Design

3 answers

There is not a lot to the java language. A few simple operators and the basic data types. Char, int, float, double long etc.
The strength of this and any OO language is that you can extend the basic language with additional classes. A lot of this has already been done for you by the Sun programmers.

So anything that is related to the basic language and its types can be found in the lang package. The the first link for the starting page of documentation.

Now you want to receive data from System.in().
This data is by default a string. So you need to use a parse process. There is one in the lang package for each of the basic numeric data types. I have included a link to parseInt to demonstrate.

2007-08-16 23:27:34 · answer #1 · answered by AnalProgrammer 7 · 1 0

if you want to read input of real numbers from the keyboard do this:
import java.util.*;
put the above before the declaration of the class, then:

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try{
double number=Double.parseDouble( br.readLine() );
}catch(Exception e){}

java.lang is autonmatically imported to all classes whereas
other packages must be imported by the programmer. java.lang contain all basic classes needed for a java program(String, Integer, etc). System.in is in java.lang

2007-08-17 02:15:47 · answer #2 · answered by rodette p 3 · 1 0

wat u want is the Scanner class..
i think its in java.util.

this will allow u to read fromthe console.

Scanner keyboard = new Scanner(System.in);

//nw u can ask the user a question.. when the user types the answer and hits enter, the answer is read into the keyboard object.

if u expecting numbers, then use

int n = keyboard.nextInt();

//n will nw contain the value the user types in.. just becareful nt to use the wrong method for wrong types.. the scanner class is wat u need i think..

2007-08-17 04:46:17 · answer #3 · answered by har4winn 1 · 1 0

fedest.com, questions and answers