0

So I am trying to make a calculator in VSCode but for some reason, my scanner is not running. Here is the full code for my project so far...

import java.util.Scanner;
class main { //Main
 public static void main(String[] args) {
  int firstnumber = 0;
  int secondnumber = 0;
  String Operator = "-";

  boolean erase = true;

  Scanner scan = new Scanner(System.in);

  while (erase = true) { //While Loop (main)
   System.out.println("Enter First Number");
   firstnumber = scan.nextInt();



   System.out.println("Enter Second Number");
   secondnumber = scan.nextInt();

   System.out.println("Select Operator.");
   System.out.println("For Multiplaction: Type X");
   System.out.println("For Division: Type %");
   System.out.println("For Addition: Type +");
   System.out.println("For Subtraction: Type -");
   Operator = scan.nextLine()
  } //While Loop (main)
 }
} //Main

For some reason when I run that code it runs the first two scanners but then fails to run the one that asks for the operator. If anyone knows why I would be pleased to know.

Peter Badida
  • 7,432
  • 8
  • 33
  • 74
Miqhtie
  • 63
  • 6
  • 1
    How do you know it "fails to run" the scanner? –  Sep 14 '19 at 22:17
  • 1
    What does a "running" `Scanner` look like? – Zephyr Sep 14 '19 at 22:19
  • 1
    I assume you are looking for `scan.next()` instead of `scan.nextLine()`. Per the JavaDocs, .nextLine "Advances this scanner past the current line and returns the input that was skipped.". Also, I assume you want `while(erase==true)` rather than `while(erase=true)`. The first is comparison, the second performs assignment. – KellyM Sep 14 '19 at 22:20
  • 2
    @KellyM - Actually the `==true` is completely unnecessary with a `boolean` :) – Zephyr Sep 14 '19 at 22:20
  • What I imagine is happening is that after being prompted for the second number, the user types a number followed by newline (enter). After the corresponding call to nextInt, the scanner is positioned immediately before the newline. The call to nextLine then just consumes that newline, returning the skipped characters, probably none. –  Sep 14 '19 at 22:21
  • 2
    @Zephyr, you are absolutely right there, was not thinking. :) `while(erase)` is just sufficient. Thanks for pointing that out. – KellyM Sep 14 '19 at 22:22
  • Could you print error output ? – Bilal Ekrem Harmanşa Sep 14 '19 at 22:54
  • Your last Scanner#nextInt() method call is holding the System.lineSeparator() character which is given when the ENTER key is hit. The Scanner#nextInt() method does not consume this like the **.nextLine()** method does, it remains within the scanner buffer. When Scanner.nextLine() is called it takes this line break from the buffer automatically therefore thinking data has been supplied. After `secondnumber = scan.nextInt();` add the code line: `scan.nextLine();` to consume the Line Break; – DevilsHnd Sep 15 '19 at 01:16
  • Possible duplicate of [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Progman Sep 15 '19 at 07:19
  • May I knkw your JDK version – obnoxiousnerd Sep 15 '19 at 12:46
  • You should use a char for a operator – obnoxiousnerd Sep 15 '19 at 12:47

2 Answers2

0

I did change the operator to char, andd now it works:-

import java.util.Scanner;
class main { //Main
 public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int firstnumber = 0;
  int secondnumber = 0;
  char operator;

  boolean erase = true;



  while (erase) { //While Loop (main)
   System.out.println("Enter First Number");
   firstnumber = scan.nextInt();



   System.out.println("Enter Second Number");
   secondnumber = scan.nextInt();

   System.out.println("Select Operator.");
   operator = scan.next().charAt(0);
   System.out.println("For Multiplaction: Type X");
   System.out.println("For Division: Type %");
   System.out.println("For Addition: Type +");
   System.out.println("For Subtraction: Type -");

  } //While Loop (main)
 }
} //Main 

Maybe your nextLine() doesn't trigger because you already have two scanner prompts and so it behaves to be accepted input. Maybe you should debug more on that. Still, scan.next(); works... Tried on Visual Studio Code 1.38.1 using Oracle JDK 1.8.0_u221

obnoxiousnerd
  • 570
  • 1
  • 3
  • 16
0

use scan.next(); instead.

public static void main(String[] args) {
    int firstnumber;
    int secondnumber;
    String operator;
    Scanner scan = new Scanner(System.in);
    while (true) { //While Loop (main)
        System.out.println("Enter First Number");
        firstnumber = scan.nextInt();


        System.out.println("Enter Second Number");
        secondnumber = scan.nextInt();

        System.out.println("Select Operator.");
        System.out.println("For Multiplaction: Type X");
        System.out.println("For Division: Type %");
        System.out.println("For Addition: Type +");
        System.out.println("For Subtraction: Type -");
        operator = scan.next();

        System.out.println(firstnumber);
        System.out.println(secondnumber);
        System.out.println(operator);
    } //While Loop (main)
}
rjeeb
  • 391
  • 1
  • 10