Results 1 to 15 of 15
  1. #1
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default Reversing and inputting files

    Ok I made a similar thread about this a while ago but it didn't make much sense

    here is the objective


    Programming Problem: Text File Processing

    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



    Your main class should be in a file called called ReverseDriver. The class where all the work is done should be called Reversal, and should extend the Echo class from chapter 10 of the textbook.





    So here is what I have my main

    Java 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();
        }
    
    }








    The echo class
    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 the writer class where is where everything is changed mostly

    Java Code:
     import java.util.Scanner;
      import java.io.*;
    import java.util.StringTokenizer;
    public class Writer extends Echo {
         int countline=0;
        PrintWriter writer;
    
        String[] Array = new String[100];
    
        public Writer(String f) throws IOException {
          super(f);
          System.out.println(System.getProperty("user.dir"));
          writer = new PrintWriter("backwards.txt");
    }
    
    
         public void readLines(){ // reads lines, hands each to processLine
    
             while(scan.hasNext()){
         countline++;
         processLine(scan.nextLine());
         for(int i = countline-1; i>-1; i--) {
    writer.print(Array[i]);
         }
         }
    
    
        scan.close();
    
    writer.close();
       }
    
        public void processLine(String line){ // does the real processing work
         StringTokenizer st = new StringTokenizer(line);
    
         String d= " ";
    
         Array[countline]=line;
    
      //   writer.print(Array[countline]);
    
    
     //    while (st.hasMoreTokens()) {
    
       //  }
    
       /*    for(int i=countline; i>0; i--) {
               s[i]=line;
          //     writer.print(s[i]);
             //  writer.println("");
           }
    
         for(int m=countline; m>0; m--) {
               writer.print(s[m]);
             }*/
        writer.println("");
    
      }
    
    
    
    
    }
    The problems lie in readlines and processline and I am totally confused


    On an input like this
    Java Code:
    Chapter 3 Modeling with Objects 
    Section 1. Objects, and the Old MacDonald Problem 
    In this chapter we'll first take a close look at how Java's object system can be used to model a programming problem. Our base example will be the text to that old saw "Old MacDonald had a farm". In case you've forgotten it, you can look at it here:
    Old MacDonald Had a Farm
    Before we look explicitly at the mapping to Java, let's make some observations about the song's overall structure. Old MacDonald is laid out like this:
    Chorus
    Pig verse
    Chorus
    Chorus
    Dog verse
    Chorus
    Here the chorus is simply "Old MacDonald had a farm ei, ei, o", and it repeats, unaltered, before and after every verse. Verses, on the other hand, vary from animal to animal: each verse names a different animal and a different associated animal noise, e.g. dog, then woof, pig, then oink.
    We want to build a Java application that uses the mechanics of object-oriented programming explicitly to send the song text to the console. Recall that while Java does important atomic chores like writing lines to the console using primitive statements like System.out.println, the real currency of the language is the mechanism of objects, and their associated blueprints, class definitions. 
    What are the objects and classes here? Determining this is often the most creative step in object-oriented programming. We'll talk about this a lot in coming chapters, but for now let's forego this step and simply state what they are. There are chorus objects (actually just one), which we'll create from a MacChorus class; there are verse objects (and here there needs to be several, since different verses have different attributes) from a MacVerse class; and finally there is a master or "driver" class that coordinates objects and object behavior from the other two classes. This is where main resides. We'll call this the MacSong class. Here is a diagram of the classes:

    It throws this out
    Java Code:
    null
    Chapter 3 Modeling with Objects null
    Section 1. Objects, and the Old MacDonald Problem Chapter 3 Modeling with Objects null
    In this chapter we'll first take a close look at how Java's object system can be used to model a programming problem. Our base example will be the text to that old saw "Old MacDonald had a farm". In case you've forgotten it, you can look at it here:Section 1. Objects, and the Old MacDonald Problem Chapter 3 Modeling with Objects null
    Old MacDonald Had a FarmIn this chapter we'll first take a close look at how Java's object system can be used to model a programming problem. Our base example will be the text to that old saw "Old MacDonald had a farm". In case you've forgotten it, you can look at it here:Section 1. Objects, and the Old MacDonald Problem Chapter 3 Modeling with Objects null
    Before we look explicitly at the mapping to Java, let's make some observations about the song's overall structure. Old MacDonald is laid out like this:Old MacDonald Had a FarmIn this chapter we'll first take a close look at how Java's object system can be used to model a programming problem. Our base example will be the text to that old saw "Old MacDonald had a farm". In case you've forgotten it, you can look at it here:Section 1. Objects, and the Old MacDonald Problem Chapter 3 Modeling with Objects null
    ChorusBefore we look explicitly at the mapping to Java, let's make some observations about the song's overall structure. Old MacDonald is laid out like this:Old MacDonald Had a FarmIn this chapter we'll first take a close look at how Java's object system can be used to model a programming problem. Our base example will be the text to that old saw "Old MacDonald had a farm". In case you've forgotten it, you can look at it here:Section 1. Objects, and the Old MacDonald Problem Chapter 3 Modeling with Objects null
    Pig verseChorusBefore we look explicitly at the mapping to Java, let's make some observations about the song's overall structure. Old MacDonald is laid out like this:Old MacDonald Had a FarmIn this chapter we'll first take a close look at how Java's object system can be used to model a programming problem. Our base example will be the text to that old saw "Old MacDonald had a farm". In case you've forgotten it, you can look at it here:Section 1. Objects, and the Old MacDonald Problem Chapter 3 Modeling with Objects null
    ChorusPig verseChorusBefore we look explicitly at the mapping to Java, let's make some observations about the song's overall structure. Old MacDonald is laid out like this:Old MacDonald Had a FarmIn this chapter we'll first take a close look at how Java's object system can be used to model a programming problem. Our base example will be the text to that old saw "Old MacDonald had a farm". In case you've forgotten it, you can look at it here:Section 1. Objects, and the Old MacDonald Problem Chapter 3 Modeling with Objects null
    ChorusChorusPig verseChorusBefore we look explicitly at the mapping to Java, let's make some observations about the song's overall structure. Old MacDonald is laid out like this:Old MacDonald Had a FarmIn this chapter we'll first take a close look at how Java's object system can be used to model a programming problem. Our base example will be the text to that old saw "Old MacDonald had a farm". In case you've forgotten it, you can look at it here:Section 1. Objects, and the Old MacDonald Problem Chapter 3 Modeling with Objects null
    Dog verseChorusChorusPig verseChorusBefore we look explicitly at the mapping to Java, let's make some observations about the song's overall structure. Old MacDonald is laid out like this:Old MacDonald Had a FarmIn this chapter we'll first take a close look at how Java's object system can be used to model a programming problem. Our base example will be the text to that old saw "Old MacDonald had a farm". In case you've forgotten it, you can look at it here:Section 1. Objects, and the Old MacDonald Problem Chapter 3 Modeling with Objects null
    ChorusDog verseChorusChorusPig verseChorusBefore we look explicitly at the mapping to Java, let's make some observations about the song's overall structure. Old MacDonald is laid out like this:Old MacDonald Had a FarmIn this chapter we'll first take a close look at how Java's object system can be used to model a programming problem. Our base example will be the text to that old saw "Old MacDonald had a farm". In case you've forgotten it, you can look at it here:Section 1. Objects, and the Old MacDonald Problem Chapter 3 Modeling with Objects null
    Here the chorus is simply "Old MacDonald had a farm ei, ei, o", and it repeats, unaltered, before and after every verse. Verses, on the other hand, vary from animal to animal: each verse names a different animal and a different associated animal noise, e.g. dog, then woof, pig, then oink.ChorusDog verseChorusChorusPig verseChorusBefore we look explicitly at the mapping to Java, let's make some observations about the song's overall structure. Old MacDonald is laid out like this:Old MacDonald Had a FarmIn this chapter we'll first take a close look at how Java's object system can be used to model a programming problem. Our base example will be the text to that old saw "Old MacDonald had a farm". In case you've forgotten it, you can look at it here:Section 1. Objects, and the Old MacDonald Problem Chapter 3 Modeling with Objects null
    We want to build a Java application that uses the mechanics of object-oriented programming explicitly to send the song text to the console. Recall that while Java does important atomic chores like writing lines to the console using primitive statements like System.out.println, the real currency of the language is the mechanism of objects, and their associated blueprints, class definitions. Here the chorus is simply "Old MacDonald had a farm ei, ei, o", and it repeats, unaltered, before and after every verse. Verses, on the other hand, vary from animal to animal: each verse names a different animal and a different associated animal noise, e.g. dog, then woof, pig, then oink.ChorusDog verseChorusChorusPig verseChorusBefore we look explicitly at the mapping to Java, let's make some observations about the song's overall structure. Old MacDonald is laid out like this:Old MacDonald Had a FarmIn this chapter we'll first take a close look at how Java's object system can be used to model a programming problem. Our base example will be the text to that old saw "Old MacDonald had a farm". In case you've forgotten it, you can look at it here:Section 1. Objects, and the Old MacDonald Problem Chapter 3 Modeling with Objects null
    Anyone got any ideas what is wrong? I am totally lost.

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    I would suggest that you use a smaller input to be able to debug your code. What does it do with the first sample you gave:
    hello there
    Dana
    old pal!
    And follow the flow of your code with printlns. Place them throughout your code, at key places that will show what is right or wrong.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Your main class should be in a file called called ReverseDriver.
    Where is this class as I don't see it?

  4. #4
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    Oh I just named my classes wrong for the time being, I shoulda read the question, but it doesn't really matter for now because I still need to get the thing working :/

  5. #5
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    Also I tried another input
    Java Code:
    hello there
    old pal
    whats are you doing here?
    and I got this

    Java Code:
    null
    hello therenull
    old palhello therenull

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by jigglywiggly View Post
    Oh I just named my classes wrong for the time being, I shoulda read the question, but it doesn't really matter for now because I still need to get the thing working :/
    but you then also confuse me. Also, I don't see a whole class, just a fragment. Why not post the whole thing?

  7. #7
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    ReverseDriver=WriterDriver
    Reverse=Writer
    Echo=Echo
    that's everything I have :O

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    So how would you do this on paper? What logic would you follow?

    The key here is to think of the steps you need to do before committing code to the computer. The main difference between the two programs is that your first one simply echos everything it encounters. The second one can't do that. It has to have a two step process. 1) Read the file and store each line in an array and 2) Then read out that array, but backwards -- use a simple for loop.

  9. #9
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    But process line itself is in a loop, so I got really confused because if I make a loop in processline it would be a loop inside a loop :?

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Your algorithm is going to have to be two stages:
    1) get the strings and store them: that's one for loop, and
    2) read out the data but backwards: that's another for loop.

    But you can't do this by nesting one loop inside of another. You'll have to first do the storage, and then after the complete file has been read, do the other output loop. Think on this, write down what happens on paper, and you'll see that you have no choice here.

  11. #11
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    This might seem drastic, but it will be faster then trying to fix the program that ate the world... start over. Taking Furball's post, the pseudo code would look something like this:

    Java Code:
    start loop
    read line from file
    place the read line into an array as an element
    end loop
    array size = x
    start loop 
    read the last element of the array [x]
    print line
    decrement element place x--
    end loop
    and your done !!!

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  12. #12
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    Ok I get what you are saying so I tried this instead I cahnge the for loop in readlines outside of the otherloop. Well you take a look please :D

    Java Code:
    import java.util.Scanner;
      import java.io.*;
    import java.util.StringTokenizer;
    public class Writer extends Echo {
         int countline=0;
        PrintWriter writer;
    
        String[] Array = new String[100];
    
        public Writer(String f) throws IOException {
          super(f);
          System.out.println(System.getProperty("user.dir"));
          writer = new PrintWriter("backwards.txt");
    }
    
    
         public void readLines(){ // reads lines, hands each to processLine
    
             while(scan.hasNext()){
         countline++;
         processLine(scan.nextLine());
         }
               for(int i = countline-1; i>-1; i--) {
    writer.println(Array[i]);
         }
    
    
    
        scan.close();
    
    writer.close();
       }
    
        public void processLine(String line){ // does the real processing work
         StringTokenizer st = new StringTokenizer(line);
    
         String d= " ";
    Array[countline]=line;
    
    
     //    Array[countline]=line;
    
    
        writer.println("");
    
      }
    
    
    
    
    }
    I thought that would work but my end result is this


    input

    hello there
    old pal
    whats are you doing here?

    Output:


    old pal
    hello there
    null
    Last edited by jigglywiggly; 03-23-2009 at 05:02 AM.

  13. #13
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    Quote Originally Posted by CJSLMAN View Post
    This might seem drastic, but it will be faster then trying to fix the program that ate the world... start over. Taking Furball's post, the pseudo code would look something like this:

    Java Code:
    start loop
    read line from file
    place the read line into an array as an element
    end loop
    array size = x
    start loop 
    read the last element of the array [x]
    print line
    decrement element place x--
    end loop
    and your done !!!

    Luck,
    CJSL
    Oh I shoulda mentioned this, I woulda done a much simpler way of doing this but I was forced to use echo and I had to override the other commands in writer. On a side note, I really do appreciate you guys reading this junkmess of code, I mean it's not easy reading other peoples work.

  14. #14
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    Oh wiat I just got it I think I changed it to this
    Java Code:
    import java.util.Scanner;
      import java.io.*;
    import java.util.StringTokenizer;
    public class Writer extends Echo {
         int countline=0;
        PrintWriter writer;
    
        String[] Array = new String[100];
    
        public Writer(String f) throws IOException {
          super(f);
          System.out.println(System.getProperty("user.dir"));
          writer = new PrintWriter("backwards.txt");
    }
    
    
         public void readLines(){ // reads lines, hands each to processLine
    
             while(scan.hasNext()){
         countline++;
         processLine(scan.nextLine());
         }
               for(int i = countline; i>0; i--) {
    writer.println(Array[i]);
         }
    
    
    
        scan.close();
    
    writer.close();
       }
    
        public void processLine(String line){ // does the real processing work
         StringTokenizer st = new StringTokenizer(line);
    
         String d= " ";
    Array[countline]=line;
    
    
     //    Array[countline]=line;
    
    
        writer.println("");
    
      }
    
    
    
    
    }

    On an input like this
    hello there
    old pal
    whats are you doing here?


    I got this




    whats are you doing here?
    old pal
    hello there

  15. #15
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    One thing, I'm getting like 3 lines of white space before any of the text shows up? Anyone see anything particularly wrong with the code?

Similar Threads

  1. inputting and writing a file backwards
    By jigglywiggly in forum New To Java
    Replies: 0
    Last Post: 03-18-2009, 08:24 PM
  2. Replies: 2
    Last Post: 12-06-2008, 07:54 PM
  3. Reversing
    By whosadork in forum New To Java
    Replies: 14
    Last Post: 11-06-2008, 05:29 AM
  4. Reversing String
    By mew in forum New To Java
    Replies: 4
    Last Post: 12-02-2007, 10:42 PM
  5. reversing a string
    By toad in forum New To Java
    Replies: 5
    Last Post: 11-07-2007, 10:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •