• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 223
  • Last Modified:

reading elements from a file into a stack and outputing them in a reversed form to another File


my program is  supposed to read names from a file an output them to another file in a reversed form. i have a problem with my program because it does not reverse the names from another file  in to another .Where did i get  it wrong.

this is the code below



import java.io.*;
class Stacking implements Stacks
{
   String sourceName, destName;
   BufferedReader source;
   BufferedWriter dest;
   String line;
  Stacking s = new Stacking ();
   Stacking r = new Stacking ();
        String num[]= new String[size];    
     private int top = 0;
     
      String word;

  public Stacking()
 {
 top = 0;
 }

 public boolean emptyStack()
{
 if (top==EMPTY)// if stack is Empty returns true
  return true;  // else return false
else
  return false;
}// End emptyStack()

 public boolean fullStack()
{
if(top ==FULL)//if stack is full returns true
return true;// else return false
else
return false;
}//End fullStack()


public void push(String line)

{
 if (!fullStack())//if not full push element
 
num[top++]= line;
}//End push()


public String pop()
{
if(!emptyStack()) //if not empty return int
 
     return num[--top];//else return empty

 
else
 
   return null;
}//End pop()

public String peek()
{
if(!emptyStack())//if not empty return the top int
return num[top];//else return empty

else return null;



 
}//End peek()
 


     
     



     
     
 
   private boolean openFiles()  // return true if files open, else false
   {
     // open the source
     try
     {      
       source = new BufferedReader(new FileReader( sourceName ));
     }
     catch ( IOException iox )
     {
       System.out.println("Problem opening " + sourceName );
       return false;
     }
     // open the destination
     try
     {      
       dest = new BufferedWriter(new FileWriter( destName ));
     }
     catch ( IOException iox )
     {
       System.out.println("Problem opening " + destName );
       return false;
     }
     return true;
   }
   private boolean copyFiles()  // return true if copy worked, else false
   {
     try
     {      
       
       line = source.readLine();
        while ( line != null )
       p.push(line);
       
        {
         dest.write(r.pop());
         dest.newLine();
         line = source.readLine();
       }
     }
     catch ( IOException iox )
     {
       System.out.println("Problem reading or writing" );
       return false;
     }
     return true;
   }

   private boolean closeFiles()  //return true if files close, else false
   {
     boolean retVal=true;
     // close the source
     try
     {      
       source.close();
     }
     catch ( IOException iox )
     {
       System.out.println("Problem closing " + sourceName );
       retVal = false;
     }
     // close the destination
     try
     {      
       dest.close();
     }
     catch ( IOException iox )
     {
       System.out.println("Problem closing " + destName );
       retVal = false;
     }
     return retVal;
   }
   public boolean copy(String src, String dst )
   {
     sourceName = src ;
     destName   = dst ;
     return openFiles() && copyFiles() && closeFiles();
   }
}
 class StackererTester
{
  public static void main ( String[] args )
  {
               
       if ( args.length == 3 && args[1].toUpperCase().equals("TO") )
      new CopyMaker().copy(args[0], args[2]);
    else
      System.out.println("java CopyTextFile source to destination");
  }
}
 
0
Bwaycct
Asked:
Bwaycct
  • 2
1 Solution
 
jmfrazier1Commented:
ok, you had some serious problems with your code, but i got them figured out. I dont know where you got the interface Stacking, but i took that completly out since it isnt needed except for the size variable and i made this a standalone class. when you read the line in, you need to do that in the body or the condition of the while statement and you do not need to read in the writing phase. when popping off the stack, you should use top-1 since you increment after you add to the stack and i also cannot find the CopyMaker class, but that can also be removed since the call to test it can be made by sinply instantiating the Stacking class.  if you need any more explanation, just let me know.  here is my code:


import java.io.*;
import java.util.*;

class Stacking //implements Stacks
 { //SIZE needs to be set to the size that you need to be the maximum size of the stack
   //you may want to experiment with an expandible stack that will allow you to make
   //this a beginning point and then have an increment.
   int size = 10;
   int EMPTY = 0;
   int FULL = 10;
   String sourceName, destName;
   BufferedReader source;
   BufferedWriter dest;
   String line;
   //these 2 statements cause the java stack to overflow since you are recursing in the
   //construction phase of the object creation
   //Stacking s = new Stacking ();
   //Stacking r = new Stacking ();
   String num[]= new String[size];
   private int top = 0;

   String word;
   public Stacking()
    { top = 0;
    }
   public boolean emptyStack()
    { if (top == EMPTY)// if stack is Empty returns true
       { return true;  // else return false
       }
      else
       { return false;
       }
    }// End emptyStack()
   public boolean fullStack()
    { if(top == FULL)//if stack is full returns true
       { return true;// else return false
       }
      else
       { return false;
       }
    }//End fullStack()
   public void push(String line)
    { if (!fullStack())//if not full push element
       { num[top++]= line;
       }
    }//End push()
   public String pop()
    { if(!emptyStack()) //if not empty return int
       { return num[--top];//else return empty
       }
      else
       { return null;
       }
    }//End pop()
   public String peek()
    { if(!emptyStack())//if not empty return the top int
       { return num[top-1];//else return empty
       }
      else
       { return null;
       }
    }//End peek()
   private boolean openFiles()  // return true if files open, else false
    { // open the source
      try
       { source = new BufferedReader(new FileReader( sourceName ));
       }
      catch ( IOException iox )
       { System.out.println("Problem opening " + sourceName );
         return false;
       }
      // open the destination
      try
       { dest = new BufferedWriter(new FileWriter( destName ));
       }
      catch ( IOException iox )
       { System.out.println("Problem opening " + destName );
         return false;
       }
      return true;
    }
   private boolean copyFiles()  // return true if copy worked, else false
    { try
       { //line = source.readLine();
         while ( (line = source.readLine()) != null )
          { /*p.*/push(line);
          }
         while( peek() != null)
          { dest.write(/*r.*/pop());
            dest.newLine();
            //line = source.readLine();
          }
       }
      catch ( IOException iox )
       { System.out.println("Problem reading or writing" );
         return false;
       }
      return true;
    }
   private boolean closeFiles()  //return true if files close, else false
    { boolean retVal=true;
      // close the source
      try
       { source.close();
       }
      catch ( IOException iox )
       { System.out.println("Problem closing " + sourceName );
         retVal = false;
       }
      // close the destination
      try
       { dest.close();
       }
      catch ( IOException iox )
       { System.out.println("Problem closing " + destName );
         retVal = false;
       }
      return retVal;
    }
   public boolean copy(String src, String dst )
    { sourceName = src ;
      destName   = dst ;
      return openFiles() && copyFiles() && closeFiles();
    }
   public static void main ( String[] args )
    { if ( args.length == 3 && args[1].toUpperCase().equals("TO") )
       { new Stacking().copy(args[0], args[2]);
       }
      else
       { System.out.println("java Stacking source to destination");
       }
    }
 }
0
 
CleanupPingCommented:
Bwaycct:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
jmfrazier1Commented:
This answer works for what the question was asked for.  And since I was the only one to answer, I believe the points should be awarded to me.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now