Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 6th, 2005, 3:45 PM   #1
Knight
Newbie
 
Join Date: Jun 2005
Posts: 28
Rep Power: 0 Knight is on a distinguished road
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
Knight is offline   Reply With Quote
Old Jun 7th, 2005, 6:29 AM   #2
Gilward Kukel
Newbie
 
Join Date: Jun 2005
Location: Vienna, Austria
Posts: 15
Rep Power: 0 Gilward Kukel is on a distinguished road
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String s = in.readLine();
Gilward Kukel is offline   Reply With Quote
Old Jun 7th, 2005, 8:50 AM   #3
Pilgrim
Newbie
 
Join Date: Jun 2005
Posts: 11
Rep Power: 0 Pilgrim is on a distinguished road
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.
Pilgrim is offline   Reply With Quote
Old Jun 7th, 2005, 9:13 AM   #4
Knight
Newbie
 
Join Date: Jun 2005
Posts: 28
Rep Power: 0 Knight is on a distinguished road
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
Knight is offline   Reply With Quote
Old Jun 7th, 2005, 2:29 PM   #5
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
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
Mjordan2nd is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:41 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC