Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 73
  • Last Modified:

StringTokenizer not printing last item in expression


I hav something like this

while (st.hasMoreTokens( )) {
    if ( !token.matches("[0-9\\(\\)\\+\\-\\*/]+") ) {
        System.err.println("INVALID EXPRESSION");
        System.exit(1);
    } else {
        System.out.println(token);
        token = st.nextToken( );
    }
}
System.exit(1);

Where i hav a StringTokenizer goin through the string which is in a file...
What happens here is if i hav a expression in the file, for example ( 4 + 3 )..
it prints:

(
4
+
3

.. It doesnt print watever the last item is...

Puzzled..
0
AnthonyCosenza
Asked:
AnthonyCosenza
  • 7
  • 4
1 Solution
 
CEHJCommented:
How are you creating the tokenizer?
0
 
AnthonyCosenzaAuthor Commented:
while (line != null) {

    String token;
    StringTokenizer st = new StringTokenizer(line);
                  
    // Set token to be the first of item in the String
    token = st.nextToken();
....
}

like so..
0
 
CEHJCommented:
Can you post what's in the line here please?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
AnthonyCosenzaAuthor Commented:
FileReader fr = new FileReader("expression.txt");
BufferedReader br = new BufferedReader(fr);
            
// Create a String to store the contents of the file
String line = br.readLine( );


Thats it... line is wat is read from the file.. which is ( 4 + 3 ) for example..
0
 
CEHJCommented:
The following will give you an array of atoms

String[] atoms = line.split("[0-9\\(\\)\\+\\-\\*/]");
0
 
AnthonyCosenzaAuthor Commented:
I dont want an array... i want to kno why i'm not getting the last "atom" with the string tokenizer the way i am doin it...
0
 
CEHJCommented:
Because you call this:

>>token = st.nextToken( );

as the last line in your loop, which advances the 'pointer' and you do nothing with it
0
 
CEHJCommented:
I thought you might want the atoms - that's why i suggested the split thing. Have you got the  thing about nextToken?
0
 
AnthonyCosenzaAuthor Commented:
ok, if i remove that line it just prints the first "atom" out continuously...

What can i do to stop this and get all the values in the expressions..
0
 
CEHJCommented:
String token = null;
while (st.hasMoreTokens( )) {
      token = st.nextToken();      
      if ( !token.matches("[0-9\\(\\)\\+\\-\\*/]+") ) {
            System.err.println("INVALID EXPRESSION");
            System.exit(1);
      } else {
            System.out.println(token);
      }
}
0
 
CEHJCommented:
8-)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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