The object here is to read a file..which this does...taking the population number and separating populations greater than 49999 into text files of big cities and small cities.
This runs but does NOT separate the populations.
It creates the bigCity.txt file and the SmallCity.txt file but only the small city txt file has text and it is ALL the cities
What am I missing?
How could I do it better?
import java.io.*;
import java.util.Scanner;
public class CensusTextScanner
{
public static void main(String[] args) throws FileNotFoundException
{
boolean BigCity = true;
boolean SmallCity = true;
Scanner scan = null;
String BigCities = "";
String SmallCities = "";
try
{
scan = new Scanner(new BufferedReader(new FileReader("Census.txt")));
while (scan.hasNextLine())
{
String thisLine = scan.nextLine();
int population = 0;
if (population > 49999)
BigCities += thisLine + "\n";
else
SmallCities += thisLine + "\n";
}
}
finally
{
scan.close();
}
File firstFile = new File("BigCities.txt");
File secondFile = new File("SmallCities.txt");
if (!firstFile.exists())
{
try
{
firstFile.createNewFile();
BigCity = false;
}
catch (IOException e)
{
System.out.println(e);
}
}
if (!secondFile.exists())
{
try
{
secondFile.createNewFile();
SmallCity = false;
}
catch (IOException e)
{
System.out.println(e);
}
}
try
{
byte[] readData = new byte[1024];
readData = BigCities.getBytes();
if (BigCity)
{
FileOutputStream fos = new FileOutputStream(firstFile, true);
fos.write(readData);
fos.close();
System.out.println("Your Data has been separated and Saved in 'BigCities.txt'");
}
else
{
FileOutputStream fos = new FileOutputStream(firstFile);
fos.write(readData);
fos.close();
System.out.println("Your Data has been separated and Saved in 'BigCities.txt'");
}
readData = SmallCities.getBytes();
if (SmallCity)
{
FileOutputStream fos = new FileOutputStream(secondFile, true);
fos.write(readData);
fos.close();
System.out.println("Your Data has been separated and Saved in 'SmallCities.txt'");
}
else
{
FileOutputStream fos = new FileOutputStream(secondFile);
fos.write(readData);
fos.close();
System.out.println("Your Data has been separated and Saved in 'SmallCities.txt'");
}
}catch (IOException e)
{
System.out.println(e);
}
}
}
The text file to separate
Chicago Illinois 47000 Indianappolis Indiana 78000 San Francisco California 87456 Cleveland Ohio 48789 Savannah Georgia 35098 New York New York 94234
Thanx in Advance!
Elwood

New Topic/Question
Reply



MultiQuote





|