|
-
Feb 27th, 2006, 07:08 AM
#1
Thread Starter
Lively Member
[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?
-
Feb 27th, 2006, 04:01 PM
#2
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
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Feb 27th, 2006, 04:50 PM
#3
-
Feb 28th, 2006, 07:13 AM
#4
Thread Starter
Lively Member
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?
-
Feb 28th, 2006, 02:00 PM
#5
Re: Getting input from a user
Code:
Scanner s=new Scanner(System.in);
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Feb 28th, 2006, 05:07 PM
#6
Frenzied Member
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();
-
Feb 28th, 2006, 10:59 PM
#7
Thread Starter
Lively Member
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?
-
Mar 1st, 2006, 09:33 PM
#8
Lively Member
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.
-
Mar 1st, 2006, 10:29 PM
#9
Addicted Member
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.*
-
Mar 1st, 2006, 11:03 PM
#10
Thread Starter
Lively Member
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?
-
Mar 2nd, 2006, 02:33 AM
#11
Addicted Member
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
}
}
-
Mar 2nd, 2006, 06:14 AM
#12
Frenzied Member
Re: Getting input from a user
I recommend in a try-catch clause.
-
Mar 2nd, 2006, 06:46 AM
#13
Thread Starter
Lively Member
Re: Getting input from a user
-
Mar 2nd, 2006, 09:19 AM
#14
Fanatic Member
Re: Getting input from a user
Can't you just use .readInt()?
-
Mar 2nd, 2006, 08:14 PM
#15
Addicted Member
Re: Getting input from a user
No you cannot use .readInt(). There is no readInt() method of BufferedReader.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|