![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jun 2005
Posts: 28
Rep Power: 0
![]() |
Input syntax?
Quick question. What is the "default" syntax for input from a screen (take information from the screen and place it into a variable, etc..). For example, I have the SavitchIn class that contains input methods such as:
SavitchIn.readLine(); // Gets data from a line on the screen SavitchIn.readLineInt(); // Gets an interger from a line on the screen Thank you in advance ![]() |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Jun 2005
Location: Vienna, Austria
Posts: 15
Rep Power: 0
![]() |
BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String s = in.readLine(); |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Jun 2005
Posts: 11
Rep Power: 0
![]() |
For input like above, you have to import java.io.BufferedReader and java.io.InputStreamReader. The input will come in as a String. If you want it to be an integer, you have to change it with this:
int i = Integer.parseInt(s); To use this method, you have to import java.lang.Integer. |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Jun 2005
Posts: 28
Rep Power: 0
![]() |
So, to cover all the bases and to allow a program to be run on all PCs, would be to stick the following at the top (well, in the right area..
):import java.io.* BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); After that, I would just have to cast the String to whatever I would need. Doesn't java automatically import the java.lang.* ? Thanks ![]() |
|
|
|
|
|
#5 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
BufferedReader is probably what you'll see most often, but I personally feel the Scanner class is much easier to use. I would certainly take a look into that.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|