Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: Java Exception

  1. #1
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Java Exception

    Hello! For my university assignment I had to create a prog**ram that calculated the n**th Line of pa**scals tria**ngle (inputed by user) and display the cont**ents of that line, and I have done so...my problem now is that I would like to make my program a bit cooler by making it throw and expcetion if an int is not entered, but I dont know how to do it correctly, this is my working program (sorry for the ***s but i dont want classmates to google my work )
    Last edited by Darr2cool78; 11-26-2010 at 10:21 AM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    That explains the *'s >.<
    Exception handling in Java is very easy, just gets some getting used to. Scanner's nextInt call will throw a NoSuchElementException, IllegalStateException or InputMismatchException.
    What you want is not to toss an exception, rather to catch it. This is done quite simply with:
    PHP Code:
    int number 0;
    try
    {
        
    number sc.nextInt();
    }
    catch (
    Exception ex)
    {
        
    System.out.println("Invalid entry!");

    Combine that with a do/while loop and some flushing:
    PHP Code:
    boolean isGood false;
    int number;
    do
    {
        try
        {
            
    System.out.print("Enter Required Line Number: ");
            
    number sc.nextInt();
            
    isGood true;
        }
        catch (
    Exception ex)
        {
            
    System.out.println("Invalid entry!");
            
    sc.nextLine();
        }
    } while (!
    isGood);
    // Process it. 
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks very much for the help much appreciated, I was wondering how this code fits into my program, I guess i thought i knew how to place it but everytime i try i get errors or It just doesnt see the exception

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    It should replace most of your main.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #5
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    When i put it in like this it keeps telling me number is already declared in the main, damn im not the coder i thot i was i cant place this, im bad with exceptions
    Last edited by Darr2cool78; 11-26-2010 at 01:57 AM.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    That means you have an int number specified more than once. I moved my declaration above the do/while since there really isn't a need to keep instantiating it.
    Don't forget to declare your scanner, I'd also put that outside of the loop.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #7
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    That means you have an int number specified more than once. I moved my declaration above the do/while since there really isn't a need to keep instantiating it.
    Don't forget to declare your scanner, I'd also put that outside of the loop.
    Well I finallly got it working, Thanks so much for your help
    (hope u dont mind but i removed my original code incase classmates are on snooping as today is handin day)


  •  

    Posting Permissions

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