Results 1 to 12 of 12
- 02-06-2011, 12:17 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 5
- Rep Power
- 0
Need help with 'Exception in thread "main" java.util.NoSuchElementException'
---SOLVED---
Java Code:import java.util.Scanner; class Calculator { public static void main(String[] args) { double num1 = 0; double num2 = 0; double ans = 0; String op = new String(); Scanner NumIn = new Scanner(System.in); Scanner OpIn = new Scanner(System.in); System.out.println("Would you like to 'add', 'subtract', 'multiply' or 'divide'?"); op = OpIn.nextLine(); OpIn.close(); System.out.println("What is the first number?"); num1=NumIn.nextDouble(); System.out.println("Enter the second number"); num2=NumIn.nextDouble(); if(op.equals("add")){ ans = num1 + num2; } if(op.equals("subtract")){ ans = num1 - num2; } if(op.equals("multiply")){ ans = num1 + num2; } if(op.equals("divide")){ ans = num1 + num2; } System.out.println("= " + ans); } }
I get that error instantly after entering a correct string (add).
.Last edited by Wicked; 02-10-2011 at 11:58 PM.
- 02-06-2011, 01:03 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
Your code seems overly complex, you might want to look into simplifying it. One error I noticed right away is you tried to use
Java Code:correctoperator = false
Also, you are incorrectly comparing strings, try using .equals() instead of == when comparing strings.
Also, why are you nesting the tests for the operator?
Java Code:if(operator.equals("add")){ //set add to true //set all others to false else if(operator.equals("mult")){ //set mult to true //set all others to false } ...
Java Code:prompt if(operator.equals("add")){ answer = x + y; }
Last edited by sunde887; 02-06-2011 at 01:06 AM.
- 02-06-2011, 01:16 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 5
- Rep Power
- 0
Thanks for the help.
As for it being overcomplicated - It's the second thing I've ever written in Java so I have a bit of improving to do :)
I'll try and understand what you're saying.
- 02-06-2011, 01:34 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
I can break it down better, of course the tutorials may explain it better.
When comparing strings you can't use ==, instead you want to use the method .equals() which compares two objects(in this case, strings)
String (Java 2 Platform SE 5.0)
It's a waste to first determine the value of the operator, then get the input, then apply the correct operation. You don't really need to be using .close on the scanner on something this small.
You have a weird condition "tree" I guess is the term. It states
if operator is add, test if operator is sub, if true, test div, and so on. It also sets the state of the items as it goes. With this conditional it only works if the user inputs add. If he says mult it fails the initial if clause.
Java Code:if (operation == ("add")); { addition = true; correctoperator = true; if (operation == ("subtract")) { subtraction = true; correctoperator = true; if (operation == ("divide")) { division = true; correctoperator = true; if (operation == ("multiply")) { multiplication = true; correctoperator = true; } } }
Java Code:if(something){ //do something } else if(something else){ //do something else }
Java Code:String operator; double number1; double number2; double answer;
if you do
Java Code:int x = 5; int y = 2; int answer = x / y;
You don't need to initialize all the variables either, you can set them later. You just have to make sure you set them all.
instead of determining the input, then setting boolean variables, remove the boolean variables and do it all in one step. Ill give you one for free
Java Code:System.out.println("Enter an operator"); Scanner operator = new Scanner(); String op = operator.nextLine(); System.out.println("Enter a number"); number1 = operator.nextInt(); System.out.println("Enter a number"); number2 = operator.nextInt(); if(op.equals("add")){ answer = number1 + number2; }
to others: Im sorry if this is a bit of spoon feeding. I do however feel I simply corrected his major flaws and pushed him into a simpler solution.Last edited by sunde887; 02-06-2011 at 01:36 AM.
- 02-06-2011, 01:42 PM #5
Member
- Join Date
- Feb 2011
- Posts
- 5
- Rep Power
- 0
Thank you very much.
I took what you said into account and re-wrote the entire thing at about 1/4 of the length.
- 02-07-2011, 04:54 AM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
Glad to have helped, did you get it working correct?
- 02-09-2011, 11:07 PM #7
Member
- Join Date
- Feb 2011
- Posts
- 5
- Rep Power
- 0
Not exactly.
I still get that error when I try to run it but my code seems much better so I guess that's what matters.
#1547018 - Pastie
^ That's the new thing there.
- 02-09-2011, 11:10 PM #8
I don't follow links. Copy and paste your code (only relevant part or a SSCCE) and the EXACT error message.
If you can't be bothered then I can't be bothered to help.
- 02-10-2011, 12:22 AM #9
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
You only need one scanner and really don't even need to close it. You also can leave all variables except ans uninitialized. If you paste your code you will get a lot more help. Can you post the exact error you are getting?
Last edited by sunde887; 02-10-2011 at 12:28 AM.
- 02-10-2011, 11:41 PM #10
Member
- Join Date
- Feb 2011
- Posts
- 5
- Rep Power
- 0
- 02-11-2011, 12:01 AM #11
Reason - you attached 2 Scanner objects to the same input stream. Then one of your Scanner objects closed the stream. Therefore there was no longer any input for the other Scanner to read.
- 02-11-2011, 12:11 AM #12
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
Similar Threads
-
Exception in thread "main" java.util.NoSuchElementException
By xx__rose in forum New To JavaReplies: 4Last Post: 05-07-2010, 07:58 PM -
xception in thread "main" java.util.NoSuchElementException: No line found
By Tenn in forum New To JavaReplies: 12Last Post: 12-05-2008, 05:37 AM -
Exception in thread "main" java.util.NoSuchElementException
By vileoxidation in forum New To JavaReplies: 5Last Post: 09-17-2008, 07:29 AM -
Exception in thread "main" java.util.NoSuchElementException
By ragav in forum New To JavaReplies: 4Last Post: 06-08-2008, 02:19 PM -
[SOLVED] Exception in thread "main" java.util.NoSuchElementException
By thevoice in forum New To JavaReplies: 5Last Post: 05-14-2008, 01:43 PM
Bookmarks