Results 1 to 4 of 4
  1. #1
    SCS17 is offline Member
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0

    Default question regarding exceptions..

    Hey,

    This might be a very simple question.
    I'm writing a simple program where the user is asked to enter an integer.

    System.out.println("Please enter an integer");
    a = scanner.nextInt();

    So i'm expecting him to enter an integer, but lets say that the user didnt enter an integer instead he entered a word or anything other than an int. I need to print him a message saying "Sorry... you have to enter an integer.." and then reprompt him again to enter an integer "Please enter an integer". Basically what should i do ???

    Should I write an "if statement" or should i try to catch the exception ???
    I hope somebody can clarify this point.. maybe with an example.

    Any help would be appreciated. Thanks alot.


    Heres the code:


    public class DemoProgram2{


    public static void main(String[] args){


    Scanner scanner;

    int x; //raw date information

    //create scanner for reading user keyboard input
    scanner = new Scanner(System.in);

    //read and int representation of the date from the keyboard
    System.out.print("Enter birthday as an DDMMYYYY int and [ENTER]: ");
    x = scanner.nextInt();

    //output the data collected
    System.out.println("Your Data");
    System.out.println("=========");
    System.out.println("x=" + x);
    System.out.println("Your Birthday");


    int day = (x - x % 1000000) / 1000000;
    int month = (x%1000000 - x % 10000)/10000;
    int year = x % 10000;
    System.out.println(day + "/" + month + "/" + year);

    }


    }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.util.Scanner;
    
    public class DP2 {
        public static void main(String[] args) {
            //create scanner for reading user keyboard input
            Scanner scanner = new Scanner(System.in);
            String line;
            do {
                //read and int representation of the date from the keyboard
                System.out.print("Enter birthday as an DDMMYYYY int and [ENTER]: ");
                line = scanner.nextLine();
            } while(!process(line));
            scanner.close();
        }
    
        private static boolean process(String s) {
            char[] chars = s.toCharArray();
            if(chars.length != 8)
                return false;
            for(int j = 0; j < chars.length; j++) {
                if(!Character.isDigit(chars[j]))
                    return false;
            }
            int x = Integer.parseInt(s);
            //output the data collected
            System.out.println("Your Data");
            System.out.println("=========");
            System.out.println("x=" + x);
            System.out.println("Your Birthday");
    
            int day = (x - x % 1000000) / 1000000;
            int month = (x%1000000 - x % 10000)/10000;
            int year = x % 10000;
            System.out.println(day + "/" + month + "/" + year);
            return true;
        }
    }

  3. #3
    SCS17 is offline Member
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0

    Default

    Thanks alot for your help.
    Do you know where should I add the

    System.out.println("Sorry, thats not acceptable.. enter an int");

    Thanks again.. really appreciate it.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    String line;
    boolean okay = true;
    do {
        if(!okay)
            System.out.println("Sorry, thats not acceptable.. enter an int");
        System.out.print("Enter birthday as an DDMMYYYY int and [ENTER]: ");
        line = scanner.nextLine();
    } while(!(okay = process(line)));

Similar Threads

  1. How to use chained exceptions
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-04-2008, 02:50 PM
  2. Runtime Exceptions
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-12-2007, 10:31 AM
  3. how to handle exceptions
    By paty in forum Advanced Java
    Replies: 2
    Last Post: 08-05-2007, 04:17 AM
  4. Question on Exceptions
    By yelllow4u in forum New To Java
    Replies: 6
    Last Post: 07-27-2007, 01:41 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
  •