?
Solved

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

Posted on 2003-04-01
4
Medium Priority
?
214 Views
Last Modified: 2010-04-17

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
Comment
Question by:Bwaycct
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
jmfrazier1 earned 560 total points
ID: 8250116
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
 

Expert Comment

by:CleanupPing
ID: 9473898
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
 
LVL 1

Expert Comment

by:jmfrazier1
ID: 9476193
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

Introducing Priority Question

Increase expert visibility of your issues by participating in Priority Question, our latest feature for Premium and Team Account holders. Adjust the priority of your question to get emergent issues in front of subject-matter experts for help when you need it most.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Introduction to Processes
Suggested Courses

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question