Java code not running | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

30th Mar 2020, 2:49 PM
andreas
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.
30th Mar 2020, 2:54 PM
Hatsy Rei
Hatsy Rei - avatar
+ 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?
30th Mar 2020, 4:29 PM
andreas