0

The result i get :

Enter a number : 
5
Enter another number : 
4
What do you want to perform on these numbers? 
You have entered a wrong action, please try again 

Where did i go wrong in my code?

import java.util.Scanner;

    public class App {

        public static void main(String[] args) {

            double num1, num2;
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter a number : ");
            num1 = sc.nextDouble();
            System.out.println("Enter another number : ");
            num2 = sc.nextDouble();

            System.out.println("What do you want to perform on these numbers? ");
            String word = sc.nextLine();

            sc.close();

            double result = 0;
            switch (word) {
            case "Addition":
                result = num1 + num2;
                System.out.println(num1 + " " + word + " " + num2 + " : " + result);
                break;
            case "Subtraction":
                result = num1 - num2;
                System.out.println(num1 + " " + word + " " + num2 + " : " + result);
                break;
            case "Multiplication":
                result = num1 * num2;
                System.out.println(num1 + " " + word + " " + num2 + " : " + result);
                break;
            case "Division":
                result = num1 / num2;
                System.out.println(num1 + " " + word + " " + num2 + " : " + result);
                break;
            default:
                System.out.println("You have entered a wrong action, please try again ");
                break;

            }

        }

    }
Lino
  • 17,873
  • 4
  • 40
  • 57
Stewie
  • 27
  • 8
  • Please see: [Why is “Can someone help me?” not an actual question?](http://meta.stackoverflow.com/q/284236) – Lino Jun 29 '18 at 09:07
  • And also: 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:07
  • Sorry, new to this. Will correct myself for future questions. thank you. – Stewie Jun 29 '18 at 09:10

3 Answers3

0

Can you change your code like below:

        System.out.println("What do you want to perform on these numbers? ");
        sc.nextLine(); // ADD THIS LINE
        String word = sc.nextLine();

The problem here is with the num2 = sc.nextDouble(); the newline char is not consumed.

Below is the code I use:

public static void main(String args[]) throws Exception {
      //  A1Pattern.printPattern(26);


        double num1, num2;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a number : ");
        num1 = sc.nextDouble();
        System.out.println("Enter another number : ");
        num2 = sc.nextDouble();

        System.out.println("What do you want to perform on these numbers? ");
        sc.nextLine();
        String word = sc.nextLine();

        sc.close();

        double result = 0;
        switch (word) {
            case "Addition":
                result = num1 + num2;
                System.out.println(num1 + " " + word + " " + num2 + " : " + result);
                break;
            case "Subtraction":
                result = num1 - num2;
                System.out.println(num1 + " " + word + " " + num2 + " : " + result);
                break;
            case "Multiplication":
                result = num1 * num2;
                System.out.println(num1 + " " + word + " " + num2 + " : " + result);
                break;
            case "Division":
                result = num1 / num2;
                System.out.println(num1 + " " + word + " " + num2 + " : " + result);
                break;
            default:
                System.out.println("You have entered a wrong action, please try again ");
                break;

        }

    }
  • OUTPUT:

    Enter a number : 1 Enter another number : 2 What do you want to perform on these numbers? Subtraction 1.0 Subtraction 2.0 : -1.0

Skynet
  • 7,522
  • 5
  • 40
  • 78
0

Instead of using sc.nextline() on line 15 use sc.next(). The program will now wait for your input before continuing.

Anthony
  • 16
0

The java.util.Scanner.next() method finds and returns the next complete token from this scanner.

Use scanner.next() instead of scanner.nextLine().

Ananya Antony
  • 178
  • 10
  • Yeah it works. The Previous answer from Anthony was to do the same. But can you tell me the difference – Stewie Jun 29 '18 at 09:17
  • The same is already answered in stackoverflow. You can refer the below link.https://stackoverflow.com/questions/22458575/whats-the-difference-between-next-and-nextline-methods-from-scanner-class – Ananya Antony Jun 29 '18 at 09:25
  • Okay will follow up – Stewie Jun 29 '18 at 09:27