Try catch loop problems :'(
I'm trying to get this code segment to repeat while the exception is caught. I don't see any errors in the code, neither does the compiler, except for the local boolean declared to set the "i" to true, it says it is never read. The program terminates before it is able to execute the account method.
Code:
//Import
import java.util.Scanner;
import java.util.InputMismatchException;
//End import
// ----------------------------------------------------------------------
//Begin WelcomeScreen Class
public class WelcomeScreen
{
Scanner input = new Scanner(System.in); //Create scanner object
DataStorage data = new DataStorage(); //Create DataStorage object for use
//Begin welcome
public void welcome()//Declare method
{
System.out.print("Welcome. Please enter your name\n\n"); //Display welcome message
String userName = input.next(); //Wait for input
data.setname(userName); //Value is set through setname's parameters
/*
* Removed for redundancy. See method "account"
* System.out.print("\nHello " + userName); //Display text + input
*/
}//End welcome
//Begin account method
public void account()//Declare method
{
boolean i = true;//Local variable declare for loop segment
try//Try segment containing loop
{
while (i = false)
{
System.out.printf("\n\n%s please enter your account number", data.getname());
int account = input.nextInt();//Input = int
data.setaccount(account);//Store input in DatStorage class
}//End while loop
} catch (InputMismatchException e)//Catch exception when input ! int
{
/*
* System.out.print(e);//Print exception for error checking
*/
i = false;//Variable checked by while statement to repeat loop after exception catch
}//End Catch
}//End account method
}//End WelcomeScreen Class