• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Helo with error: Exception in thread "main" java.util.NoSuchElementException

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,

I have just started taking JAVA and I have been struggling with my first assignment. I think I have been looking at it so much that I just don't see it anymore. I think I am close to running it but getting this error:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at TestMax.inputNum(TestMax.java:14)
at TestMax.displayNum(TestMax.java:24)
at TestMax.main(TestMax.java:33)

That's a lot of errors! lol
Here is my code. any insight would be greatly appreciated

import java.util.Scanner; //import package to alow use of scanner for input

public class TestMax {
       
       int minNum = 1; //declare variable
       int maxNum = 5; //declare variable

       public int inputNum() {
           
           Scanner input = new Scanner(System.in);
           System.out.print("Enter a number between 1 and 5: ");
           int userNum = input.nextInt();
           input.close();
   return userNum;    
           
       } //end of inputNum

       public void displayNum(int userNum) {
           
           System.out.printf("%d\n", Math.min(5,Math.max(1,inputNum())));

       } //end of displayNum
   

   public static void main(String[] args) {  
   
       TestMax newTest = new TestMax();  //declare a variable of type TestMax
       int userNum = newTest.inputNum();  //call inputNum method
       newTest.displayNum(userNum);
       
           
   } // end method main
   
} //end of class TestMax

I appreciate any input you may have. I am struggling quite a bit and could you some advice!

Cheers
Josee
 
Sheriff
Posts: 7123
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!  Thanks for posting the full error message (called a stack trace).  One thing to do next time is UseCodeTags (that's a link) when you post code.  Make sure the code you post exactly matches the error message.
The problem is in line 20 where you have a parameter of userNum which you don't use (and you should).  Instead you are calling inputNum() again.  You strange error is because you closed a Scanner opened to System.in, then tried to open it again.  This doesn't work, as you have seen.
 
Josee Landry
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much for you help! I was about to give up I went back and made the modification and my program is now working and doing exactly what it is supposed to.
I am extremely grateful for your response.
cheers
Josee
 
Marshal
Posts: 76884
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch (again)

Be careful when closing Scanners. You have to close a Scanner reading everything except System.in. Not only do you close the Scanner, but you (probably) also close System.in. And you can never reopen System.in.
Solution: you need one Scanner reading System.in. Not one per class, but one per application. Never close it. Let it go out of scope when the application finishes and all will be well.
 
Josee Landry
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
thank you for the clarification on the close Scanner. I have so much to learn and I am happy to have found this site! im sure this won't be the last post you see from me
Cheers!
 
permaculture is giving a gift to your future self. After reading this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic