2 Replies - 194 Views - Last Post: 21 February 2011 - 07:21 PM Rate Topic: -----

#1 eric4215  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 09-September 10

validate a mathmatical operator

Posted 21 February 2011 - 06:11 PM

I am making a command line calculator in Java, I have figured out most of the project, the calculating essentials, but I need help with the part where the user inputs a string variable which is the mathematic sign, I cannot figure out how to validate that it is only a “-“,”+”,”/” or “*” sign, I have seen examples on validating an integer but can’t see how it would apply to an operator, Here’s the code I have so far starting from the last mathematical operation, I’m sure there a better way to do it.
  else {
                 if (operator.equals("/"))

                 {answer = A/ B;
                 System.out.printf("The quotient of " + A + " and " + B + "is %20.2f%n", answer);
                     {
                         if (B == 0)
                 System.out.println("You cannot devide by zero!!!");
                     }
                     System.out.print(" Do you wish to continue ? 1 = Yes; 0 = No: ");
                     Continue = input.nextInt();
                    System.out.println();
                     }
              {
                  while (!operator.equals("+")&&
                      !operator.equals("-")&&
                      !operator.equals("*")&&
                      !operator.equals("/"));
                  System.out.println("You must choose a correct operator!!!");
                     }
                  System.out.print(" Do you wish to continue ? 1 = Yes; 0 = No: ");
                     Continue = input.nextInt();
                    System.out.println();
                    



Is This A Good Question/Topic? 0
  • +

Replies To: validate a mathmatical operator

#2 eric4215  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 09-September 10

Re: validate a mathmatical operator

Posted 21 February 2011 - 07:15 PM

I am also trying it this way, but I run into a problem with the string operator, since it will not let me use a string with a switch, so I changed it to char, but I still get errors mainly with the "enter the operator" statement, do i need to convert anything, which way would work better, and what am I doing wrong. Any help would be appreciated. Thanks.

   
        char operator;
        double answer = 0;
        boolean valid = true;

        Scanner input = new Scanner(System.in);
        

        int Continue = 1;
        while(Continue == 1)

        {
          
            System.out.print(" Welcome to the JAVA Calculator!!!");
            System.out.println();
           

	    System.out.print(" Enter the first number : ");
            double A = input.nextDouble();

            System.out.print(" Enter the second number : ");
            double B = input.nextDouble();

            System.out.print(" Enter the operator symbol : ");
            operator = input.next();
          
            switch (operator)
            {
                case '+':
                answer = A + B;
                break;
               
                case '-':
                answer = A - B;
                break;
                
                case '*':
                    answer = A*B;
break;
                   
                case '/':
                    answer = A/B;
               break;
               
                default:
                  valid = false;
                  break;
            }
            
        
            if (valid == true)
                System.out.printf("The answer is %20.2f%n", answer);
            else
                 System.out.println("You must choose a correct operator!!!");
                    
            }


Was This Post Helpful? 0
  • +
  • -

#3 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: validate a mathmatical operator

Posted 21 February 2011 - 07:21 PM

The issue is that you can't switch on a String...only char. Use if statements instead or convert it to char (using charAt)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1