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

Click For Summary
SUMMARY

The discussion focuses on developing a Java application that reads city, state, and population data from a text file named "population.txt". The application categorizes cities into two output files: "BigCities.txt" for populations of 50,000 or more, and "SmallCities.txt" for populations below that threshold. The code provided includes a GUI built with Java Swing and utilizes classes such as CityRecord for data management and ReadTextFile for file reading. The main challenge highlighted is correctly populating the ArrayList with city data from the input file.

PREREQUISITES
  • Java programming knowledge, specifically Java Swing for GUI development.
  • Understanding of file I/O operations in Java, including FileInputStream and BufferedInputStream.
  • Familiarity with Java collections, particularly ArrayList.
  • Basic knowledge of object-oriented programming concepts, especially class creation and object instantiation.
NEXT STEPS
  • Implement file reading logic in the ReadTextFile class to populate the ArrayList with CityRecord objects.
  • Develop a method to categorize cities based on population and write to the respective output files.
  • Explore Java exception handling to manage potential file I/O errors effectively.
  • Enhance the GUI to provide user feedback on the processing status of the input file.
USEFUL FOR

Java developers, especially those interested in file handling and GUI applications, as well as students learning about object-oriented programming and data management in Java.

  • #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
24K
  • · 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