Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: I'm starting on java development. What is wrong on this calculator?

  1. #1
    Junior Member
    Join Date
    Oct 2020
    Location
    GB
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question I'm starting on java development. What is wrong on this calculator?

    I'm trying to make a calculator as my first program java program and something is wrong. When the calculator asks me to do other operation and I choose "Yes" or "No", then it asks me 2 times before doing anything, ignoring the first input, any idea of why?
    Also, it's a bit too much complicated for what it does but I don't know how to fix that mess. If you have some tips for simplifiying this, I would love to read them.

    [SOLUTION]
    Lines 27, 36, 45, 55 and 68 are calls to the Oper method and the following line to each of those (28, 37, 46, 56 and 69) are if statements with that call in the condition.
    I just deleted those lines and leave only the if statement and now it works as I wanted
    [/SOLUTION]

    import java.util.Scanner;
    public class Calculator{
    	public static void main(String[] args){
    		long n1, n2, res, rem;
    		char op = 'a';
    		boolean otOp = true;
    		Scanner sc1 = new Scanner(System.in);
    		while(otOp == true){
    			boolean valid1 = false;
    			System.out.println("insert the first number:");
    			n1 = sc1.nextInt();
    			while(valid1 == false){
    				System.out.println("insert operator:");
    				System.out.println("(+, -, :, X o ^)");
    				op = sc1.next().charAt(0);
    				if(op != '+' && op != '-' && op != ':' && op != 'X' && op != 'x' && op != '^'){
    					Error();
    				} else {
    					valid1 = true;
    				}
    			}
    			System.out.println("insert the second number:");
    			n2 = sc1.nextInt();
    			switch(op){
    				case '+':
    					res = n1 + n2;
    					Oper(" addition ", res, otOp, n1, n2); //ERROR IS HERE
    					if(Oper(" addition ", res, otOp, n1, n2) == true){ //In this line, the condition has the same call of the previous line
    						otOp = true;
    					} else{
    						otOp = false;
    					}
    				break;
    				case '-':
    					res = n1 - n2;
    					Oper(" substraction ", res, otOp, n1, n2); //same here
    					if(Oper(" substraction ", res, otOp, n1, n2) == true){ //and here
    						otOp = true;
    					} else{
    						otOp = false;
    					}
    				break;
    				case ':':
    					res = n1 / n2;
    					Oper(" division ", res, otOp, n1, n2); //^^
    					if(Oper(" division ", res, otOp, n1, n2) == true){ //==
    						otOp = true;
    					} else{
    						otOp = false;
    					}
    				break;
    				case 'X':
    				case 'x':
    					res = n1 * n2;
    					Oper(" multiplication ", res, otOp, n1, n2); //^^
    					if(Oper(" multiplication ", res, otOp, n1, n2) == true){ //==
    						otOp = true;
    					} else{
    						otOp = false;
    					}
    				break;
    				case '^':
    					long pot = 1;
    						for (int i = 1; i <= n2; i++){
    							pot *= n1;
    						}
    					res = pot;
    					Oper(" power ", res, otOp, n1, n2); //^^
    					if(Oper(" power ", res, otOp, n1, n2) == true){ //==
    						otOp = true;
    					} else{
    						otOp = false;
    					}
    				break;
    			}
    		}
    	}
    	static void Error(){
    		System.out.println("ERROR, invalid option");
    	}
    	static boolean Oper(String oper, long res, boolean otOp, long n1, long n2){
    		boolean valid2 = false;
    		Scanner sc2 = new Scanner(System.in);
    		System.out.println("the result of the" + oper + "is " + res);
    		if(oper == " division "){
    			System.out.println("reminder: " + (n1 % n2));
    		}
    		System.out.println("any other operation? (y/Y = Yes, n/N = No)");
    		char in = sc2.next().charAt(0);
    		while(valid2 == false){
    			if(in == 'y' || in == 'Y'){
    				otOp = true;
    				valid2 = true;
    			} else if(in == 'n' || in == 'N'){
    				otOp = false;
    				valid2 = true;
    			} else{
    				Error();
    			}
    		} return otOp;
    	}
    }
    Last edited by pansito; October 13th, 2020 at 08:06 AM. Reason: The error was solved

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I'm starting on java development. What is wrong on this calculator?

    Please copy the full contents of the console from when you test the program and paste it here so we can run the test exactly the same way you have.

    One problem I see is the code is using 2 Scanner objects. It is better if only one Scanner is used.

    How are you trying to debug the code to see why it is not doing what you want? Add some print statements that print the values of variables at all the places where the code is making the wrong decisions based on some variables' contents. The print outs will show you what the computer is seeing when the code is executed and help you see what is wrong in the code.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    pansito (October 13th, 2020)

  4. #3
    Junior Member
    Join Date
    Oct 2020
    Location
    GB
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I'm starting on java development. What is wrong on this calculator?

    I think you asked for something like this:

    java Calculator

    out: insert the first number:

    in: (any number in the long range)

    out: insert operator:
    out: (+, -, :, X o ^)

    in: (any operator)

    out: insert the second number:

    in: (any number in the long range)

    out: "the result of the" + operation + "is " + result
    out: any other operation? (y/Y = Yes, n/N = No)

    in: Y

    out: "the result of the" + operation + "is " + result
    out: any other operation? (y/Y = Yes, n/N = No)

    in: Y

    -asks for numbers and operator again-

    out: "the result of the" + operation + "is " + result
    out: any other operation? (y/Y = Yes, n/N = No)

    in: N

    out: "the result of the" + operation + "is " + result
    out: any other operation? (y/Y = Yes, n/N = No)

    in: Y

    out: "the result of the" + operation + "is " + result
    out: any other operation? (y/Y = Yes, n/N = No)

    -asks for numbers and operator again-

    out: "the result of the" + operation + "is " + result
    out: any other operation? (y/Y = Yes, n/N = No)

    in: N

    out: "the result of the" + operation + "is " + result
    out: any other operation? (y/Y = Yes, n/N = No)

    in: N

    out: "the result of the" + operation + "is " + result
    out: any other operation? (y/Y = Yes, n/N = No)

    -program stops-

    [...]

    out: "the result of the" + operation + "is " + result
    out: any other operation? (y/Y = Yes, n/N = No)

    in: Y

    out: "the result of the" + operation + "is " + result
    out: any other operation? (y/Y = Yes, n/N = No)

    in: N

    out: "the result of the" + operation + "is " + result
    out: any other operation? (y/Y = Yes, n/N = No)

    -program stops-

    If I choose anything, the program ignores my first input and asks me one more time before closing the program or printing "insert the first number:" again.

    I tried printing the variables of the program but it just works as it should work.
    Last edited by pansito; October 13th, 2020 at 07:48 AM.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I'm starting on java development. What is wrong on this calculator?

    Please copy the exact text of the console from when you run the program.
    The last post does not look like the exact text from when you executed the program. The exact text is important to allow anyone to follow what the code did when it executed. Your edits to the output muddy the waters for seeing what exactly the program did.

    Where in the code is this message printed:
    out: any other operation? (y/Y = Yes, n/N = No)
    For it to be printed 2 times means the method that the print statement is in was called 2 times. Why is that method called 2 times?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Java Enterprise Development(J2EE) or Java Android Development?
    By gokhan47 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 1st, 2013, 08:27 PM
  2. Starting Android Application Development
    By MikeSki3 in forum Android Development
    Replies: 8
    Last Post: May 3rd, 2013, 07:54 AM
  3. Starting with Java
    By corbs in forum Java Theory & Questions
    Replies: 4
    Last Post: March 29th, 2013, 01:20 PM
  4. Need help starting on Java
    By dfixx in forum Member Introductions
    Replies: 2
    Last Post: November 18th, 2012, 04:12 AM
  5. Starting BlackBerry development
    By AttilioCarotenuto in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: July 29th, 2011, 12:11 PM