-1

Does anyone know why my scanner is asking for input every three lines, rather than every one line? If you run it, you will see that it asks the question first, but then doesn't for two more lines, then it asks the question again. How do I get it to ask a question every line, let alone store it in a linked list.

import java.util.Scanner;

public class StackCalc {

    public static StackLL stackll = new StackLL();

    public static void create() {

        String hold = new String();
        Scanner scanner = new Scanner(System.in);
        boolean running = true;
        while (true) {
        System.out.print("Enter number, math operation(+,-,*, or /), or Q to quit: ");
        hold = scanner.next();


        if (scanner.next().equals("Q") || scanner.next().equals("q")) {
            System.out.println(hold);
            break;
        }
    }

}

public static boolean checkInt(String s, int it){
    Scanner scan = new Scanner(s.trim());
    if(!scan.hasNextInt(it))
        return false;
    scan.nextInt(it);
    return !scan.hasNext();
    }

    public static void main(String args[]) {
        create();
    }
}

Any input would be awesome.

Matt
  • 51
  • 5

1 Answers1

3

It is because you are using the method Scanner.next() three times:

hold = scanner.next()

scanner.next().equals("Q")

and scanner.next().equals("q")

Instead, only call it once by changing:

scanner.next().equals("Q") to hold.equals("Q")

and

scanner.next().equals(q") to hold.equals("Q")

sameer-s
  • 395
  • 2
  • 11