0

I just learned some java, and tried to make a simple calculator. When I got a scanner to input the total numbers the user needs, it worked fine. It was when I used another scanner to get the numbers that broke the code. It gave me these errors:

Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Unknown Source)
at java.base/java.util.Scanner.next(Unknown Source)
at java.base/java.util.Scanner.nextDouble(Unknown Source)
at test2.calculator2.input(calculator2.java:45)
at test2.calculator2.main(calculator2.java:128)

Code:

package test2;

import java.util.Scanner;

public class calculator2 {
    public static int cNum, cOp;
    public static double num[], tNums, output;
    public static String op[];

public static void input() {
    boolean isInputting, invalidTNum, invalidOp;
    
    isInputting = true;
    invalidTNum = true;
    invalidOp = true;
    cNum = 1;
    cOp = 1;
    
    Scanner tNumInput = new Scanner(System.in);
    System.out.println("Please input a number larger or equal to 2.");
    System.out.println("How many numbers would you like?");
    tNums = tNumInput.nextDouble();
    
    while(invalidTNum) {
        if(tNums < 2) {
            invalidTNum = true;
            System.out.println("Please input a number larger or equal to 2.");
            System.out.println("How many numbers would you like?");
            tNums = tNumInput.nextDouble();
        } else {
            invalidTNum = false;
            tNumInput.close();
        }
    }
    
    num = new double[(int)tNums];
    op = new String[(int)tNums --];
    
    System.out.println("Please input number.");
    Scanner numInput = new Scanner(System.in);
41: num[cNum] = numInput.nextDouble();
    
    cNum ++;
    
    while(isInputting) {
        if(cNum >= tNums) {
            isInputting = false;
        } else {
            isInputting = true;
        }
        
        while(invalidOp) {
            System.out.printf("Please input operation. (+,-,*,/)");
            Scanner opInput = new Scanner(System.in);
            op[cOp] = opInput.nextLine();
            
            switch(op[cOp]) {
                case"+":
                case"-":
                case"*":
                case"/":
                    invalidOp = false;
                default:
                    invalidOp = true;
                    System.out.println("Your operation is invalid.");
            }
        }
        
        System.out.printf("Please input number.");
        num[cNum] = numInput.nextDouble();
        
        cOp ++;
        cNum ++;
        invalidOp = true;
    }
    
    
}

public static void count() {
    boolean isCounting = true;
    double awnswer;
    cNum = 1;
    cOp = 1;
    
    awnswer = num[cNum];
    cNum ++;
    
    while(isCounting) {
        if(cNum >= tNums) {
            isCounting = false;
        } else {
            isCounting = true;
        }
        
        switch(op[cOp]) {
            case"+":
                awnswer += num[cNum];
                break;
            case"-":
                awnswer -= num[cNum];
                break;
            case"*":
                awnswer *= num[cNum];
                break;
            case"/":
                awnswer /= num[cNum];
                break;
            default:
                break;
        }
        
        cNum ++;
        cOp ++;
    }
    
    output = awnswer;
}

public static void main(String args[]) {
    boolean reuse = true;
    
    while(reuse) {
128:    input();
        count();
        System.out.println(output);
    }
  }
}

Just to say, I really only just started on java, I'm really bad at it, so forgive me if I made some idiotic mistakes.

Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – Lino Jun 29 '18 at 09:06
  • 2
    And also please see: [Why is “Can someone help me?” not an actual question?](http://meta.stackoverflow.com/q/284236) – Lino Jun 29 '18 at 09:06
  • 2
    Possible duplicate of [java.util.NoSuchElementException - Scanner reading user input](https://stackoverflow.com/questions/13042008/java-util-nosuchelementexception-scanner-reading-user-input) – Ivar Jun 29 '18 at 09:08
  • Class names should begin with a capital letter and if you don't have a domain name to use as a package there's little point in using `package test2;` -- you might as well just leave it off and have your class in the unnamed package. – David Conrad Jun 29 '18 at 09:21

3 Answers3

1

You shouldn't make multiple instances of Scanner. Instead, just make one scanner at the start, and reuse that one throughout your program.

A fixed version might look like this:

package test2;

import java.util.Scanner;

public class calculator2 {
    public static int cNum, cOp;
    public static double num[], tNums, output;
    public static String op[];
    public static Scanner scanner;

    public static void input() {
        boolean isInputting, invalidTNum, invalidOp;

        isInputting = true;
        invalidTNum = true;
        invalidOp = true;
        cNum = 1;
        cOp = 1;

        System.out.println("Please input a number larger or equal to 2.");
        System.out.println("How many numbers would you like?");
        tNums = scanner.nextDouble();

        while(invalidTNum) {
            if(tNums < 2) {
                invalidTNum = true;
                System.out.println("Please input a number larger or equal to 2.");
                System.out.println("How many numbers would you like?");
                tNums = scanner.nextDouble();
            } else {
                invalidTNum = false;
                scanner.close();
            }
        }

        num = new double[(int)tNums];
        op = new String[(int)tNums --];

        System.out.println("Please input number.");
        41: num[cNum] = scanner.nextDouble();

        cNum ++;

        while(isInputting) {
            if(cNum >= tNums) {
                isInputting = false;
            } else {
                isInputting = true;
            }

            while(invalidOp) {
                System.out.printf("Please input operation. (+,-,*,/)");
                op[cOp] = scanner.nextLine();

                switch(op[cOp]) {
                    case"+":
                    case"-":
                    case"*":
                    case"/":
                    invalidOp = false;
                    default:
                    invalidOp = true;
                    System.out.println("Your operation is invalid.");
                }
            }

            System.out.printf("Please input number.");
            num[cNum] = scanner.nextDouble();

            cOp ++;
            cNum ++;
            invalidOp = true;
        }


    }

    public static void count() {
        boolean isCounting = true;
        double awnswer;
        cNum = 1;
        cOp = 1;

        awnswer = num[cNum];
        cNum ++;

        while(isCounting) {
            if(cNum >= tNums) {
                isCounting = false;
            } else {
                isCounting = true;
            }

            switch(op[cOp]) {
                case"+":
                awnswer += num[cNum];
                break;
                case"-":
                awnswer -= num[cNum];
                break;
                case"*":
                awnswer *= num[cNum];
                break;
                case"/":
                awnswer /= num[cNum];
                break;
                default:
                break;
            }

            cNum ++;
            cOp ++;
        }

        output = awnswer;
    }

    public static void main(String args[]) {
        boolean reuse = true;
        scanner = new Scanner(System.in);

        while(reuse) {
            input();
            count();
            System.out.println(output);
        }
    }
}
user464014
  • 140
  • 8
0

while calling tNumInput.nextDouble(); you should check if scanner has Element. otherwise it will this error.

 if(tNumInput.hasNext())
 tNumInput.next();

Also Make Sure to Calling tNumInput.close();

Mr. Roshan
  • 1,777
  • 13
  • 33
0

I think first, that one instance of Scanner is enough. Then, probably you should set Locale, to correctly define decimal point, because if you use , as decimal point, it will not be working in US locale.

try (Scanner scan = new Scanner(System.in)) {
    scan.useLocale(Locale.US);
    // ...
}
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35