7 Replies - 320 Views - Last Post: 09 March 2013 - 05:43 PM Rate Topic: -----

#1 manbearpig11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-December 09

Help with Exceptions

Posted 09 March 2013 - 01:48 PM

The goal is to create a program that asks the user to enter 5 strings convert them to integers then throw an error if a string entered cannot be converted to an integer or is greater than 5 characters in length. Then print out the maximum value. I am supposed to use a separate class called StringTooLongException to check if a string is greater than 5 characters and end the program if this is true.
My program compiles and runs but I am not sure I am handling the StringTooLongException properly and it does not end my program when thrown, which I need it to do. Any help would be appreciated, Thanks.

 
//Main class 
import java.util.Scanner; 

public class MaxInt {

	public static void main(String[] args) { 
				
		String userString; 
		final int MAX = 5; 
		
		int[] userInts = new int[MAX];
		int tempInt = 0; 
		int maxValue = 0; 	 		
		
		Scanner scan = new Scanner(System.in); 
				
		System.out.println("Enter " + userInts.length + " integer values: "); 		
		
		for (int i = 0; i < userInts.length; i++) {
			
			userString = scan.next(); 	
			
			try { 
				tempInt = Integer.parseInt(userString);
			} catch (NumberFormatException nfe) {
				System.out.println("Please enter only integer values!");			
				if (i > 0)
					i--;  //decrements i so the loop continues until 5 successful ints have been entered.  
			}
			
			try { 
				if (userString.length() > 5 ) { //checks if string is greater than 5 characters
					throw new StringTooLongException("String must be less than 5 characters");
				}
			} catch(Exception e){
        		System.out.println(e.getMessage());
    		} 

			userInts[i] = tempInt;
			
			if (tempInt > maxValue) { 
				maxValue = tempInt; 
			}
		}
		
		System.out.print("You entered: " ); 
		 
		for (int q = 0; q < userInts.length; q++) { 
			System.out.print(userInts[q] + " "); 
		}
		
		System.out.println("\nThe maximum value entered was: " + maxValue); 
	}
}


 
public class StringTooLongException extends Exception {

    public StringTooLongException(String message) {
    		super(message); 		  
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Help with Exceptions

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Help with Exceptions

Posted 09 March 2013 - 01:50 PM

You are throwing your exception from within a try/catch block. So when the exception is thrown execution just moves to the catch block and it deals with your exception without ending your program.
Was This Post Helpful? 1
  • +
  • -

#3 manbearpig11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-December 09

Re: Help with Exceptions

Posted 09 March 2013 - 02:20 PM

View PostRyano121, on 09 March 2013 - 01:50 PM, said:

You are throwing your exception from within a try/catch block. So when the exception is thrown execution just moves to the catch block and it deals with your exception without ending your program.


How would I do this without a try catch block? If i remove it I get a compiler error saying "unreported exception StringTooLongException; must be caught or declared to be thrown." Sorry I am very new to exceptions/errors.
Was This Post Helpful? 0
  • +
  • -

#4 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Help with Exceptions

Posted 09 March 2013 - 02:24 PM

You use the 'throws' keyword to let the compiler know that you expect a kind of exception to be thrown in a method. It essentially means that the caller of the method has to take care of the exception instead of the the method itself.

In your case you have everything in main so it will crash your program instead which you said you wanted.

..void main throws StringTooLongException

Was This Post Helpful? 1
  • +
  • -

#5 manbearpig11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-December 09

Re: Help with Exceptions

Posted 09 March 2013 - 02:39 PM

View PostRyano121, on 09 March 2013 - 02:24 PM, said:

You use the 'throws' keyword to let the compiler know that you expect a kind of exception to be thrown in a method. It essentially means that the caller of the method has to take care of the exception instead of the the method itself.

In your case you have everything in main so it will crash your program instead which you said you wanted.

..void main throws StringTooLongException


Ah pretty simple fix. Thank you!
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Help with Exceptions

Posted 09 March 2013 - 03:14 PM

this is false
          if (i > 0)  
               i--;  //decrements i so the loop continues until 5 successful ints have been entered.    



it has to been done in all cases. If i == 0 you want to do i-- so i will be equal to -1
Later on the i++ will be done and i will be back at 0 what you want
Was This Post Helpful? 1
  • +
  • -

#7 manbearpig11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-December 09

Re: Help with Exceptions

Posted 09 March 2013 - 03:54 PM

View Postpbl, on 09 March 2013 - 03:14 PM, said:

this is false
          if (i > 0)  
               i--;  //decrements i so the loop continues until 5 successful ints have been entered.    



it has to been done in all cases. If i == 0 you want to do i-- so i will be equal to -1
Later on the i++ will be done and i will be back at 0 what you want

Hmm I didn't notice that thank you. I changed some things around and this seems to work in all cases.
 
do { 
			i++;
			userString = scan.next(); 
						
			if (userString.length() > 5)	{
				throw new StringTooLongException("***String must be less than 5 characters***"); 
			} 
			
			try { 
				tempInt = Integer.parseInt(userString);
				finalInts[i-1] = tempInt; //creates array of accepted integer values
			} catch (NumberFormatException nfe) {
				System.out.println("Please enter only integer values!");			
				if (i > 0)
					i--;  //decrements i so the loop continues until 5 successful ints have been stored.  
			}	
		} while (i < 5); 


Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Help with Exceptions

Posted 09 March 2013 - 05:43 PM

if you use a while() rather than a for.. it is easy simply increment i when required no need at all to systematically increment it and to decrement it later if there is an error
... and your test on i > 0 is still invalid anyhow

do {

     .. input number
     try {
         ....
         i++;  // number is valid so increment i
     }
     catch(Exception...  {
         display error//// do not increment i
     }

     while(condition based on i)


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1