Cannot find symbol - variable
Posted
by
Ben Garside
on Stack Overflow
See other posts from Stack Overflow
or by Ben Garside
Published on 2013-10-31T09:45:08Z
Indexed on
2013/10/31
9:53 UTC
Read the original article
Hit count: 408
I'm new to Java, and I'm trying to get user input, store each line of input as a variable and then return each value so that it can be passed on somewhere else. When I try and compile it is telling me that it can't find the variable magnitude. I'm assuming it won't find the others either. I'm guessing that this is because I've declare the variables inside of the "try" but don't know how to get it so that the return statement accepts them. Code is as follows:
public Earthquake userAddEarthquake()
{
Scanner scanner = new Scanner(System.in);
try{
// convert the string read from the scanner into Integer type
System.out.println("Please Enter An Earthquake Magnitude: ");
Double magnitude = Double.parseDouble(scanner.nextLine());
System.out.println("Please Enter The Earthquakes Latitude Position: ");
scanner = new Scanner(System.in);
Double positionLatitude = Double.parseDouble(scanner.nextLine());
System.out.print("Please Enter The Earthquakes Longitude Position: ");
scanner = new Scanner(System.in);
Double positionLongitude = Double.parseDouble(scanner.nextLine());
System.out.print("Please Enter The Year That The Earthquake Occured: ");
scanner = new Scanner(System.in);
int year = Integer.parseInt(scanner.nextLine());
System.out.println("Magnitude = " + magnitude);
}
catch(NumberFormatException ne){
System.out.println("Invalid Input");
}
finally{
scanner.close();
}
return new Earthquake(magnitude, positionLatitude, positionLongitude, year);
}
© Stack Overflow or respective owner