This week's book giveaways are in the Jython/Python and Object-Oriented programming forums.
We're giving away four copies each of Machine Learning for Business: Using Amazon SageMaker and Jupyter and Object Design Style Guide and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

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

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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
 
Marshal
Posts: 6643
177
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 3
  • Mark post as helpful
  • send pies
  • 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: 67464
257
  • Mark post as helpful
  • send pies
  • 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: 3
  • Mark post as helpful
  • send pies
  • 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!
 
What do you have in that there bucket? It wouldn't be a tiny ad by any chance ...
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!