- #1
camel-man
- 76
- 0
Code:
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
public class CountryPopulationReverse{
public static void main(String args[])throws FileNotFoundException {
//Variable declarations
Scanner in = new Scanner(System.in);
Scanner FileIn;
FileReader inputFile;
String population, country;
int checkNum;
PrintWriter output;
boolean good = true;
try
{
System.out.print("Input file: ");
String inputName = in.nextLine();
inputFile = new FileReader(inputName);
FileIn = new Scanner(inputFile);
System.out.print("Output file: ");
String outputName = in.nextLine();
output = new PrintWriter(outputName);
}catch(FileNotFoundException e){ System.out.println("File not found.");}
while(good)
{
try
{
/*
System.out.print("Input file: ");
String inputName = in.nextLine();
inputFile = new FileReader(inputName);
FileIn = new Scanner(inputFile);
System.out.print("Output file: ");
String outputName = in.nextLine();
output = new PrintWriter(outputName);
*/
while(FileIn.hasNextLine())
{
while(!FileIn.hasNextInt())
{
country = FileIn.next();
output.print(country);
}
population = FileIn.next();
checkNum = Integer.parseInt(population);
output.println(checkNum);
System.out.println(checkNum);
if(checkNum<=5)
throw new BadDataException("Population (" + checkNum + ") is too low.");
if(checkNum>2000000000)
throw new BadDataException("Population (" + checkNum + ") is too high.");
}
output.close();
good=false;
} catch (FileNotFoundException e){ System.out.println("File not found.");}
catch (BadDataException e) { System.out.println("Bad Data: " + e.getMessage());}
catch (NumberFormatException e) { System.out.println("Number Format Exception found.");}
}//End while loop
}
}
Code:
CountryPopulationReverse.java:46: error: variable FileIn might not have been initialized
while(FileIn.hasNextLine())
Not understanding java right now Why can't I declare these variables outside the try block?!?? I hate java I would rather do C it is so much more lenient.
Only time this program compiles is when I remove the innner comments of the try block and delete the top portion.