Handling Exceptions Help
Results 1 to 3 of 3

Thread: Handling Exceptions Help

  1. #1
    Banned
    Join Date
    Aug 2003
    Posts
    130

    Handling Exceptions Help

    Im writing a java application for my biology class on epidemiology(study of viruses; not computer)
    When i try to assign a variable before, in and after a try-catch block either i get compile errors or the application doesnt run correctly.

    Code:
    int virusChoice;
    
    System.out.print("\n(1)VirusName \t(2)VirusName \nChoice:");
    			
    			try 
    			{
    				Integer.parseInt(input.readLine());
    			}
    			catch(NumberFormatException e)
    			{
    				System.out.println("Incorrect Choice");
    			}
    			virusChoice = Integer.parseInt(input.readLine());
    This compiles correctly but when you enter an integer the program doesnt run the next line. Until you enter another number.
    When i put assignment after the print.
    System.out.print("\n(1)VirusName \t(2)VirusName \nChoice:");
    virusChoice = Integer.parseInt(input.readLine());
    try(...)
    If you enter a string the interpreter displays an exception.
    When i put the assignment in the try block.
    try
    {
    virusChoice = Integer.parseInt(input.readLine());
    }
    The compiler gives me the error:
    VirusSpreadRate.java:81: variable virusChoice might not have been initialized
    switch(virusChoice)
    because i have a switch decision later on in the program.
    Any answers to this problem, Its due tomorrow?

  2. #2
    Elite Hacker
    Join Date
    Mar 2003
    Posts
    1,407
    I guess it already had to be turned in, but ...
    First of all, it takes the number on the second input, cause you ask twice for input and only on the second time do you assign it to a variable. I think a while statement would have been better.
    Code:
    int virusChoice = Integer.parseInt(input.readLine());
    
    while(virusChoice > 3 || virusChoice < 1){  //put however many choices you have in place of 3
        System.out.println("invalid specification, please try again");
        virusChoice = Integer.parseInt(input.readLine());
    }
    switch(virusChoice){
    ...
    edit I guess you would still have to do a try and catch, hrm. To get rid of that error, you should just be able to do int virusChoice = 0; instead of just int virusChoice;
    I think that would work. Hope you got it figured out before you turned it in.

  3. #3
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    As h3r3tic correctly said, the java compiler will generally not allow you to use a variable without first initialising it. Edited code would possibly be:

    Code:
    int virusChoice = 0;
    
    System.out.print("\n(1)VirusName \t(2)VirusName \nChoice:");
    			
    try 
    {
    	Integer.parseInt(input.readLine());
    }
    catch(NumberFormatException e)
    {
    	System.out.println("Incorrect Choice");
    }
    Also, I wouldn't use a try catch block to check for an incorrect choice (unless you wish to write a new exception class, which would be a waste). I would use a try catch block to check if a number was inputted, and to stop the java runtime from getting angry, then afterwards, I would use a while loop (like h3r3tic suggested) to repeatedly ask the user for input assuming their original input was "out of range".

    You were almost right...Just remember that when you create variables (of primitive types, anyhow), you should initialise them (either when you declare them, or in a constructor). Which objects, you might not wish to create them until you use them...

    ac

Posting Permissions

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