Results 1 to 10 of 10
  1. #1
    Nano is offline Member
    Join Date
    Feb 2012
    Posts
    2
    Rep Power
    0

    Default Problem with simple addition and subtraction calculator

    I just started learning Java and im coming from C++. Can someone explain to me why i cant call Addition() and Subtraction() from main? Also, what can i do to clean this code up?(Make it shorter, easier to read, etc..)

    Java Code:
    import java.util.Scanner;
    import java.io.FileOutputStream;
    
    public class Calculator {
    	
    	
    	public void Addition(){
    		Scanner scan = new Scanner(System.in);
    		int one, two;
    		
    		System.out.println("Enter your first number: ");
    		one = scan.nextInt();
    		
    		System.out.println("Enter your second number: ");
    		two = scan.nextInt();
    		
    		System.out.println(one + " + " + two + " = " + (one+two));
    	}
    	
    	public void Subtraction(){
    		Scanner scan = new Scanner(System.in);
    		int one, two;
    		
    		System.out.println("Enter your first number: ");
    		one = scan.nextInt();
    		
    		System.out.println("Enter your second number: ");
    		two = scan.nextInt();
    		
    		System.out.println(one + " - " + two + " = " + (one-two));
    	}
    	
    	
    	public static void main(String[] args) {
    		int choice;
    		Scanner scan = new Scanner(System.in);
    		
    		System.out.println("CALCULATOR\n\n");
    		System.out.println("1)Addition\n 2)Subtraction");
    		choice = scan.nextInt();
    		
    		switch(choice){
    		case 1:
    			Addition();
    			break;
    		case 2:
    			Subtraction();
    			break;
    		default:
    			System.out.println("You did not enter a valid choice.");
    		}
    	}
    }
    Last edited by Nano; 02-05-2012 at 03:19 AM.

  2. #2
    Vampiricx3 is offline Member
    Join Date
    Feb 2012
    Posts
    31
    Rep Power
    0

    Default Re: Problem with simple addition and subtraction calculator

    The reason as to why you were getting errors is because, you were trying to call Addition and Subtraction from non-static methods, into your main method, which is static. Also I have done some editing to your code, to make it slightly more presentable. :) Good luck.
    Java Code:
    import java.util.*;
    import java.io.*;
      
    public class Calculator {
        public Calculator() {
            int choice;
            Scanner scan = new Scanner(System.in);
              
            System.out.println("JAVA CALCULATOR\n");
            System.out.println("    1)Addition\n    2)Subtraction");
            choice = scan.nextInt();
              
            switch(choice){
            case 1:
                Addition();
                break;
            case 2:
                Subtraction();
                break;
            default:
                System.out.println("You did not enter a valid choice.");
            }
        }
     
        public void Addition(){
            Scanner scan = new Scanner(System.in);
            int one, two;
     
            System.out.println("Addiction selected!\n\n");
              
            System.out.println("Enter your first number: ");
            one = scan.nextInt();
              
            System.out.println("Enter your second number: ");
            two = scan.nextInt();
            System.out.println("////////////////////////////");
    	System.out.println("Answer: ");
            System.out.println(one + " + " + two + " = " + (one+two));
        }
          
        public void Subtraction(){
            Scanner scan = new Scanner(System.in);
            int one, two;
     
            System.out.println("Subtraction Selected!\n\n");
              
            System.out.println("Enter your first number: "); 
    	one = scan.nextInt();
            System.out.println("Enter your second number: ");
            two = scan.nextInt();
            System.out.println("////////////////////////////");
    	System.out.println("Answer: ");
            System.out.println(one + " - " + two + " = " + (one-two));
        }
          
          
        public static void main(String[] args) {
           new Calculator();
        }
    }
    Last edited by Vampiricx3; 02-05-2012 at 03:48 AM.

  3. #3
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default Re: Problem with simple addition and subtraction calculator

    The problem isn;t calling them in your main the method. The problem is that you cannot call them inside a switch statement.

  4. #4
    Vampiricx3 is offline Member
    Join Date
    Feb 2012
    Posts
    31
    Rep Power
    0

    Default Re: Problem with simple addition and subtraction calculator

    Quote Originally Posted by mwr1976 View Post
    The problem isn;t calling them in your main the method. The problem is that you cannot call them inside a switch statement.
    By calling them in the switch statement, inside the Calculator() constructor instead of the main class, it works perfectly fine. Of course you can call them inside a switch statement. Also, if you actually went ahead and copied OP's source code, you'd notice the errors.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,415
    Rep Power
    20

    Default Re: Problem with simple addition and subtraction calculator

    Note that class names should start with an uppercase letter, method names with a lowercase letter. Read about this and more in Code Conventions for the Java Programming Language: Contents

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: Problem with simple addition and subtraction calculator

    Quote Originally Posted by mwr1976 View Post
    The problem isn;t calling them in your main the method. The problem is that you cannot call them inside a switch statement.
    Unless you add the following comment line next to it:"

    // pretty please with sugar on top?

    kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Nano is offline Member
    Join Date
    Feb 2012
    Posts
    2
    Rep Power
    0

    Default Re: Problem with simple addition and subtraction calculator

    THank you everyone! I had it working but i wanted to make the code better so i changed it. Now it doesnt work lol. The problem is that it cant read the operation. It always goes to make last else statement. Can anyone tell me what is wrong?

    Java Code:
    import java.util.Scanner;
    import java.io.FileOutputStream;
    
    public class Calculator {
    	public Calculator(){
    		int choice;
    		int one, two;
    		String sign;
    		Scanner scan = new Scanner(System.in);
    		
    		System.out.println("CALCULATOR\nPress 1 to use.\nPress 0 to exit.");
    		choice = scan.nextInt();
    		
    		while(choice != 0){
    			System.out.println("\n\nFirst number: ");
    			one = scan.nextInt();
    			System.out.println("Operation:");
    			sign = scan.next();
    			System.out.println("Second number: ");
    			two = scan.nextInt();
    			
    			if(sign == "+"){
    				System.out.println(one + " + " + two + " = " + (one+two));
    			}
    			else if(sign == "-"){
    				System.out.println(one + " - " + two + " = " + (one-two));
    			}
    			else if(sign == "*"){
    				System.out.println(one + " * " + two + " = " + (one*two));
    			}
    			else if(sign == "/"){
    				System.out.println(one + " / " + two + " = " + (one/two));
    			}
    			else
    				System.out.println("You did not enter a valid operation.");
    	
    		}
    	}
    	
    	public static void main(String[] args) {
    		new Calculator();
    	}
    }

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: Problem with simple addition and subtraction calculator

    Don't compare Strings for equality with the == operator. Use the equals( ... ) method instead.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Vampiricx3 is offline Member
    Join Date
    Feb 2012
    Posts
    31
    Rep Power
    0

    Default Re: Problem with simple addition and subtraction calculator

    Exactly as Jos said, using .equals(""); is a lot more secure in your code then just using ==
    If I have helped you, + rep

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,415
    Rep Power
    20

    Default Re: Problem with simple addition and subtraction calculator

    Quote Originally Posted by Vampiricx3 View Post
    Exactly as Jos said, using .equals(""); is a lot more secure in your code then just using ==
    Nothing to do with security. The two do different comparisons.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. [Question]Simple Calculator problem?
    By AlleDingeAndern in forum New To Java
    Replies: 4
    Last Post: 02-02-2011, 02:19 AM
  2. Simple Calculator
    By marksey07 in forum New To Java
    Replies: 12
    Last Post: 01-09-2011, 06:30 AM
  3. Java Swing Simple Addition help
    By dbasenoob in forum New To Java
    Replies: 4
    Last Post: 04-27-2010, 01:26 PM
  4. Simple Calculator Display Problem :(
    By jimbob in forum Java Applets
    Replies: 4
    Last Post: 07-18-2009, 04:13 AM
  5. Simple Addition Program Outputting Wrong Value
    By carlodelmundo in forum New To Java
    Replies: 4
    Last Post: 08-05-2008, 03:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •