[RESOLVED] Getting input from a user
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?
Re: Getting input from a user
You can instantiate a "java.util.Scanner" object it can read all kinds of input
p.s. Scanner exists for java 5 only
Re: Getting input from a user
Re: Getting input from a user
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?
Re: Getting input from a user
Code:
Scanner s=new Scanner(System.in);
Re: Getting input from a user
In several ways.. The Scanner is 1.5
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String crap = br.readLine();
Re: Getting input from a user
ok,after declaring the buffered reader,how do I get it to read an integer or a float variable?
Re: Getting input from a user
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.
Re: Getting input from a user
You can try the following:
VB Code:
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.*
Re: Getting input from a user
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?
Re: Getting input from a user
BufferedReader is a class in java.io.*. The method readLine() of this class throws an execption. Here is a complete example.
VB Code:
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
}
}
Re: Getting input from a user
I recommend in a try-catch clause.
Re: Getting input from a user
Re: Getting input from a user
Can't you just use .readInt()?
Re: Getting input from a user
No you cannot use .readInt(). There is no readInt() method of BufferedReader.