Note that there are some explanatory texts on larger screens.

plurals
  1. POJava - validate operator
    primarykey
    data
    text
    <p>I written the below program, but confused on part b of the question. Question is- Write a program that will evaluate simple expressions such as 17 + 3 and 3.14159 * 4.7. The expressions are to be typed in by the user. The input always consists of a number, followed by an operator, followed by another number. The operators that are allowed are +, -, *, and /. You are not required to perform data validation here as it is to be done in part (b) below. Your program output must show the expression entered together with the result (e.g. 17 + 3 = 20).</p> <p>Part B: Modify your program in part (a) above to validate the operator entered. The program repeats for the input until a valid operator is entered. You are required to make use of method for the validation.</p> <p>Here's what i wrote</p> <pre><code>import java.util.Scanner; public class JavaCalculator { public static void main(String[] args) { Scanner console = new Scanner(System.in); double digit1; double digit2; double total; String operator1; System.out.print("Enter 1st number: "); digit1 = console.nextDouble(); System.out.print("Enter the operator: "); operator1=console.next(); System.out.print("Enter 2nd number: "); digit2 = console.nextDouble(); if (operator1.equals("-")) { total = digit1-digit2; System.out.println(+digit1+ "-" +digit2+ "=" +total); } else if (operator1.equals("+")) { total = digit1+digit2; System.out.println(+digit1+ "+" +digit2+ "=" +total); } else if (operator1.equals("*")) { total = digit1*digit2; System.out.println(+digit1+ "*" +digit2+ "=" +total); } else if (operator1.equals("+")) { total = digit1/digit2; System.out.println(+digit1+ "/" +digit2+ "=" +total); } } } </code></pre> <p>Just got started out with Java, please bear with me or ignorance here in this question. On which line should I get started on Part B to validate the operator?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload