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

Please help!!!

0 views
Skip to first unread message

prakjava

unread,
Apr 4, 2001, 12:10:31 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);
}
}

}


Roedy Green

unread,
Apr 4, 2001, 1:28:07 PM4/4/01
to
On Wed, 04 Apr 2001 09:10:31 -0700, prakjava
<prakjava...@sun.partner.remarq.com.invalid> wrote or quoted :

>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.

Phil Hanna

unread,
Apr 4, 2001, 9:08:44 PM4/4/01
to

>Here is the text file I'm reading:
>S
>A apple
>A eggplant
...

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

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

0 new messages