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 6 of 6

Thread: (Beginner's calculator) Additional calculation after first result...

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    8
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default (Beginner's calculator) Additional calculation after first result...

    Sorry for this crap disguised as a code. Just about finished learning loops...

    This is a simple calculator and I have to do one more calculation after the initial calculation (i.e. 120 + 23 = 123, - 33 = 90)

    import java.util.Scanner;
     
    public class Calc {
     
        public static void main(String[] args) {
     
        Scanner input = new Scanner (System.in);
     
        double first, sec, third, result;
     
        String symbol;   
     
     
        System.out.print("Enter the first value: ");
            first=input.nextDouble();    
        System.out.print("Enter your operator (+,-,*,/): ");
            symbol=input.next();
        System.out.print("Enter the second value: ");
            sec=input.nextDouble();
     
     Scanner input2 = new Scanner(System.in);
     
            switch(symbol){
             case "+":
                result = first + sec;
                System.out.println(result);
     
                String another;
                System.out.print ("Enter another operator (+,-,*,/): ");
                    another=input2.next();
                System.out.print(("Enter the third value: "));
                    third=input2.nextDouble();
     
                switch(another){
                    case"+":
                        System.out.println(result + third);
                        break;
                    case"-":
                        System.out.println(result-third);
                        break;
                    case "*":
                        System.out.println(result*third);
                        break;
                    case "/":
                        System.out.println(result/third);   
                        break;
                    default:
                        System.out.println("What the hell?");
                        break;
                }
     
             case "-":
                result = first - sec;
     
                System.out.println(result);
                    System.out.print ("Enter another operator (+,-,*,/): ");
                another=input2.next();
                System.out.print(("Enter the third value: "));
                    third=input2.nextDouble();
     
                switch(another){
                    case"+":
                        System.out.println(result+third);
                        break;
                    case"-":
                        System.out.println(result-third);
                        break;
                    case "*":
                        System.out.println(result*third);
                        break;
                    case "/":
                        System.out.println(result/third);   
                        break;
                    default:
                        System.out.println("What the hell?");
                        break;
                }
     
             case "*":
                result = first * sec;
                System.out.println(result);
                    System.out.print ("Enter another operator (+,-,*,/): ");
                another=input2.next();
                System.out.print(("Enter the third value: "));
                    third=input2.nextDouble();
     
                switch(another){
                    case"+":
                        System.out.println(result+third);
                        break;
                    case"-":
                        System.out.println(result-third);
                        break;
                    case "*":
                        System.out.println(result*third);
                        break;
                    case "/":
                        System.out.println(result/third);   
                        break;                
                    default:
                        System.out.println("What the hell?");
                        break;
                }
     
             case "/":
                result=first/sec;
                System.out.println(result);
                    System.out.print ("Enter another operator (+,-,*,/): ");
                another=input.next();
                System.out.print(("Enter the third value: "));
                    third=input.nextDouble();
     
                switch(another){
                    case"+":
                        System.out.println(result+third);
                        break;
                    case"-":
                        System.out.println(result-third);
                        break;
                    case "*":
                        System.out.println(result*third);
                        break;
                    case "/":
                        System.out.println(result/third);   
                        break;
                    default:
                        System.out.println("What the hell?");
                        break;
                }
             default:
                System.out.println("What the hell?");
                break;     
            }     
        }   
    }

    I'll never look at a calculator the same way again after this...wow.

    I can get the result, but:

    (a) displays a weird result after the answer after the correct second calculation (i.e. 12 + 5 = 17.0 - 2, 15.0 7.0 OR 4 / 2 = 2.0 * 8 = 16.0 What the hell?)

    (b) it doesn't break like it should after the second calculation. It doesn't end unless "what the hell" comes out.

    I'm thinking both are resulting from the same mistake; bracket problem (in switch(another)), but I can't point exactly where and how to fix it...

    A few other Q's:

    (1) Is there any way to put numeric operations (i.e. +,-) on the same line as my numbers? For example, make 2 + 3 runnable with an intended outcome instead of asking for 2, 3, then +?

    (2) How do I make this code better? I know using switch within a switch is 99.99% of the time a big No-No...but I just couldn't find an alternative to get the result at my current level of knowledge (toenail-deep).


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: (Beginner's calculator) Additional calculation after first result...

    A suggestion: Add the value of the variable that caused the default: case to be executed to the error message so you can see what the computer saw and understand what the code is doing.
    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:

    skw4712 (January 30th, 2013)

  4. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    8
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: (Beginner's calculator) Additional calculation after first result...

    It doesn't display any error message...It's a logic error...But I just can't seem to pinpoint where...

    --- Update ---

    Wow. Wow. I forgot to put breaka at the end of each cases under switch(symbol). Found the problem as I was writing comments from top-down to see what the problem was...

    New code:

    package calc;
     
    import java.util.Scanner;
     
    public class Calc {
     
        public static void main(String[] args) {
     
        Scanner input = new Scanner (System.in);
     
        double first, sec, third, result;
     
        String symbol;   
     
     
        System.out.print("Enter the first value: ");
            first=input.nextDouble(); //First value enterable
        System.out.print("Enter your operator (+,-,*,/): ");
            symbol=input.next(); //Operator enterable, will be using switch loop
        System.out.print("Enter the second value: ");
            sec=input.nextDouble(); //Second value enterable
     
        Scanner input2 = new Scanner(System.in); //input for under switch(symbol)
     
            switch(symbol){
             case "+": //when + is entered as an operator
                result = first + sec;
                System.out.println(result);
     
                String another;
                System.out.print ("Enter another operator (+,-,*,/): ");
                    another=input2.next(); //2nd set of operator
                System.out.print(("Enter the third value: "));
                    third=input2.nextDouble(); //Third value enterable
     
                switch(another){
                    case"+":
                        System.out.println(result + third); //Add the third value
                        break; 
                    case"-":
                        System.out.println(result-third);
                        break;
                    case "*":
                        System.out.println(result*third);
                        break;
                    case "/":
                        System.out.println(result/third);   
                        break;
                    default:
                        System.out.println("What the hell?");
                        break;
                }
                 break; //BREAK THE CASE "+"
     
             case "-":
                result = first - sec;
     
                System.out.println(result);
                    System.out.print ("Enter another operator (+,-,*,/): ");
                another=input2.next();
                System.out.print(("Enter the third value: "));
                    third=input2.nextDouble();
     
                switch(another){
                    case"+":
                        System.out.println(result+third);
                        break;
                    case"-":
                        System.out.println(result-third);
                        break;
                    case "*":
                        System.out.println(result*third);
                        break;
                    case "/":
                        System.out.println(result/third);   
                        break;
                    default:
                        System.out.println("What the hell?");
                        break;
                }
                 break;
     
             case "*":
                result = first * sec;
                System.out.println(result);
                    System.out.print ("Enter another operator (+,-,*,/): ");
                another=input2.next();
                System.out.print(("Enter the third value: "));
                    third=input2.nextDouble();
     
                switch(another){
                    case"+":
                        System.out.println(result+third);
                        break;
                    case"-":
                        System.out.println(result-third);
                        break;
                    case "*":
                        System.out.println(result*third);
                        break;
                    case "/":
                        System.out.println(result/third);   
                        break;                
                    default:
                        System.out.println("What the hell?");
                        break;
                }
                 break;
     
             case "/":
                result=first/sec;
                System.out.println(result);
                    System.out.print ("Enter another operator (+,-,*,/): ");
                another=input.next();
                System.out.print(("Enter the third value: "));
                    third=input.nextDouble();
     
                switch(another){
                    case"+":
                        System.out.println(result+third);
                        break;
                    case"-":
                        System.out.println(result-third);
                        break;
                    case "*":
                        System.out.println(result*third);
                        break;
                    case "/":
                        System.out.println(result/third);   
                        break;
                    default:
                        System.out.println("What the hell?");
                        break;
                }
                 break;
             default:
                System.out.println("What the hell?");
                break;     
            }
     
     
     
        }   
    }

    I still do want to find out 2 questions: 1. Is there a way to include numeric operator in the same line and still get the result? (ask in one line instead of having to separate values and an operator) 2. Is there a better alternative to my code (other loop, etc)?

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: (Beginner's calculator) Additional calculation after first result...

    Is there a way to include numeric operator in the same line and still get the result?
    Are you asking if the user can enter: 3.4 + 7 and then press Enter?
    Have you tried it?

    I see that the code still does NOT show the value of the input that caused the error message to be printed.
    If you don't understand my answer, don't ignore it, ask a question.

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

    skw4712 (January 30th, 2013)

  7. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    8
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: (Beginner's calculator) Additional calculation after first result...

    1st: Yes and yes. I have a problem enabling Java to recognize certain characters in a string (like capitalizing each word in a string, or math operators in this case). This whole substring (what we learned) thing baffles me...can't grasp the concept

    2nd: This is my output:

    Enter the first value:
    Enter the operator (+,-,*,/):
    Enter the second value:
    RESULT SHOWN
    Enter the operator (+,-,*,/):
    Enter the third value:
    RESULT SHOWN

    I got the result that I wanted...There was no error message. Thank you for so much for helping out, though!

  8. #6
    Junior Member
    Join Date
    Feb 2012
    Location
    Ireland
    Posts
    6
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: (Beginner's calculator) Additional calculation after first result...

    It seems like a lot of code to comlete your task alright. You could try a few else if statments to get the same results.

    if (symbol.equals("+")) {
      result = first + second;
    } else if (symbol.equals("-")) {
      result = first - second;
    } else if   etc ......
    System.out.println(result);
     
    if(another.equals("+")) {
      total = result + third;
    } else if (another.equals("-")) {
       total = result - third;
    } else if   etc .......
     
    System.out.println(first+" "+symbol+" "+second +" = "+result+" "+another+" "+third+" = "+total);

    wrote this on tablet so just showed you the bear bones, hope you can take something from this.
    give me shout if you have any questions.

  9. The Following User Says Thank You to dave_alan For This Useful Post:

    skw4712 (January 30th, 2013)

Similar Threads

  1. Help with beginner's calculator
    By CrashOverride in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 14th, 2012, 06:16 PM
  2. Calculator beginner tutorial(s)
    By Mr.Greedy in forum Java Theory & Questions
    Replies: 4
    Last Post: June 13th, 2012, 05:53 PM
  3. Need Beginner Calculator Program help!
    By theJastro in forum What's Wrong With My Code?
    Replies: 18
    Last Post: December 17th, 2011, 07:30 PM
  4. Beginner trying to write Java code, has issue w/ printing result and 2 decimals
    By flpanthers1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 5th, 2011, 11:11 AM
  5. I need calculation
    By Swiss518 in forum Loops & Control Statements
    Replies: 7
    Last Post: January 27th, 2011, 01:26 PM