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.
This compiles correctly but when you enter an integer the program doesnt run the next line. Until you enter another number.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());
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?




Reply With Quote