75 pts.
 Errors printing Java code
I tried to create an upperclass code, but it seem like it doesn't want to print out. Here is my code:
 import java.io.*; import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; public class upperCase { public static void main(String[]args) { Scanner in = null; PrintWriter out = null; try { File file = new File("Chapter9.txt"); in = new Scanner(file); out = new PrintWriter("output.txt"); } catch (FileNotFoundException ex) { System.out.println(ex.getMessage()); System.out.println("in" + System.getProperty("user.dir")); System.exit(1); } while(in.hasNextLine()) { String record = in.nextLine(); if (record.indexOf("bwbecker") >= 0) { String upper = record.toUpperCase(); System.out.println(upper); System.out.println("output.txt"); } } //Close the file in.close(); out.close(); } } 
Can anyone help me?? Thank You!

Software/Hardware used:
ASKED: November 18, 2010  12:48 PM
UPDATED: November 22, 2010  3:06 PM

Answer Wiki:
You are successfully creating your PrintWriter, which is named 'out', but when you do this: <pre>System.out.println(upper);</pre> Your are printing to the console not to your print writer. The above line should be: <pre>out.println(upper);</pre> Any content that was previously in the file is being erased because you are opening it for writing (but you are not writing anything to it). -CarlosDL
Last Wiki Answer Submitted:  November 18, 2010  11:52 pm  by  carlosdl   63,535 pts.
All Answer Wiki Contributors:  carlosdl   63,535 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


 

Sorry it got error:

import java.io.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

public class upperCase {

    public static void main(String[]args)
    {
    	Scanner in = null;
    	PrintWriter out = null;
    	try
    	{
    		File file = new File("Chapter9.txt");
    		in = new Scanner(file);
    		out = new PrintWriter("output.txt");
    	}		
    	catch (FileNotFoundException ex)
    	{
    		System.out.println(ex.getMessage());
    		System.out.println("in" + System.getProperty("user.dir"));
    		System.exit(1);
    	}
    	
    	while(in.hasNextLine())
    	{
    		String record = in.nextLine();
    		if (record.indexOf("bwbecker") >= 0)
    		{
    			String upper = record.toUpperCase();
    			System.out.println(upper);
				System.out.println("output.txt");
    		}
    	}
    	
    	
    	
    	//Close the file
    	in.close();
    	out.close();
    }
    
    
}

Can anyone help me??
Thank You!

 75 pts.

 

it doesn’t want to print out

Try to convince it :-)

Are you getting errors ? what happens when you run the code ?

Please provide more details.

 63,535 pts.

 

There is no error at all. It just not printing anything. I tried to output it on the file with content but it turn into blank. All the content inside that file disappeared.

 75 pts.

 

I still can’t print out the output even though I tried to change from “

System.out.println(upper);

” to “

out.println(upper);

“. Did anyone have any other solution???

 75 pts.

 

The problem could be that you are not reading the input file correctly, and this condition is never met:

while(in.hasNextLine())

I would recommend testing the reading and writing to files separately. Make sure you know how to write to files first, and when you have a working example of writing to files using a PrintWriter then create another program to test the reading of files and the use of the Scanner class.

Here’s a small code you could use to test the PrintWriter class:

import java.io.FileWriter;
import java.io.PrintWriter;

public class printwriter {
  public static void main(String[] args) throws Exception {
    String[] linesToWrite = new String[] { "a", "b" };

    PrintWriter out = null;
    out = new PrintWriter("some_new_file.txt");
    for (int i = 0; i < linesToWrite.length; i++) {
      out.println(linesToWrite[i]);
    }
    out.flush();
    out.close();
  }
} 
 63,535 pts.