Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

StringTokenizers, linked lists

0 views
Skip to first unread message

prakjava

unread,
Apr 4, 2001, 12:30:06 PM4/4/01
to
Hi there,
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.
So I decided to use StringTokenizers to break the text file into
tokens and subsequently add, remove etc,the nextToken. I hope
you're with me...( I already have the Stack and LinkedList
classes.This class is to test their functionality) My code
compiles, but I get the following message when I run the
program:
Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken
(StringTokenizer.java:165)
at Assignment.Tokenize(Assignment.java:69)
at Assignment.ReadWrite(Assignment.java:39)
at Assignment.main(Assignment.java:26)

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

}

Harald Bock

unread,
Apr 5, 2001, 8:39:47 AM4/5/01
to
> Exception in thread "main" java.util.NoSuchElementException
> at java.util.StringTokenizer.nextToken
> (StringTokenizer.java:165)
> at Assignment.Tokenize(Assignment.java:69)
> at Assignment.ReadWrite(Assignment.java:39)
> at Assignment.main(Assignment.java:26)

I'm not quite sure but you should check for more tokens before
calling nextToken():

if (st.hasMoreTokens()) {...}


HTH
Harald

0 new messages