Results 1 to 2 of 2
Thread: Looping through tokens
- 09-12-2010, 10:23 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 5
- Rep Power
- 0
Looping through tokens
Hi again,
I'm currently stuck on a program. I have a string that is turned into tokens. I need to loop through the tokens and so the output will look something like this:
Bob 45 34 29 1 87 total 197
total Bob 98 34 23 23 total 178
and so on. I know the code I have right now isn't close to being correct but I wanted to get the idea i had down quickly. I wanted to see if i'm on the right(ish) track or should abandon this method and go a different way? and also when I'm looping and trying to check what type the next token is how do I do that? Like so, if (S = word) or if (S = float).
Thanks in advance
Java Code:import java.io.*; import java.util.*; public class theLoop { public static void main(String[] args) { String S = "Bob 45 34 29 1 87 total 789 total Bob 98 34 23 23 fred 50 50 50 40 total 190"; StringTokenizer StrTok = new StringTokenizer(S); while (StrTok.hasMoreTokens) { String S = StrTok.nextToken(); int State == 1; //If statement for first state if (State = 1 && S = char) { State == 2; } else { System.out.println("Name is not the first object, program terminating"); } //Statement for the second state if (State = 2 && S = char) { //Combine with previous word to form name State == 2; } else if (State == 2 && S = float) { State == 3; } //Statement for the 3rd state if (State = 3 && S = float) { //Add with previous number State == 3; } else if (State = 3 && S = "total") { State == 4; } else { State == 5; } //Statement for the 4th state if (State = 4 && S = float) { //Add all the previous numbers //Print Name followed by numbers //and total State == 1; } else { //Keep previous total b/c it is part of a name //Print name, numbers, and total again State == 2; } if (State = 5) { //Print name, number, and totals //keep new word State == 2; } } }
- 09-12-2010, 11:34 AM #2
hi Landon, your code if full of errors. please learn the basics like the difference between = and ==, otherwise you will never be able to wrote advanced code. a starting point is here Java Tutorials and then follow the links in the section Trails the Basics. said that here is some code that produce the output like
Bob = word
45 = int
and so on. note that the number are all formatted as integer and not float.
Java Code:import java.io.*; import java.util.*; public class theLoop { public static void main(String[] args) { String input = "Bob 45 34 29 1 87 total 789 total Bob 98 34 23 23 fred 50 50 50 40 total 190"; // first of all replace multiple spaces with a single one String s1 = input.replaceAll("\\b\\s{2,}\\b", " "); // the following println is only for checking the s1 string after the replaceAll System.out.println(s1); // split your string in tokens String[] s2 = s1.split(" "); for (String token : s2) { try { Integer i = Integer.valueOf(token); System.out.println(i + " = int "); // if the valueOf throws a exception if the token was not a integer // not very elegant but working } catch (IllegalArgumentException ex) { // the IllegalArgumentException was thrown so the token is a word System.out.println(token + " = word"); } } } }
Similar Threads
-
Indexing tokens
By peliukasss in forum LuceneReplies: 4Last Post: 08-06-2010, 01:40 AM -
Getting tokens using Scanner class
By Java Tip in forum Java TipReplies: 0Last Post: 02-05-2008, 09:11 AM -
tokens
By Gilgamesh in forum New To JavaReplies: 5Last Post: 12-02-2007, 11:30 PM -
How to use StringTokenizer for multiple tokens
By javaplus in forum New To JavaReplies: 2Last Post: 11-29-2007, 09:38 AM -
tokens
By Gilgamesh in forum New To JavaReplies: 3Last Post: 11-25-2007, 02:39 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks