Making a calculator using switch case


Making a calculator using switch case



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;

}

}

}





Please see: Why is “Can someone help me?” not an actual question?
– Lino
Jun 29 at 9: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.
– Lino
Jun 29 at 9:07





Sorry, new to this. Will correct myself for future questions. thank you.
– Stewie
Jun 29 at 9:10




3 Answers
3



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


sc.nextline()


sc.next()





Works as expected now. Can you tell me why sc.nextLine() did not work and sc.next() does
– Stewie
Jun 29 at 9:15



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.


num2 = sc.nextDouble();



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





Enter a number : 5 Enter another number : 4 What do you want to perform on these numbers? Addition You have entered a wrong action, please try again
– Stewie
Jun 29 at 9:06





It accepts input now but does not do the calculation but just prints out default statement
– Stewie
Jun 29 at 9:06





Can you clarify? i don't follow you
– Stewie
Jun 29 at 9:14



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



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





Yeah it works. The Previous answer from Anthony was to do the same. But can you tell me the difference
– Stewie
Jun 29 at 9:17





The same is already answered in stackoverflow. You can refer the below link.stackoverflow.com/questions/22458575/…
– Ananya Antony
Jun 29 at 9:25






Okay will follow up
– Stewie
Jun 29 at 9:27






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV