3 Replies - 433 Views - Last Post: 26 May 2010 - 07:58 AM Rate Topic: -----

#1 Elwood 101  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 110
  • Joined: 19-May 09

Text is not separating into new txt files

Posted 26 May 2010 - 07:00 AM

Okay,
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

Is This A Good Question/Topic? 0
  • +

Replies To: Text is not separating into new txt files

#2 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 766
  • View blog
  • Posts: 2,916
  • Joined: 12-May 09

Re: Text is not separating into new txt files

Posted 26 May 2010 - 07:24 AM

The line where you do the population test will always be false, because you assign the population:

   int population = 0;
   //PARSE HERE
   if (population > 49999)



You'll need to parse the population from the line before you can do the test.
Was This Post Helpful? 0
  • +
  • -

#3 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Text is not separating into new txt files

Posted 26 May 2010 - 07:26 AM

This is why:

...
int population = 0;
                
if (population > 49999)
...



You are not parsing the text lines to retrieve the population
number'/

I would suggest splitting the line read from the file into a string array. Given
the file format the (size of the string array - 1) will be the index into the string array of the number.

See http://java.sun.com/....lang.String%29
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2117
  • View blog
  • Posts: 8,818
  • Joined: 20-September 08

Re: Text is not separating into new txt files

Posted 26 May 2010 - 07:58 AM

Quote

The text file to separate


That won't work - it's not properly delimited. For instance, look closely at the first and last lines and look at the difference in each
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1