i was reading up on this syntax and it said that this automatically stores everything as int and ot convert it to char you would need to do something like - ch = (char) System.in.read();. well i was wondering if there is a way to convert to and store a string. i am basically looking to store a string value (name) through user input.
09-23-2005, 02:56 AM
NPH
A way to read a string from the user is shown below:
Code:
//get buffered reader
BufferedReader in = new BufferedReader( new InputStreamReader(System.in));
//get line
String line = in.readLine();
//get another line
String line2 = in.readLine();
.. etc. Remember to import java.io
If a line consist of many words you can tokenize the line:
Code:
StringTokenizer tok = new StringTokenizer(line);
tok.nextToken(); //gets the first word
tok.nextToken(); //gets the second word
tok.nextToken(); //gets the third word
The above code breaks up the line by using the 'space' as a delimiter. To use some other character (like a comma) as a delimited replace the first line above with:
Code:
StringTokenizer tok = new StringTokenizer(line, ',');