Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By jashburn

Thread: try/catch problem

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

    Default try/catch problem

    I want the program to give an error message after the first input if none of the required operators are inputted. But my try catch isn't working? Also when exit is inputted it still as operator the 2nd and 3rd questions ("Enter 1stnum & Enter 2ndnum" are both asked instead of exitting immediately. Is this more of a design issue or can it be overcome? Any comments would be welcomed.
    Java Code:
    import java.util.Scanner;
    import java.io.*;
    
    
    class Monday {
        public static void main(String[] args) {
            double n1,n2;
            boolean check = true;
    
            while(check) {
                System.out.println("Enter your operation: add, subtract, divide, multiply, or exit");
                 try{
                Scanner myScan = new Scanner(System.in);
                 
                String op = myScan.next();
               
                    System.out.println("Enter your 1st number");
                    n1 = myScan.nextDouble(); 
                    System.out.println("Enter your 2nd number");
                    n2 = myScan.nextDouble();
    
    
    
    
                    switch (op) {
                        case"add":
                        System.out.println("Your answer is "+ (n1 + n2));
                        break;
    
                        case"subtract":
                        System.out.println("Your answer is "+ (n1 - n2));
                        break;
    
                        case"divide":
                        System.out.println("Your answer is "+ (n1 / n2));
                        break;
    
                        case"multiply":
                        System.out.println("Your answer is "+ (n1 * n2)) ;
                        break;
    
                        case"exit":
                        System.out.println("Goodbye!");
                        break;
    
                    }
    
    
                    if ("exit".equals(op))
                    check = false;
    
                } catch (Exception e) {
                    System.out.println("This is my error");
    
                    
                }
            }
        }
    }
    Last edited by Eoghan_o; 04-29-2014 at 12:56 AM.

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

    Default Re: try/catch problem

    my try catch isn't working
    What exception is not being caught?

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    7

    Default Re: try/catch problem

    Please wrap your code with [code] tags so that it is easier to read, e.g.,
    [code]
    // your code here
    [/code]

    It preserves formatting and provides syntax highlighting, e.g.,
    Java Code:
    // your code here
    Quote Originally Posted by Eoghan_o View Post
    I want the program to give an error message after the first input if none of the required operators are inputted. But my try catch isn't working?
    Try-catch will only work if there is an exception thrown on the first place. In your case, myScan.next() might throw an exception, but the reasons for the exception are not what you think they are. Take a look at the API doc for Scanner.next(). Compare that with the API doc for Scanner.nextDouble() where the latter would throw an exception if the input is not a float.

    You might also want to review Lesson: Exceptions (The Java™ Tutorials > Essential Classes) to deepen your understanding on exceptions and exception handling.

    In order to display an error message right after the first input prompt you'll need to check the inputted value using if statements right after String op = myScan.next().

    Quote Originally Posted by Eoghan_o View Post
    Also when exit is inputted it still as operator the 2nd and 3rd questions ("Enter 1stnum & Enter 2ndnum" are both asked instead of exitting immediately. Is this more of a design issue or can it be overcome?
    Take a look again at how this is coded:
    Java Code:
    Scanner myScan = new Scanner(System.in);
    
    String op = myScan.next();
    
    System.out.println("Enter your 1st number");
    n1 = myScan.nextDouble();
    System.out.println("Enter your 2nd number");
    n2 = myScan.nextDouble();
    After String op = myScan.next(), the program proceeds to prompt for the 2 numbers. There is no code there to react to "exit". If you add the if statements I mentioned earlier, you can also check for "exit", and write the code to exit from the loop if "exit" is found.

    Do also review The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics) as your switch-case is missing the default case.
    AlexGraal likes this.

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

    Default Re: try/catch problem

    Thanks for the direction went back to the drawing board and redrafted all works well now. As a further question, any ideas on streamlining the next iteration?
    Last edited by Eoghan_o; 04-30-2014 at 08:56 AM.

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

    Default Re: try/catch problem

    deas on streamlining the current iteration?
    Validate the operation first. If it's valid, then get the number of operands it requires next.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Using try and catch problem
    By beginnerprog in forum New To Java
    Replies: 3
    Last Post: 03-23-2013, 01:58 AM
  2. try and catch Problem ...
    By LogicBrix in forum New To Java
    Replies: 2
    Last Post: 02-05-2012, 06:22 AM
  3. try and catch problem
    By Masamune1059 in forum New To Java
    Replies: 6
    Last Post: 01-08-2012, 04:47 AM
  4. try catch problem
    By g123456 in forum New To Java
    Replies: 8
    Last Post: 05-17-2010, 06:54 PM
  5. Problem JOptionPane try catch
    By jason99 in forum New To Java
    Replies: 1
    Last Post: 07-10-2009, 10:39 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
  •