PDA

Click to See Complete Forum and Search --> : [RESOLVED] Getting input from a user


sid_19840
Feb 27th, 2006, 06:08 AM
Im just learning the basics of Java.Im stuck at something.

How do I get an input from a user.Eg-If I want to get a number(integer),I tried using BufferedReader,but isnt there an easier way,like for output I use System.out.println,is there something similar to input?

ComputerJy
Feb 27th, 2006, 03:01 PM
You can instantiate a "java.util.Scanner" object it can read all kinds of input
p.s. Scanner exists for java 5 only

manavo11
Feb 27th, 2006, 03:50 PM
Search for System.in

sid_19840
Feb 28th, 2006, 06:13 AM
Im a bit confused with the answer's,My question is: Can System.in command be used in any way to get input from the user?

ComputerJy
Feb 28th, 2006, 01:00 PM
Scanner s=new Scanner(System.in);

System_Error
Feb 28th, 2006, 04:07 PM
In several ways.. The Scanner is 1.5

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String crap = br.readLine();

sid_19840
Feb 28th, 2006, 09:59 PM
ok,after declaring the buffered reader,how do I get it to read an integer or a float variable?

fartman_900
Mar 1st, 2006, 08:33 PM
This is what you do:

String temp;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
......
System.out.print("enter message here"); //prompt for user
temp=br.readLine();
.......

Then if you need it as an integer value, use Integer.parseInt(temp). I think there is the same thing for the float class to, I could be wrong.

ychhuong
Mar 1st, 2006, 09:29 PM
You can try the following:


BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter Integer value : ");
int x=Integer.parseInt(br.readLine()); //get user input and convert to Integer
System.out.print("Enter Float value : ");
float f = Float.parseFloat(br.readLine()); //convert to float
System.out.print("Enter Double value : ");
double d = Double.parseDouble(br.readLine());//convert to double


But don't forget to import package java.io.*

sid_19840
Mar 1st, 2006, 10:03 PM
Thanks,one final question,does Buffered Reader require an Exception or anything to be written(like extends or implements) in the function where I use it?

ychhuong
Mar 2nd, 2006, 01:33 AM
BufferedReader is a class in java.io.*. The method readLine() of this class throws an execption. Here is a complete example.


import java.io.*;
public class UserInput{
public static void main(String args[]) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter Integer value : ");
int x=Integer.parseInt(br.readLine()); //get user input and convert to Integer
System.out.print("Enter Float value : ");
float f = Float.parseFloat(br.readLine()); //convert to float
System.out.print("Enter Double value : ");
double d = Double.parseDouble(br.readLine());//convert to double
}
}

System_Error
Mar 2nd, 2006, 05:14 AM
I recommend in a try-catch clause.

sid_19840
Mar 2nd, 2006, 05:46 AM
Thanks to all.

shirazamod
Mar 2nd, 2006, 08:19 AM
Can't you just use .readInt()?

ychhuong
Mar 2nd, 2006, 07:14 PM
No you cannot use .readInt(). There is no readInt() method of BufferedReader.