Results 1 to 4 of 4
Like Tree2Likes
  • 1 Post By jim829
  • 1 Post By JosAH

Thread: Handling errors

  1. #1
    Robben is offline Member
    Join Date
    Feb 2015
    Posts
    67
    Rep Power
    0

    Default Handling errors

    I am trying to code using error handling and I am a bit confused on how to go about doing it correctly.

    My code converts a string that has all numbers into an integer and the error handling should recognize that if it isn't a proper number and ask the user to try again or enter 'q' to quit.

    Do I place a throw new exception in the try block and put conditionals like if charAt(i) is some letter or a symbol then throw new exception? I am not sure how to do this properly?


    Java Code:
    import java.util.Scanner;
    
    public class practice {
    
        public static void main(String[] args) {
    
            Integer finalValue = null;
            boolean validValue = false;
            Scanner scan = new Scanner(System.in);
            int result = 0;
    
            while(!validValue) {
                try {
    
                    System.out.println("Enter the string you want to convert");
                    String input = scan.nextLine();
    
                    int result = 0;     
    
                    for (int i = 0; i < input.length(); i++) {
    
                        result = result * 10 + input.charAt(i) - '0';
                    }            
    
    
                    validValue = true;
                } catch(Exception e) {
                    
                }
            }
            System.out.println(result);
        }
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Handling errors

    How do you plan on throwing the exception? As far as I can tell your code will not generate an exception on its own. Even if the character is not numeric, you can still perform math on it. So you would need to validate the character yourself and then simply re-prompt for user input. Also, I don't see the point of throwing an exception which you are going to immediately catch and process. If you want to practice with exceptions, a better approach (imho) would be to declare a method which takes a string and returns an integer. The method could throw an exception if the string contained invalid characters.

    BTW, you are re-declaring result as an int. That will generate a compiler error.

    Regards,
    Jim
    Robben likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Robben is offline Member
    Join Date
    Feb 2015
    Posts
    67
    Rep Power
    0

    Default Re: Handling errors

    Thank you! What I done was the following, which does what I want it to do. Is there anything I can do to make it (for the lack of a better term) look more professional?

    Java Code:
    public class practice {
    
        public static void main(String[] args) {
    
            Integer finalValue = null;
            boolean validValue = false;
            Scanner scan = new Scanner(System.in);
            int result = 0;
    
            while(!validValue) {
                try {
    
                    System.out.println("Enter the string you want to convert");
                    String input = scan.nextLine();
    
                    if (input.equals("q")) {
                        System.exit(0);
                    }
    
                    for (int i = 0; i < input.length(); i++) {
                        if (input.charAt(i) < 48 || input.charAt(i) > 57) {
                            throw new Exception();
                        }
                    }                    
    
                    for (int i = 0; i < input.length(); i++) {
                        result = result * 10 + input.charAt(i) - '0';
                    } 
    
                    validValue = true;
    
                } catch(Exception e) {                
                    System.out.println("Not a correctly written whole number. Try again or enter q to quit.");               
                }
            }
    
            System.out.println(result);
        }
    }

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Handling errors

    Get rid of those magic numbers 48 and 57; use '0' and '9' instead; btw. you can read ints with a Scanner, there's no need to reinvent the wheel; (it throws Exceptions if anything is wrong, so you can catch them and act accordingly).

    kind regards,

    Jos
    Robben likes this.
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Handling input errors
    By navya4u2004 in forum New To Java
    Replies: 4
    Last Post: 07-30-2011, 09:11 PM
  2. Replies: 4
    Last Post: 01-04-2011, 12:36 AM
  3. Handling Errors IE: Wrong User Input
    By movsesinator in forum New To Java
    Replies: 4
    Last Post: 04-06-2010, 01:37 AM
  4. Replies: 3
    Last Post: 03-08-2009, 01:51 AM
  5. Handling SQL Errors and Warnings
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-12-2008, 09: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
  •