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
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!
“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.
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.
I still can’t print out the output even though I tried to change from “
” to “
“. Did anyone have any other solution???
The problem could be that you are not reading the input file correctly, and this condition is never met:
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(); } }