Results 1 to 8 of 8
  1. #1
    Eoghan_o is offline Member
    Join Date
    Apr 2014
    Posts
    8
    Rep Power
    0

    Default snagged on 'if' statement

    I'm trying to draft an if statement so that if there is an error with the inital intput the program will skip the following input prompts and go straight to the default in the switch statement. Any ideas?

    Java Code:
    import java.util.Scanner;
    public class MeddleThursday {
        public static void main(String args[]) {
            double n1,n2, answer;
            boolean check = true;
    
            while(true) {
                System.out.print("Enter your operation: add, subtract, divide, multiply, or exit: ");
                Scanner myScan = new Scanner(System.in);
                String op = myScan.nextLine();
    
                if(op.equals("exit")) {
                    System.out.println("Goodbye! ");
                    System.exit(0);
                }
    
    
    
                System.out.print("Enter your 1st number: ");
                n1 = myScan.nextDouble();
    
                System.out.print("Enter your 2nd number: ");
                n2 = myScan.nextDouble();
    
                switch (op) {
                    case"add":
                    answer = add(n1, n2);
                    System.out.println("The result of: "+ n1 +" plus "+ n2 +" is "+ (answer) +"\n");
                    break;
    
                    case"subtract":
                    answer = subtract(n1, n2);
                    System.out.println("The result of: "+ n1 +" minus "+ n2 +" is "+ (answer) +"\n");
                    break;
    
                    case"divide":
                    answer = divide(n1,n2);
                    System.out.println("The result of: "+ n1 +" divided "+ n2 +" is "+ (answer) +"\n");
                    break;
    
                    case"multiply":
                    answer = multiply(n1,n2);
                    System.out.println("The result of: "+ n1 +" times "+ n2 +" is "+  (answer) +"\n") ;
                    break;
    
                    default:
                    System.out.println("Error!"+"\n");
                }
            }
        }
        public static double add (double n1,  double n2) {
            return n1 + n2;
        }
        public static double subtract (double n1, double n2) {
            return n1 - n2;
        }
        public static double multiply (double n1, double n2) {
            return n1 * n2;
        }
        public static double divide (double n1, double n2) {
            return n1 / n2;
        }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: snagged on 'if' statement

    if there is an error with the inital intput the program will skip the following input prompts and go straight to the default in the switch statement.
    Hard to mix an if statement with a switch statement.
    Can the two statements be separate?

    BTW The printed message in the default case should show the invalid op value.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Eoghan_o is offline Member
    Join Date
    Apr 2014
    Posts
    8
    Rep Power
    0

    Default Re: snagged on 'if' statement

    the default shows the error message, but this happens after the next next to prompts are printed. At this stage I think a redraft is in order.

  4. #4
    Eoghan_o is offline Member
    Join Date
    Apr 2014
    Posts
    8
    Rep Power
    0

    Default Re: snagged on 'if' statement

    Partially solution but now with a new problem.
    By adding the if statements I've locked the number inputs. So now when 2 numbers are inputted on the first loop, on further successive loops the operation performed is now the only change that can be made.

    Java Code:
    import java.util.Scanner;
    public class MeddleThursday {
        public static void main(String args[]) {
            double n1= 0;
            double n2= 0;
            double answer = 0;
            boolean check = true;
    
            while(true) {
                System.out.print("Enter your operation: add, subtract, divide, multiply, or exit: ");
                Scanner myScan = new Scanner(System.in);
                String op = myScan.nextLine();
    
                if(op.equals("exit")) {
                    System.out.println("Goodbye! ");
                    System.exit(0);
                }
    
                if(op.equals("add")) {
                    check = true;
                    }
                    if(op.equals("subtract")) {
                    check = true;
                    }
                    if(op.equals("multiply")) {
                    check = true;
                    }
                    if(op.equals("add")) {
                    check = true;
    
                    System.out.print("Enter your 1st number: ");
                    n1 = myScan.nextDouble();
    
                    System.out.print("Enter your 2nd number: ");
                    n2 = myScan.nextDouble();
                }
                switch (op) {
                    case"add":
                    answer = add(n1, n2);
                    System.out.println("The result of: "+ n1 +" plus "+ n2 +" is "+ (answer) +"\n");
                    break;
    
                    case"subtract":
                    answer = subtract(n1, n2);
                    System.out.println("The result of: "+ n1 +" minus "+ n2 +" is "+ (answer) +"\n");
                    break;
    
                    case"divide":
                    answer = divide(n1,n2);
                    System.out.println("The result of: "+ n1 +" divided "+ n2 +" is "+ (answer) +"\n");
                    break;
    
                    case"multiply":
                    answer = multiply(n1,n2);
                    System.out.println("The result of: "+ n1 +" times "+ n2 +" is "+  (answer) +"\n") ;
                    break;
    
                    default:
                    System.out.println("Error!"+"\n");
                }
            }
        }
        public static double add (double n1,  double n2) {
            return n1 + n2;
        }
        public static double subtract (double n1, double n2) {
            return n1 - n2;
        }
        public static double multiply (double n1, double n2) {
            return n1 * n2;
        }
        public static double divide (double n1, double n2) {
            return n1 / n2;
        }
    }
    Last edited by Norm; 05-02-2014 at 03:06 AM. Reason: \ changed to / in ending code tag

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: snagged on 'if' statement

    do all of the operators take two operands? If so, you should always get two values to use with any operator, not just the add. The 4 if statements could be merged into a single if statement with a compound condition that is true if op is one of the 4 operators.

    The formatting of the code makes it very hard to read and understand.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    Eoghan_o is offline Member
    Join Date
    Apr 2014
    Posts
    8
    Rep Power
    0

    Default Re: snagged on 'if' statement

    Quote Originally Posted by Norm View Post
    do all of the operators take two operands? If so, you should always get two values to use with any operator, not just the add. The 4 if statements could be merged into a single if statement with a compound condition that is true if op is one of the 4 operators.

    The formatting of the code makes it very hard to read and understand.
    Sorry about the formatting, most of it is intentional to try out as many basis features as possible. With each succesive loop I'd want to be able to input 2 new number inputs for whichever operation is chosen.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: snagged on 'if' statement

    I don't know what that has to do with properly formatting the code so it is readable.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    Eoghan_o is offline Member
    Join Date
    Apr 2014
    Posts
    8
    Rep Power
    0

    Default Re: snagged on 'if' statement

    I see what you meant, point taken.

Similar Threads

  1. Statement and prepared statement
    By suhaas.mohandos@gmail.com in forum JDBC
    Replies: 10
    Last Post: 01-30-2014, 02:44 PM
  2. Replies: 3
    Last Post: 01-30-2014, 09:21 AM
  3. Replies: 3
    Last Post: 11-19-2012, 09:12 PM
  4. Replies: 2
    Last Post: 03-26-2012, 05:27 PM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 05:45 PM

Posting Permissions

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