+ 1
Java code not running
This is a listing https://code.sololearn.com/chTKzsvGXyof/#java I've copied from a book of 2014. Can anybody please tell me why it isn't working?
2 Answers
+ 3
IOException needs to be imported.
https://docs.oracle.com/javase/8/docs/api/java/io/IOException.html
Also, the code won't really work on SL since it doesn't recognize the scanner call and will not prompt for input.
+ 2
Thank you Hatsy, thank you - swim -,
I actually tried the code with Eclipse first. As I'm still a beginner with that IDE, I copied the code to here.
Even if I add 'import.io.IOException' in Eclipse, I'm getting another mistake:
import java.util.Scanner;
import java.io.Console;
import java.io.IOException;
public class Return {
public static void main(String[] args) throws IOException {
Console cons = System.console();
cons.printf("\n");
cons.printf("\n Sie haben bei unserem Musikquiz gewonnen! \n");
cons.printf("\n Geben Sie Ihr Alter an: ");
Scanner sc = new Scanner(cons.reader());
int alter = sc.nextInt();
cons.printf("\n Ihr Gewinn ist ... %s \n", Demo.belohnung(alter));
cons.printf("\n");
}
}
Exception in thread "main" java.lang.NullPointerException
at Return.main(Return.java:9)
which means that even Eclipse doesn't regognize the Console class. How can that be? And what can I do about it?
By the way, I guess the way I put my listing in here isn't really how it ought to be. Anything I should change about this?