Results 1 to 12 of 12
  1. #1
    Wicked is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default 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.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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
    in an if clause, which is incorrect.

    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
    }
    ...
    Might be smarted to prompt for the operator, and the two numbers initially, then use if else clauses like
    Java Code:
    prompt
    
    if(operator.equals("add")){
      answer = x + y;
    }
    Last edited by sunde887; 02-06-2011 at 01:06 AM.

  3. #3
    Wicked is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    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.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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;
                  }
                    }
                      }
    is what you have, where you want it to be
    Java Code:
    if(something){
    //do something
    }
    else if(something else){
    //do something else
    }
    All the boolean variables are kind of a waste as well, the only variables you need are
    Java Code:
    String operator;
    double number1;
    double number2;
    double answer;
    I used doubles for more precision when working with division.

    if you do
    Java Code:
    int x = 5;
    int y = 2;
    int answer = x / y;
    you would expect 2.5, with integer arithmetic, the .5 is truncated and it will give you 2.

    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;
    }
    Ill leave the rest up to you. I gave you a LOT to work on, and way more than necessary to complete this.

    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.

  5. #5
    Wicked is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    Thank you very much.

    I took what you said into account and re-wrote the entire thing at about 1/4 of the length.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Glad to have helped, did you get it working correct?

  7. #7
    Wicked is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    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.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    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.

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  10. #10
    Wicked is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    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?
    I removed one scanner and now it works perfectly.

    Not sure why... But thanks :D
    Last edited by Wicked; 02-10-2011 at 11:56 PM.

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    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.

  12. #12
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Im glad you solved it, I hope I helped you understand some of the concepts.

Similar Threads

  1. Replies: 4
    Last Post: 05-07-2010, 07:58 PM
  2. Replies: 12
    Last Post: 12-05-2008, 05:37 AM
  3. Exception in thread "main" java.util.NoSuchElementException
    By vileoxidation in forum New To Java
    Replies: 5
    Last Post: 09-17-2008, 07:29 AM
  4. Replies: 4
    Last Post: 06-08-2008, 02:19 PM
  5. Replies: 5
    Last Post: 05-14-2008, 01:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •