Results 1 to 1 of 1
- 03-18-2009, 07:24 PM #1
Senior Member
- Join Date
- Nov 2008
- Posts
- 105
- Rep Power
- 0
inputting and writing a file backwards
This is the description
For this assignment, you are to write a multi-class application that reads in and processes a text file. Here's what your application should do. At a prompt from your program, the user should supply the name of a text file. Your program should then read in the contents of this file, then write the file out backwards, to a file with the name backwards.txt. For example, if the file you read in looks like this:
hello there
Dana
old pal!
Then backwards.txt, after processing, should look like this:
old pal!
Dana
hello there
Well I got it for the most part, everything in my code works
Main
EchoJava Code:import java.util.Scanner; import java.io.*; public class WriterDriver { /** * @param args the command line arguments */ public static void main(String[] args) throws IOException { String fileName; Scanner nameReader = new Scanner(System.in); System.out.println("Enter a file name"); fileName = nameReader.nextLine(); Scanner scan = new Scanner(new FileReader(fileName)); Writer d = new Writer(fileName); d.readLines(); scan.close(); } }
Java Code:import java.util.Scanner; import java.io.*; public class Echo{ String fileName; // external file name Scanner scan; // Scanner object for reading from external file public Echo(String f) throws IOException { fileName = f; scan = new Scanner(new FileReader(fileName)); } public void readLines(){ // reads lines, hands each to processLine while(scan.hasNext()){ processLine(scan.nextLine()); } scan.close(); } public void processLine(String line){ // does the real processing work System.out.println(line); } }
And here is where the work needs to be done
So I tried change that toJava Code:import java.util.Scanner; import java.io.*; import java.util.StringTokenizer; public class Writer extends Echo { PrintWriter writer = new PrintWriter("something.txt"); public Writer(String f) throws IOException { super(f); System.out.println(System.getProperty("user.dir")); } public void readLines(){ // reads lines, hands each to processLine super.readLines(); scan.close(); writer.close(); } public void processLine(String line){ // does the real processing work StringTokenizer st = new StringTokenizer(line); while (st.hasMoreTokens()) { //String[] result = fileName.split("\\s"); writer.print(st.nextToken()); writer.print(" "); } writer.println(""); } }Except in the file it prints the directory name liek C:\documents and settings\hello.txt a billion times. I thought the code would make each word backwards? But I'm totally confused anyone got any ideas?Java Code:import java.util.Scanner; import java.io.*; import java.util.StringTokenizer; public class Writer extends Echo { PrintWriter writer = new PrintWriter("something.txt"); public Writer(String f) throws IOException { super(f); System.out.println(System.getProperty("user.dir")); } public void readLines(){ // reads lines, hands each to processLine super.readLines(); scan.close(); writer.close(); } public void processLine(String line){ // does the real processing work StringTokenizer st = new StringTokenizer(line); while (st.hasMoreTokens()) { String[] result = fileName.split("\\s"); for (int x=0; x<result.length; x++) writer.print(result[result.length-x]); writer.println(""); //writer.print(st.nextToken()); //writer.print(" "); } writer.println(""); } }
Similar Threads
-
comparing inputting strings from Joptionpane and if statement
By phil128 in forum New To JavaReplies: 2Last Post: 12-06-2008, 06:54 PM -
Writing to DAT or TXT file
By hunterbdb in forum Advanced JavaReplies: 7Last Post: 10-12-2008, 02:50 PM -
swapping the contents of the file and writing to another file
By Ms.Ranjan in forum New To JavaReplies: 9Last Post: 07-10-2008, 04:52 PM -
Writing to a file (at the end)
By Java Tip in forum Java TipReplies: 0Last Post: 02-08-2008, 09:22 AM -
writing to a file
By bugger in forum New To JavaReplies: 1Last Post: 11-11-2007, 02:49 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks