Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-25-2010, 11:06 PM   PM User | #1
Darr2cool78
New to the CF scene

 
Join Date: Nov 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Darr2cool78 is an unknown quantity at this point
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 09:21 AM..
Darr2cool78 is offline   Reply With Quote
Old 11-26-2010, 12:09 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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. 
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 11-26-2010, 12:21 AM   PM User | #3
Darr2cool78
New to the CF scene

 
Join Date: Nov 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Darr2cool78 is an unknown quantity at this point
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
Darr2cool78 is offline   Reply With Quote
Old 11-26-2010, 12:28 AM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
It should replace most of your main.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 11-26-2010, 12:39 AM   PM User | #5
Darr2cool78
New to the CF scene

 
Join Date: Nov 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Darr2cool78 is an unknown quantity at this point
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 12:57 AM..
Darr2cool78 is offline   Reply With Quote
Old 11-26-2010, 01:33 AM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 11-26-2010, 09:20 AM   PM User | #7
Darr2cool78
New to the CF scene

 
Join Date: Nov 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Darr2cool78 is an unknown quantity at this point
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)
Darr2cool78 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:48 PM.


Advertisement
Log in to turn off these ads.