Need help with Java. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Need help with Java.

I made a program to print patterns composed of stars. I'm getting the following error: Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at Program.TriangularGrid(Program.java:42) at Program.main(Program.java:22) Removing close() isn't helping https://code.sololearn.com/cyx5782X6K5F/?ref=app

11th May 2017, 4:54 PM
Casey McCray
Casey McCray - avatar
5 Answers
+ 2
I got it, you have to define the scan variable like a global variable. public static Scanner scan = new Scanner(System.in); public static void main(String[] args) { .... } For example: http://stackoverflow.com/questions/12832006/scanner-error-with-nextint
11th May 2017, 6:32 PM
Jorge Daniel Lopez Irusta
Jorge Daniel Lopez Irusta - avatar
+ 3
Hi Casey, You need to send your inputs into separate lines to compile your code. Sololearn just compile your code according the inputs that you send it is not like IDE. Take a look this code when just send one input. https://code.sololearn.com/cVYEl0h520tj/#java JIC: http://stackoverflow.com/questions/12832006/scanner-error-with-nextint
11th May 2017, 5:44 PM
Jorge Daniel Lopez Irusta
Jorge Daniel Lopez Irusta - avatar
+ 1
Yes, I was thinking that too cause the code worked fine in Eclipse. I want it to run in SoloLearn too. I send my input in multiple lines, It doesn't work. for example: 1 5 This gives the value of 'choice' as 1 which then calls the TriangularGrid() and then passes the value "5" in the variable 'value'. What am I doing wrong?
11th May 2017, 5:59 PM
Casey McCray
Casey McCray - avatar
+ 1
I also tried the if(hasNextInt()) statement but that just messes up all the variable declaration for some reason.
11th May 2017, 6:02 PM
Casey McCray
Casey McCray - avatar
+ 1
Thank you so much :D It worked.
12th May 2017, 1:10 AM
Casey McCray
Casey McCray - avatar