• Support PF! Buy your school textbooks, materials and every day products Here!

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

  • Comp Sci
  • Thread starter iamjon.smith
  • Start date
  • #26
33,070
4,771
Yes.
 
  • #27
78
1
Do you control the formatting input of the file? If yes, I would consider another way to separate the city from the state. What happen when you come across "New York City" in "New York?" Maybe use a comma or tab to separate them. But if that is never going to happen, then ignore me. :)
 
  • #28
33,070
4,771
Do you control the formatting input of the file? If yes, I would consider another way to separate the city from the state. What happen when you come across "New York City" in "New York?" Maybe use a comma or tab to separate them. But if that is never going to happen, then ignore me. :)
That's a good point. There are quite a few cities whose names are two words: St. Louis, Kansas City, Rapid City, Des Moines, and so on.
 
  • #29
117
3
I realized the merit of what Mark44 and sourlemon said and tried to use a tab delimiter with the scanner class, but now I my program throws an exception error:

Error writing to file.

I think I may have the delimiter misplaced or something. Here is where I changed the code:

Code:
try{

        // Create new file output streams
        bOut = new FileOutputStream("BigCities.txt", true); // output stream for large cities with boolean value for appending to file
        sOut = new FileOutputStream("SmallCities.txt" , true); // output stream for small cities with boolean value for appending to file

        // Connect print streams to the output stream
        p1 = new PrintStream( bOut );
        p2 = new PrintStream( sOut );

        
	scanner = new Scanner(new File("population.txt"));
        
        while (scanner.hasNextLine())
            { //Checks if there's any more lines
                scanner.useDelimiter("/t");  // delimiter here to read tabbed input
                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
             
                cityRecord.setCity( city);
                cityRecord.setState( state );
                cityRecord.setPopulation ( population );
               

                if(population >= 50000)
                {
                    p1.println(city + " " + state + " " + population);
                }
                else
                {

                    p2.println(city + " " + state + " " + population);
                }
         }
                p1.close();
                p2.close();
      }
     catch (Exception e)
         {
                        System.err.println ("Error writing to file");
         }
    }
If it makes a difference, I just deleted the space between city & state, and state & population and hit the tab key in the appropriate places in the population.txt file.
 
  • #30
33,070
4,771
The tab character is '\t', so the string you pass in useDelimiter should be "\t".
 
  • #31
117
3
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.
 
  • #32
33,070
4,771
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?
 

Related Threads for: Java Program Input from file, output to file(s)

Replies
27
Views
21K
Replies
0
Views
1K
  • Last Post
Replies
6
Views
2K
Replies
1
Views
1K
Replies
3
Views
557
Replies
6
Views
1K
Replies
15
Views
675
Replies
6
Views
2K
Top