Trying to make a simple english text calculator and i keep getting some errors. Here is what i have

/*
 * A calculator that calculates your answer and outputs it i english.
 */

package Calc;

/**
 *
 * @author Austin
 */

import java.text.*;
import java.util.Scanner;







public class Calc {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

      //Declaration
         Scanner scan = new Scanner(System.in);

       int Num1, Num2, Result;
       String Num1S, Num2S, OperationS;
       char operations;

       boolean Num1OK=true, Num2OK=true, operationsOK=true;



             //Inputs


            System.out.print("Enter your first number ");
              Num1 = scan.nextInt();
              {
              if (Num1 > 9 || Num1 < 0)
               System.out.println(" Must be a Number between 0 and 9");
              }



       System.out.print("Enter your second number ");
              Num2 = scan.nextInt();
              {
          if (Num2 > 9 || Num2 < 0)
              System.out.println(" Must be a Number between 0 and 9");
              }



       // Prompt
       System.out.println("\nEnter one of the operations +,-,*,/,^");
       operations = scan.next().charAt(0);



       //Operation and results
       switch(Num1)
       {
           case 0:
           Num1S="zero";
           case 1:
           Num1S="one";
           case 2:
           Num1S="two";
           case 3:
           Num1S="three";
           case 4:
           Num1S="four";
           case 5:
           Num1S="five";
           case 6:
           Num1S="six";
           case 7:
           Num1S="seven";
           case 8:
           Num1S="eight";
           case 9:
           Num1S="nine";

           Num1OK=false;


       }

       switch(Num2)
       {
         case 0:
           Num2S="zero";
           case 1:
           Num2S="one";
           case 2:
           Num2S="two";
           case 3:
           Num2S="three";
           case 4:
           Num2S="four";
           case 5:
           Num2S="five";
           case 6:
           Num2S="ix";
           case 7:
           Num2S="seven";
           case 8:
           Num2S="eight";
           case 9:
           Num2S="nine";


           Num2OK=false;

       }

     switch (operations)
     {
         case '+':
         OperationS=" plus" ;
         Result = Num1 + Num2;
         case '-':
         OperationS=" minus" ;
         Result = Num1 - Num2;
     case '*':
         OperationS=" multiplied by " ;
         Result = Num1 * Num2;
     case '/':
         OperationS=" divided by " ;
         Result = Num1 / Num2;
             case '^':
         OperationS=" to the power of" ;
         Result = (int) (Math.pow(Num1, Num2));

         operationsOK=false;

     }
     {
         System.out.println(+ Num1 + OperationS +  + Num2 + " is " + Result +););

     }

I need to some how get it to out put like this.

Five plus six is 11
I also need it to not allow you to enter anything below 0 or above 9
and if you enter a character that isnt an operation to output a message. im missing just a few things i blieve/hope! let me know

Recommended Answers

All 23 Replies

Your problem is here

System.out.print("Enter your first number ");
Num1 = scan.nextInt();
{
if (Num1 > 9 || Num1 < 0)
System.out.println(" Must be a Number between 0 and 9");
}

Why are you including this code in a block {}. Braces are not needed here. You make the same mistake a little further down as well.
One more thing. You should include break; in your switch statements.
Also, please use code tags when posting your code. Thanks.

I took those out but still coming up with a issue down where i am trying to out put the result. Keeps saying the same thing. Illegal start of expression or that i need to initialize OperationS and Results

Did you remove these braces too?

{
System.out.println(+ Num1 + OperationS + + Num2 + " is " + Result +) ;

}

Please repost your revised code using code tags to make it easier to read

/*
 * A calculator that calculates your answer and outputs it i english.
 */

package Calc;

/**
 *
 * @author Austin
 */

import java.text.*;
import java.util.Scanner;







public class Calc {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

      //Declaration
         Scanner scan = new Scanner(System.in);

       int Num1, Num2, Result;
       String Num1S, Num2S, OperationS;
       char operations;

       boolean Num1OK=true, Num2OK=true, operationsOK=true;



             //Inputs


            System.out.print("Enter your first number ");
              Num1 = scan.nextInt();
              
              if (Num1 > 9 || Num1 < 0)
               System.out.println(" Must be a Number between 0 and 9");
              
              
           

       System.out.print("Enter your second number ");
              Num2 = scan.nextInt();
              
          if (Num2 > 9 || Num2 < 0)
              System.out.println(" Must be a Number between 0 and 9");
              



       // Prompt
       System.out.println("\nEnter one of the operations +,-,*,/,^");
       operations = scan.next().charAt(0);



       //Operation and results
       switch(Num1)
       {
           case 0:
           Num1S="zero";
           break;
           case 1:
           Num1S="one";
           break;
           case 2:
           Num1S="two";
           break;
           case 3:
           Num1S="three";
           break;
           case 4:
           Num1S="four";
           break;
           case 5:
           Num1S="five";
           break;
           case 6:
           Num1S="six";
           break;
           case 7:
           Num1S="seven";
           break;
           case 8:
           Num1S="eight";
           break;
           case 9:
           Num1S="nine";
           break;

           Num1OK=false;
           

       }

