
-----------------------------------
shookones
Sun Nov 20, 2005 1:01 pm

need help with this code!
-----------------------------------
ok this is the code i am trying to write

a multi-class application that reads in and processes a text file. Here's what the application should do. At a prompt from the program, the user should supply the name of a text file using a Scanner object. Your program should then read in the contents of this file, and 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


this is what i have so far. but dont know what to do next. please help


import java.util.Scanner;

import java.io.*;


public class ReverseDriver {
    public static void main(String[] args){
      String fileName;
      Scanner name = new Scanner(System.in);
      System.out.println("Enter a file name");
      fileName = name.nextLine();
      
      Reversal a = new Reversal(fileName);
      a.readLines();
      
      PrintWriter writer = new PrintWriter("backwards.txt");
      writer.println();
 
    }
}





import java.io.IOException;
import java.util.ArrayList;

public class Reversal extends Echo {
  public void processLine(String line) {
  }
  
}

-----------------------------------
shookones
Sun Nov 20, 2005 1:04 pm


-----------------------------------
im not sure what to make the processLine method do and im not sure how the PrintWriter works, like what code to write for it to write. someone please point me in the right direction. it would greatly be appreciated

-----------------------------------
shookones
Sun Nov 20, 2005 7:55 pm


-----------------------------------
anyone?

-----------------------------------
wtd
Sun Nov 20, 2005 7:58 pm


-----------------------------------
Please use code or syntax tags.

-----------------------------------
shookones
Sun Nov 20, 2005 8:59 pm


-----------------------------------
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));
  }

  // reads lines, hands each to processLine
  public void readLines(){
    while(scan.hasNext()){
      processLine(scan.nextLine());
    }
    scan.close();
  }

  // does the real processing work
  public void processLine(String line){
    System.out.println(line);
  }
}



import java.io.IOException;
import java.util.ArrayList;

public class Reversal extends Echo {
  String[] lines = new String[100];
  
  public Reversal(String f) throws IOException {
    super(f);
  }
  
  public void processLine(String line) {
    int i = 0;
    while(i < 99) {
      lines[i] = line;
    }
    for (int j = 99; j > lines.length - 1; j--) {
      System.out.println(lines[j]);
    }
    
  }
  
}


import java.util.Scanner;

import java.io.*;


public class ReverseDriver {
    public static void main(String[] args){
      String fileName;
      Scanner name = new Scanner(System.in);
      System.out.println("Enter a file name");
      fileName = name.nextLine();
      
      Reversal a = new Reversal(fileName);
      
      PrintWriter writer = new PrintWriter("backwards.txt");
      System.out.println(a.readLines());
      writer.close();
 
    }
}
        

-----------------------------------
wtd
Sun Nov 20, 2005 9:07 pm


-----------------------------------
Ok... let's analyze the problem first.  

You need to get a filename from the user.  That's reasonably simple.

You need to read in that file and output it, and you ned to do the same, but in reverse.

Well, in order to output it reverse you're going to need to have all of the lines before you can begin doing any output.  This means you have to store the lines from the file in something like:

ArrayList

-----------------------------------
shookones
Sun Nov 20, 2005 10:55 pm


-----------------------------------
yea store it in an array right? 
this is what i did below, is it correct?

import java.io.IOException;
import java.util.ArrayList;

public class Reversal extends Echo {
  String[] lines = new String[100];
 
  public Reversal(String f) throws IOException {
    super(f);
  }
 
  public void processLine(String line) {
    int i = 0;
    while(i < 99) {
      lines[i] = line;
    }
    for (int j = 99; j > lines.length - 1; j--) {
      System.out.println(lines[j]);
    }
   
  }
 
}

-----------------------------------
wtd
Sun Nov 20, 2005 11:01 pm


-----------------------------------
    int i = 0;
    while(i < 99) {
      lines

Well, here you have an infinite loop.

-----------------------------------
DanShadow
Tue Nov 22, 2005 2:06 pm


-----------------------------------
Pretty simple program actually, ill quickly explain how it would work (you can do the programming though). 






//assuming the array is 3 lines long 
String[][] text=new String[2][3]; 
int numAt=0; 
//readlines, store them in text[0][1..3] 
for (int i=3;i>-1;i--) { 
text[1][numAt]=text[0][i]; 
numAt++; 
} 

 

I dont have an IDE on this computer, but what it should do is go through 
text[0][1..3] 
backwards, and store it in 
text[1][1..3] 
the numAt tracks where in text[1][1..3] the text from text[0][1..3] will be stored. 

If you cant use 2D arrays, try this instead: 
String[] text0,text1; 
text0=new String[3]; 
text1=new String[3]; 

text1[numAt]=text0[i] 

Anyways, I hope that helps you.
