Errors printing Java code

75 pts.
Tags:
Java
Java applications
Java development
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!

Answer Wiki

Thanks. We'll let you know when a new response is added.

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

Discuss This Question: 5  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Kuranharuka
    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 pointsBadges:
    report
  • carlosdl
    "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.
    70,190 pointsBadges:
    report
  • Kuranharuka
    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 pointsBadges:
    report
  • Kuranharuka
    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 pointsBadges:
    report
  • carlosdl
    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();
      }
    } 
    70,190 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following