       switch(Num2)
       {
         case 0:
           Num2S="zero";
           break;
           case 1:
           Num2S="one";
           break;
           case 2:
           Num2S="two";
           break;
           case 3:
           Num2S="three";
           break;
           case 4:
           Num2S="four";
           break;
           case 5:
           Num2S="five";
           break;
           case 6:
           Num2S="ix";
           break;
           case 7:
           Num2S="seven";
           break;
           case 8:
           Num2S="eight";
           break;
           case 9:
           Num2S="nine";
           break;

           
           Num2OK=false;
           
       }

     switch (operations)
     {
         case '+':
         OperationS=" plus" ;
         Result = Num1 + Num2;
         break;

         case '-':
         OperationS=" minus" ;
         Result = Num1 - Num2;
         break;

     case '*':
         OperationS=" multiplied by " ;
         Result = Num1 * Num2;
         break;

     case '/':
         OperationS=" divided by " ;
         Result = Num1 / Num2;
         break;

         case '^':
         OperationS=" to the power of" ;
         Result = (int) (Math.pow(Num1, Num2));
         break;

         operationsOK=false;

     

         System.out.println(+ Num1 + OperationS +  + Num2 + " is " + Result +););
          

     }
    }
    }

Line 174-- you have an extra semicolon and right parenthesis at the end of the statement.
It showed up as a smiley face on your first post because you didnt use code tags. That's one of the many reasons why we stress the use of code tags so much around here. ;)

i took those out, have been messing with this code for a few days now, still the last line, the System.out.println( + Num1 + .....); is still saying it has a illegal start of expression.

i took those out, have been messing with this code for a few days now, still the last line, the System.out.println( + Num1 + .....); is still saying it has a illegal start of expression.

Don't start the inside of the println() with a plus sign. See if that works. And for the future, post your current code, not one from a few days ago. Thanks.

/*
 * A calculator that calculates your answer and outputs it i english.
 */

package Calc;

/**
 *
 * @author Austin
 */

import java.text.*;
import java.util.Scanner;







public class Calc {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

      //Declaration
         Scanner scan = new Scanner(System.in);

       int Num1, Num2, Result;
       String Num1S, Num2S, OperationS;
       char operations;

       boolean Num1OK=true, Num2OK=true, operationsOK=true;



             //Inputs


            System.out.print("Enter your first number ");
              Num1 = scan.nextInt();
              
              if (Num1 > 9 || Num1 < 0)
               System.out.println(" Must be a Number between 0 and 9");
              
              
           

       System.out.print("Enter your second number ");
              Num2 = scan.nextInt();
              
          if (Num2 > 9 || Num2 < 0)
              System.out.println(" Must be a Number between 0 and 9");
              



       // Prompt
       System.out.println("\nEnter one of the operations +,-,*,/,^");
       operations = scan.next().charAt(0);



       //Operation and results
       switch(Num1)
       {
           case 0:
           Num1S="zero";
           case 1:
           Num1S="one";
           case 2:
           Num1S="two";
           case 3:
           Num1S="three";
           case 4:
           Num1S="four";
           case 5:
           Num1S="five";
           case 6:
           Num1S="six";
           case 7:
           Num1S="seven";
           case 8:
           Num1S="eight";
           case 9:
           Num1S="nine";
           
           
           
           Num1OK=false;
           

       }

       switch(Num2)
       {
         case 0:
           Num2S="zero";
           case 1:
           Num2S="one";
           case 2:
           Num2S="two";
           case 3:
           Num2S="three";
           case 4:
           Num2S="four";
           case 5:
           Num2S="five";
           case 6:
           Num2S="six";
           case 7:
           Num2S="seven";
           case 8:
           Num2S="eight";
           case 9:
           Num2S="nine";
           
           
           Num2OK=false;
           
       }

     switch (operations)
     {
         case '+':
         OperationS=" plus" ;
         Result = Num1 + Num2;
         
         case '-':
         OperationS=" minus" ;
         Result = Num1 - Num2;
         
     case '*':
         OperationS=" multiplied by " ;
         Result = Num1 * Num2;
         
     case '/':
         OperationS=" divided by " ;
         Result = Num1 / Num2;
             case '^':
         OperationS=" to the power of" ;
         Result = (int) (Math.pow(Num1, Num2));
                 
                 

         operationsOK=false;

     }
     
         System.out.println(" " + Num1 + OperationS +  + Num2 + " is " + Result +);
          
     
     
    }
}

This is the most recent code. The last line still has either "illegal start" or that neither OperatorS or Result have been initialized.

The println() shouldn't end with a plus sign either. ;)
But that shouldn't clear up the problem regarding initialization.
Try it and post back word for word what the compiler says.

Alright that fixed that part, now the last thing is that the output is not the correct way, its still putting a # instead of a word. IE 6 should be Six

Alright that fixed that part, now the last thing is that the output is not the correct way, its still putting a # instead of a word. IE 6 should be Six

I think you want to print Num1S instead of Num1. Same for num2. You want the string values not the ints.

alright, i have that, i have just one more thing left, i need to get it to say that if i input a symbol/char other then +,-,/,^,* that it outputs " Invalid Operations"

