Comp Sci Java Program Input from file, output to file(s)

Click For Summary
The discussion focuses on developing a Java application that reads city, state, and population data from a text file and categorizes cities into "BigCities.txt" and "SmallCities.txt" based on their population. The application uses a GUI for user interaction, allowing users to specify the input file and trigger the parsing process. Key code components include a `CityRecord` class to hold city data and a `ReadTextFile` class intended to read the input file line by line. The main challenge highlighted is correctly populating the `CityRecord` objects from the file and managing the output to the respective text files without overwriting existing data. The conversation emphasizes the need for a clear algorithm to achieve these tasks effectively.
  • #31
even switching to the proper slash "\t" as follows:

Code:
scanner = new Scanner(new File("population.txt"));
        scanner.useDelimiter("\t");
        while (scanner.hasNextLine())
            { //Checks if there's any more lines         
                city = scanner.next(); // reads to first space in line, sets the variable
                state = scanner.next(); // reads to second space in line, sets the variable
                population = scanner.nextInt(); // reads to the next space in line, sets variable
                //...more code

I still get the error writing to file.

I am still missing something.
 
Physics news on Phys.org
  • #32
I believe your problem has to do with the pattern you're using to separate the city, state, and population fields, and this pattern seems to have something to do with regular expressions. See Pattern class.

The useDelimiter() method you're using has an overload that takes a Pattern parameter, and it would seem that the two methods operate similarly.

Do you have a debugger to use? If so, see what city is set to in the line, city = scanner.next();
Same for state and population.

If you don't have a debugger or don't know how to use it, add some temporary lines of code to display the values of these three variables to the console.

What sort of write error do you get?
 

Similar threads

  • · Replies 27 ·
Replies
27
Views
23K
  • · Replies 9 ·
Replies
9
Views
11K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
10K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 7 ·
Replies
7
Views
4K