Results 1 to 2 of 2

Thread: Java Calculator (Not GUI)

  1. #1
    Join Date
    Apr 2011
    Posts
    1

    question Java Calculator (Not GUI)

    I need to use StringTokenizer, which I have, but something is wrong. I keep getting errors and I'm having some trouble finding out where. I know every time the program is executed, the while statement is not implemented. Here is my code so far:
    PHP Code:
    import java.util.*; 
    public class 
    Calculator

        private 
    StringTokenizer tokenizer
        private 
    String token
        
           
        public 
    Calculator(String line
        { 
            
    tokenizer = new StringTokenizer(line); 
            
    token tokenizer.nextToken(); 
        } 

        public 
    double Evaluate() 
        { 
            return 
    Expression(); 
        } 
        
        private 
    double Primary() 
        { 
            
    double result
          
            if(
    token.equals(""))
            { 
                
    token tokenizer.nextToken(); 
                
    result Expression(); 
            } 
            else 
            { 
                
    result Double.valueOf(token).doubleValue(); 
            } 
          
            
    token tokenizer.nextToken(); 
            return 
    result
        } 
        
        private 
    double Term() 
        { 
            
    double nextValue
            
    double result
          
            
    result Primary(); 
          
            while(
    token.equals("*")) 
            { 
                
    token tokenizer.nextToken(); 
                
    nextValue Primary(); 
                
    result *= nextValue
            } 
            
            while(
    token.equals("/")) 
            { 
                
    token tokenizer.nextToken(); 
                
    nextValue Primary(); 
                
    result /= nextValue
            }   
            return 
    result;
        } 
        
        private 
    double Expression() 
        { 
            
    double nextValue
            
    double result
         
            
    result Term(); 
         
            while(
    token.equals("+")) 
            {           
                
    token tokenizer.nextToken(); 
                
    nextValue Term(); 
                
    result += nextValue
            } 
            
            while(
    token.equals("-")) 
            { 
                
    token tokenizer.nextToken(); 
                
    nextValue Term(); 
                
    result -= nextValue
            } 
          
            return 
    result
         } 
        
         public static 
    void main (String[] args)
         { 
                 
    Scanner input = new Scanner(System.in);            
                 
    String line
          
                 
    String choice "y";
                 while(
    choice.equalsIgnoreCase("y")) 
                 { 
                     
    System.out.print("Enter an expression: "); 
                     
    line input.nextLine(); 
                     
                     if(
    line.length() == 0
                     { 
                         
    System.out.println("Invalid input."); 
                         
    System.out.println("Try again? (Y/N): "); 
                         
    choice input.nextLine(); 
                         if(
    choice.equalsIgnoreCase("n"))
                         {
                             
    System.out.println("Bye.");
                             
    System.exit(0);
                         }
                     }   
                     
                 
    Calculator expn = new Calculator(line); 
                 
    System.out.println("Result is " expn.Evaluate()); 
             } 
           } 
       } 
    This is a sample output it should be:

    Enter an expression: 4.45 + 1.0
    Result is 5.45
    Try again? y
    Enter an expression: 2.3 – 10.77
    Result is -8.47
    Try again? y
    Enter an expression: 5.3
    Input error
    Try again? y
    Enter an expression: 5.4 * 2.7.7
    Input error
    Try again? y
    Enter an expression: 6.0 + * 3.0
    Input error
    Try again? n
    Bye.


    This is what I get:

    Enter an expression: 4.45 + 1.0 =
    Result is 5.45
    Enter an expression: 2.3 - 10.77 =
    Result is -8.469999999999999
    Enter an expression:
    ..............it repeats until manually terminating the program.


    Any help would be appreciated. Thanks in advance.

  2. #2
    Join Date
    Dec 2007
    Posts
    2,291

    Re: Java Calculator (Not GUI)

    You do not have enough libraries imported and are not making use of the try-catch-finally paradigm for throwing exception. You should study the following program:

    import java.io.FileReader;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.util.Scanner;
    import java.util.Locale;

    public class ScanSum {
    public static void main(String[] args) throws IOException {
    Scanner s = null;
    double sum = 0;
    try {
    s = new Scanner(
    new BufferedReader(new FileReader("usnumbers.txt")));
    s.useLocale(Locale.US);


    while (s.hasNext()) {
    if (s.hasNextDouble()) {
    sum += s.nextDouble();
    } else {
    s.next();
    }
    }
    } finally {
    s.close();
    }

    System.out.println(sum);
    }
    }

Similar Threads

  1. Help me program a calculator in java
    By Kelvin Little in forum Software Development
    Replies: 4
    Last Post: 28-06-2010, 05:55 PM
  2. E72 calculator problem
    By Madhuchhanda in forum Portable Devices
    Replies: 4
    Last Post: 19-04-2010, 11:35 AM
  3. Replace calculator with Command Line Calculator
    By Eric B in forum Windows Software
    Replies: 3
    Last Post: 07-05-2009, 04:32 PM
  4. Calculator seems to be missing on my pc
    By Ayush in forum Operating Systems
    Replies: 4
    Last Post: 29-04-2009, 03:05 PM
  5. Java Swing Calculator Tutorial
    By hemanthjava in forum Software Development
    Replies: 0
    Last Post: 15-10-2006, 11:37 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,048,685.83135 seconds with 17 queries