Please, please help me out. I will be very grateful for any
assistance.
Cheers.
Here is the text file I'm reading:
S
A apple
A eggplant
A banana
A apple
S apple
S eggplant
S banana
R apple
S apple
HERE IS MY CODE:
import java.io.*;
import java.util.*;
public class Assignment
{
private BufferedReader reader = null;
private PrintWriter outputStream = null;
private StringTokenizer st = null;
private String nextToken;
private String line;
private int numPushPop;
private int invokeMethod;
public Assignment()
{
numPushPop = 0;
invokeMethod = 0;
}
public static void main(String[] args) throws IOException
{
Assignment as = new Assignment();
as.ReadWrite("astest.txt", "te.txt");
as.closeReaderWriter();
}
public void ReadWrite(String inputFileName, String
outputFileName)
{
try
{
reader = new BufferedReader(new FileReader
(inputFileName));
outputStream = new PrintWriter(new FileOutputStream
(outputFileName, true));
//Tokenize(reader);
line = reader.readLine();
System.out.println(line);
closeReaderWriter();
}
catch (IOException e)
{
System.out.println(" Error reading file " +
inputFileName);
System.exit(0);
}
}
public String Tokenize(BufferedReader reader)throws IOException
{
line = reader.readLine();
st = new StringTokenizer(line);
System.out.println(line);
nextToken = st.nextToken();
if (nextToken.equals("S"))
{
System.out.println(st.nextToken());
invokeMethod = stackMetric(nextToken);
}
else if (nextToken.equals("L"))
{
invokeMethod = listMetric();
}
System.out.println(nextToken);
System.out.println(nextToken);
return nextToken;
}
public int stackMetric(String nextToken)throws IOException
{
MyStack my = new MyStack();
while(line!= null)
{
while(st.hasMoreTokens())
{
nextToken = st.nextToken();
if (nextToken.equals("A"))
{
nextToken = st.nextToken();
numPushPop += my.add(nextToken);
}
else if (nextToken.equals("R"))
{
nextToken = st.nextToken();
numPushPop += my.remove
(nextToken);
}
else if (nextToken.equals("S"))
{
nextToken = st.nextToken();
numPushPop += my.search
(nextToken);
}
}
line = reader.readLine();
}
return numPushPop;
}
public int listMetric()throws IOException
{
MyStack my = new MyStack();
nextToken = st.nextToken();
while(line!= null)
{
while(st.hasMoreTokens())
{
if (nextToken.equals("A"))
{
nextToken = st.nextToken();
numPushPop += my.add(nextToken);
}
else if (nextToken.equals("R"))
{
nextToken = st.nextToken();
numPushPop += my.remove(
nextToken);
}
else if (nextToken.equals("S"))
{
nextToken = st.nextToken();
numPushPop += my.search
(nextToken);
}
}
line = reader.readLine();
}
my.printMetric((double)(numPushPop));
return numPushPop;
}
public void closeReaderWriter()
{
try
{
reader.close();
outputStream.close();
}
catch (IOException e)
{
System.out.println(" End of file reached ");
System.exit(0);
}
}
}
>I desperatley need some help. I need to create a class that
>reads a text file and then perform relevant operations on a
>Stack/LinkedList. i.e if the letter "A" is read, then the word
>next to "A" is added onto the stack or if "R" is read, then the
>word next to "R" is removed and so on.
see Homework in the Java glossary.
-
For more detail, please look up the key words mentioned in this post in
the Java Glossary at:
http://mindprod.com/jgloss.html or http://209.153.246.39/jgloss.html
If you don't see what you were looking for, complain!
or send your contribution for the glossary.
--
Roedy Green, Canadian Mind Products
Custom computer programming since 1963.
Almost ready to take on new work.
You are reading the first line (which contains just one token - "S")
and calling st.nextToken() to get that token. Next, you test that
token to see if it equals "S" (it does). Then you call
System.out.println(st.nextToken()). But there are no more tokens on
the line, so you get the exception you noted. The javadoc for
StringTokenizer explains this fairly clearly.
Unless you know each line in your data always has a fixed number of
tokens, you should either
1. Test with st.hasMoreTokens() each time before calling
st.nextToken() or
2. Get the number of tokens when you first create the StringTokenizer
by calling st.countTokens();
--
Phil Hanna
Author of JSP: The Complete Reference
http://www.philhanna.com