Læse fra keyboard
Hey !Sidder med noget kode fra min bog med streams. Jeg har 3 filer og det er i TestFormattedInput at jeg ikke fatter hvad der sker.
FormattedInput__________________________________________-
import java.io.*;
public class FormattedInput {
//Int method
public int readInt() throws InvalidUserInputException {
if(readToken() != tokenizer.TT_NUMBER){
throw new InvalidUserInputException("readInt() failed. Input data not nummeric");
}
return (int) tokenizer.nval;
}
//Double method
public double readDouble() throws InvalidUserInputException {
if (readToken() != tokenizer.TT_NUMBER){
throw new InvalidUserInputException(" readDouble() failed. Input not numeric");
}
return tokenizer.nval;
}
//String method
public String readString() throws InvalidUserInputException {
if (readToken() == tokenizer.TT_WORD || ttype =='\"' || ttype == '\''){
return tokenizer.sval;
} else {
throw new InvalidUserInputException(" readString() failed. Input not string");
}
}
//Read Method
private int readToken() {
try {
ttype = tokenizer.nextToken();
return ttype;
} catch (IOException e){
e.printStackTrace(System.err);
System.exit(1);
}
return 0;
}
private StreamTokenizer tokenizer = new StreamTokenizer(
new BufferedReader(
new InputStreamReader(System.in)));
private int ttype;
}
InvalidUserInputException___________________________________
public class InvalidUserInputException extends Exception {
public InvalidUserInputException() {}
public InvalidUserInputException(String message){
super(message);
}
}
TestFormattedInput____________________________________-
public class TestFormattedInput {
public static void main(String[] args) {
FormattedInput kb = new FormattedInput();
for(int i = 0; i < 5; i++){
try{
System.out.print("Enter an integer: ");
System.out.println("Integer read: "+ kb.readInt());
} catch (InvalidUserInputException e) {
System.out.println("InvalidUserInputException thrown. \n "+ e.getMessage());
}
}
}
}
Hvordan kan det være at i den sidste fil TestFormattedInput kan jeg skrive ved
System.out.print("Enter an integer: ");
Det giver for mig ingen mening.
