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, ',');
For more help, www.NeedProgrammingHelp.com
Bookmarks