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

Click For Summary

Discussion Overview

The discussion revolves around developing a Java application that reads city, state, and population data from a text file and outputs the information to separate files based on population thresholds. The focus is on the implementation details of the program, including GUI components and file handling.

Discussion Character

  • Technical explanation
  • Exploratory
  • Homework-related

Main Points Raised

  • One participant describes the requirements for the application, specifying how to categorize cities based on their population and the expected file output.
  • The participant shares a code snippet for a GUI application using Java Swing, detailing the layout and components such as buttons and text fields.
  • There is mention of a method to handle file input and output, but the implementation details for reading from the file and writing to the output files are not fully provided.
  • Another participant introduces a separate class for reading text files, indicating an intention to read the population data line by line, but the implementation is incomplete.
  • Some participants express uncertainty about the necessity of using subclasses for the application, suggesting that the main class might suffice.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to implement the file reading and writing functionality, and there are differing opinions on the need for additional classes in the application.

Contextual Notes

The discussion includes incomplete code snippets and lacks full implementation details for file handling and data processing. There are also assumptions about the structure of the input file that may not be explicitly stated.

Who May Find This Useful

Readers interested in Java programming, GUI development, file handling in Java, or those working on similar homework assignments may find this discussion relevant.

  • #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