Basic Java question. Need a simple example of keyboard input.

Buddha Bart

Diamond Member
Oct 11, 1999
3,064
0
0
Can anyone point me to one? Everything i've tried searching for on google gives me file input, not keyboard input. And i'm too darn amature to figure out how to just convert it.

Here's what I have so far:

import java.util.*;
import java.io.*;

public class ReadIn {
static public void main(String[] args) {

BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

StringTokenizer inputLine;
inputLine = new StringTokenizer(stdin.readLine());

System.out.println(inputLine);
}
}


But its giving me this error when I try and compile:
ReadIn.java:10: unreported exception java.io.IOException; must be caught or declared to be thrown
inputLine = new StringTokenizer(stdin.readLine());
^


(i'm sure the forum will mess the alignment, the "^" should point to the period between stdin and readLine())

bart
 

athithi

Golden Member
Mar 5, 2002
1,717
0
0
import java.util.*;
import java.io.*;

public class ReadIn {
static public void main(String[] args) {

try{
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

StringTokenizer inputLine;
inputLine = new StringTokenizer(stdin.readLine());

System.out.println(inputLine);
}
catch (IOException e){
printStackTrace();
}


}
}

should get rid of your compilation error....
 

Buddha Bart

Diamond Member
Oct 11, 1999
3,064
0
0
hey thanks.

printStackTrace(); gave an error, but i just replaced it with a simple println.

Then of course I find the other error in the program, namely System.out.println(inputLine); doesnt work right. Prints this out:

java.util.StringTokenizer@17182c1

oh well, definatly not gonna have this done in time for class, i'll just bug the teacher.

bart
 

athithi

Golden Member
Mar 5, 2002
1,717
0
0

while (inputLine.hasMoreTokens()) {
System.out.println(inputLine.nextToken());
}

...but if you just want to print the one line, you could just print stdin.readLine()

Sorry, that line in the catch block should've been e.printStackTrace();

...just gives you more details about where the actual error is being generated....you could also use e.getMessage(); to get detailed information of the error that was generated....HTH
 

Buddha Bart

Diamond Member
Oct 11, 1999
3,064
0
0
thanks again :)

actually yeah, originally it was part of a loop, but i de-loopified it in an attempt to figure out where it was going wrong.

bart