Java Calculator - Need help


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Java Calculator - Need help

  1. #1
    Join Date
    Apr 2003
    Posts
    8

    Java Calculator - Need help

    Howdy; i'm trying to build a java calculator application; I keep getting errors when i compile; this is the error:

    ---------------------------------------------------------------------

    C:\javap>javac Calc.java
    Calc.java:46: variable result might not have been initialized
    System.out.println("Answer= " + result);
    ^
    1 error

    ---------------------------------------------------------------------

    and this is the code; mind you i AM a beginners, so any help AT ALL would be great.

    --------------------------------------------------------------------

    //import Keyboard;

    public class Calc
    {

    public static void main(String[] args)
    {

    String another = "y";

    while(another.equalsIgnoreCase("y"))
    {
    System.out.println("Enter the first number: ");
    int first = Keyboard.readInt();

    System.out.println("Enter operator (+, -, * or /): ");
    int op = Keyboard.readInt();

    System.out.println("Enter second number: ");
    int second = Keyboard.readInt();

    int result;

    switch(op)
    {
    case '+':
    result = first + second;
    break;

    case '-':
    result = first - second;
    break;

    case '*':
    result = first * second;
    break;

    case '/':
    result = first / second;
    break;

    default:
    System.out.println("Wrong operator; use +, -, *, or / functions");

    };
    System.out.println("Answer= " + result);
    System.out.println("Another calculation? (y/n) ");
    another = Keyboard.readString();


    }

    }

    }

    -----------------------------------------------------------------

    basically what it's meant to do is ask the user for a number, then an operator, then a number than give an answer. if the user wants to make a another calculation, s/he pressed "y" and it goes through again.

    the problem i have (apart from the error ) is that i dont know how to allow for multiple calculations:

    eg; 4-3+34=blah or 56*6-12=blah

    please help me!

  2. #2
    Join Date
    Mar 2003
    Posts
    84
    int result;

    chage this to

    int result = 0;

    the variable needs to be initialized to something when it's created even though you are changing it's value.

    you have to do this when you use variables in if blocks or try blocks or switch blocks and stuff like that
    I'm surprised more of you people don't get hit by cars.

  3. #3
    Join Date
    Apr 2003
    Posts
    8
    this is what i got after a few changes

    Code:
    public class Calculator {    	
    public static void main(String[] args) 	
    {        	
    double result;        	
    do {            		
    System.out.println ("Enter 1st number: ");            		int number1 = Keyboard.readInt ();    
            	System.out.println ("Enter operator (+, -, * or /): ");            		
    char op = Keyboard.readChar ();            		
    System.out.println ("Enter 2nd number: ");            		
    int number2 = Keyboard.readInt ();            		result = makeCalc (number1, number2, op);        	    
    } 		
    while (result == Double.NaN);        		
    System.out.println ("The result is " + (int) result);    	
    }    
    private static double makeCalc (int number1, int number2, char op) {        	
    double result;        	
    switch (op) 	
    {            	
    case '+':                	  
    result = number1 + number2;               	
    break;            	
    
    case '-':               	  
    result = number1 - number2;                	
    break;
    
    case '*':                	 
    result = number1 * number2;                	
    break;            	
    
    case '/':                	  
    result = division(number1, number2);               	
    break;            	   	
    default:                	  	
    
    result = Double.NaN;                	
    break;        	
    }        	
    return result;    
    }    
    private static double division (int number1, int number2) {        	
    return (number2 == 0) ? 	
    Double.NaN : number1 / number2;    
    }private static int division(int number1, int number2){       
     if(number2 == 0) //testing division        
    {           
     System.out.println("Error you can not divide by zero");            
    return 0;        
    }        
    else return(number1/number2);      	
    }}
    can someone fix it so the ******* works, i've been trying to get it running all night and im sick of; i need someone to fill in whatever the **** is wrong

  4. #4
    Join Date
    Mar 2003
    Posts
    84
    post the errors your compiler is giving you.

    that way we don't have to copy and paste it ourselves and compile it.
    I'm surprised more of you people don't get hit by cars.

  5. #5
    Join Date
    Mar 2003
    Posts
    834
    The compiler error was pretty clear about the problem:
    Code:
    C:\docs\forums\calc_Test>javac Calculator.java
    Calculator.java:45: division(int,int) is already defined in Calculator
    }private static int division(int number1, int number2){
                        ^
    Calculator.java:7: cannot resolve symbol
    symbol  : variable Keyboard
    location: class Calculator
    System.out.println ("Enter 1st number: ");                      int number1 = Keyboard.readInt ();
                                                                                  ^
    Calculator.java:9: cannot resolve symbol
    symbol  : variable Keyboard
    location: class Calculator
    char op = Keyboard.readChar ();
              ^
    Calculator.java:11: cannot resolve symbol
    symbol  : variable Keyboard
    location: class Calculator
    int number2 = Keyboard.readInt ();                      result = makeCalc (number1, number2, op);
                  ^
    4 errors
    Put simply, you've tried to define two identical methods:
    Code:
    private static double division (int number1, int number2){        	
      return (number2 == 0) ? Double.NaN : number1 / number2;    
    }
    
    private static int division(int number1, int number2) {       
     if(number2 == 0) { //testing division        
       System.out.println("Error you can not divide by zero");            
       return 0;        
      } else {
         return(number1/number2);      	
      }
    }
    (You were also missing a brace).

    ArchAngel.
    ArchAngel.
    O:-)

  6. #6
    Join Date
    Nov 2002
    Posts
    138
    regarding your "multiple calculations" do you want the user to enter it in the form you gave in your example ( eg; 4-3+34=blah or 56*6-12=blah )? you'd need to do some parsing on this. and there's this thing called operator precedence too. it's quite tricky, but not really hard. you can do this through a stack, or that depends on you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center