Would u put this in the operation case?

alright, i have that, i have just one more thing left, i need to get it to say that if i input a symbol/char other then +,-,/,^,* that it outputs " Invalid Operations"
Would u put this in the operation case?

I would make that the default case in the operations switch statement.

wouldnt it be easier to use an if statement, if not would i need to do a case statement for all the symbols then lol?

You already have the cases written for the operations. Just add the default.

switch (operations)
     {
         case '+':
         OperationS=" plus" ;
         Result = Num1 + Num2;
         
         case '-':
         OperationS=" minus" ;
         Result = Num1 - Num2;
         
     case '*':
         OperationS=" multiplied by " ;
         Result = Num1 * Num2;
         
     case '/':
         OperationS=" divided by " ;
         Result = Num1 / Num2;
             case '^':
         OperationS=" to the power of" ;
         Result = (int) (Math.pow(Num1, Num2));
                 
                 default: System.out.println("Illegal operation");
               break;

         operationsOK=false;

     }

This way the default will run anytime an illegal operation is input. Its always good to have defaults in switch statements to test for illegal input.

very true, that works much better!
and the very very very last thing. If you look where my Num2 is, up top where you input the int, underneath it says Num2 = scan.nextInt();

Is there a way to make this so it will goto the next line.
Ill explain.
I enter Num1
Hops down the next line to Num2 and i enter Num2

from here it stays on Num2 and doesnt go down to the next spot where i input my operation, i know there is a command for this instead of Num2 = scan.nextInt();
i just seem to be blanking out all day

System.out.println() goes to the next line. While print() stays on the same line. Looking over your code I see a few pint statements. Change them to println to go to the next line.

thank you so much for all your help it is very appreciated!!! code has been solved and fixed!

Thats what we are here for. Great working with you.

Dear, initialization means giving some starting value to the variables you declare. If we don't initialize variables, they may contain any unpredictable value. This often leads to wrong results. So if your compiler is reporting that your variable is uninitialised, just initialise them with some starting value.

Dear, initialization means giving some starting value to the variables you declare. If we don't initialize variables, they may contain any unpredictable value. This often leads to wrong results. So if your compiler is reporting that your variable is uninitialised, just initialise them with some starting value.

This thread has already been solved. The whole point of marking a thread solved is so people don't waste their time posting in it.

I have one more question on this

I would like the program to exit if a number greater then 9 is entered. How would i do this.

Scanner scan = new Scanner(System.in);

       int Num1, Num2, Result = 0;
       String Num1S = null , Num2S = null , OperationS = null  ;
       char operations;

       boolean Num1OK=true, Num2OK=true, operationsOK=true;



             //Inputs


            System.out.println("Enter your first number ");
              Num1 = scan.nextInt();

              if (Num1 > 9 || Num1 < 0)
               System.out.println(" Must be a Number between 0 and 9");
              
              


       System.out.println("Enter your second number ");
              Num2 = scan.nextInt();

          if (Num2 > 9 || Num2 < 0)
              System.out.println(" Must be a Number between 0 and 9");



       System.out.println("\nEnter one of the operations +,-,*,/,^");
       operations = scan.next().charAt(0);



       //Operation and results
       switch(Num1)
       {
           case 0:
           Num1S="zero";
           break;
           case 1:
           Num1S="one";
           break;
           case 2:
           Num1S="two";
           break;
           case 3:
           Num1S="three";
           break;
           case 4:
           Num1S="four";
           break;
           case 5:
           Num1S="five";
           break;
           case 6:
           Num1S="six";
           break;
           case 7:
           Num1S="seven";
           break;
           case 8:
           Num1S="eight";
           break;
           case 9:
           Num1S="nine";
           break;

           


           Num1OK=false;


       }

       switch(Num2)
       {
         case 0:
           Num2S="zero";
           break;
           case 1:
           Num2S="one";
           break;
           case 2:
           Num2S="two";
           break;
           case 3:
           Num2S="three";
           break;
           case 4:
           Num2S="four";
           break;
           case 5:
           Num2S="five";
           break;
           case 6:
           Num2S="six";
           break;
           case 7:
           Num2S="seven";
           break;
           case 8:
           Num2S="eight";
           break;
           case 9:
           Num2S="nine";
           break;

           


           Num2OK=false;

       }

     switch (operations)
     {

         case '+':
         OperationS=" plus " ;
         Result = Num1 + Num2;
         break;

         case '-':
         OperationS=" minus " ;
         Result = Num1 - Num2;
         break;


     case '*':
         OperationS=" multiplied by " ;
         Result = Num1 * Num2;
         break;

     case '/':
         OperationS=" divided by " ;
         Result = Num1 / Num2;
         break;
             case '^':
         OperationS=" to the power of " ;
         Result = (int) (Math.pow(Num1, Num2));
         break;

         default: System.out.println(" Invalid Operation ");

         break;



         operationsOK=false;

     }

         System.out.println(""  + Num1S + OperationS +   Num2S + " is " + Result );



    }
}

Use an if else statement.

if(number is greater than 9)
System.out.println("Illegal entry");
else
//run code

In the else block include the code that executes the program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.