The StringTokenizer class has methods for getting tokens from a String using some delimiters. You need to call the methods to get the Strings.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
The StringTokenizer class has methods for getting tokens from a String using some delimiters. You need to call the methods to get the Strings.
If you don't understand my answer, don't ignore it, ask a question.
Lotus2013 (October 25th, 2013)
I changed the code in this way to use split method instead of StringTokenizer
public class Parser { public static void main(String args[]){ parser("There are 5 table (dimension: 4 in 5 in 3 meters) in the room."); } public static void parser(String str){ String type = new String("Type: "); String number = new String("Numbers: "); String size = new String("Size: "); String position = new String("Position: "); String[] tokens = str.split(" "); if(str.equals("is")){ System.out.println(number + "1"); } else{ System.out.println(number + tokens[2]); } } }
It works What is your opinion?
(And thank you for that point about StringTokenizer)
Not sure how the parser is scanning the tokens in the statement and finding the required next token.
The posted code has hard coded index values instead of using variables so that the code can know what the current token it is and what the next token is.
Given the statements: There is a ... OR There are 3 ...
if the currentTokenIndex = 1 and that token is "is" then theNextTokenIndex = 2 and that token is "a"
else if the current token is "are" then the next token needs to be a number
If you don't understand my answer, don't ignore it, ask a question.
Lotus2013 (October 25th, 2013)
I want to say that if the token equals to the, print the next token as position. How can I say this? Using split.
In a statement, what part is the "position"? I haven't see the syntax for a statement and don't know what part of a statement is the "position".
I'm not sure what you are asking. If the nextToken (read into a String) is equal to "what????"if the token equals to the, print the next token
use an if statement with the String class's equals() method.
If you don't understand my answer, don't ignore it, ask a question.
Lotus2013 (October 25th, 2013)
for example: "There are 5 tables (dimension: 4 in 5 in 3 meters) in the room."
the position is this part: "in the room" and I want to say that:
if(tokens.equals("the")){ System.out.println(position + tokens.nextToken); }
Here "tokens" is the array of strings that contains the tokens of the str sentence(the original sentence) that have been tokenized by split method. I want to say that if one of these tokens is equals to "the" then print out the next token ( which is the position. here: room). but I really don't know what I should use to accomplish this
This question that we are discussing about is the first part of an Assignment that consists of 3 parts and the due time is tomorrow night I'm really sad and disappointed
You can't use the equals() method to test if an array contains an element. You need to use a loop to look at each element in the array one by one using the equals() method with each element."tokens" is the array of stringsIf tokens is an array of Strings then it does not have a nextToken member.tokens.nextToken
The StringTokenizer class has a nextToken() method.
If you are working with arrays, there are no methods for accessing its elements. You need to use array notation: theArrayName[theIndex] to access an element of the array.
If you don't understand my answer, don't ignore it, ask a question.
Lotus2013 (October 25th, 2013)
for(int i= 0; i < tokens.length; i++){ if(tokens[i] == "the"){ System.out.println(tokens[i+1]); }
I did this but It doesn't work. It doesn't throw exception and doesn't have any errors, but It also gives no result
I set a loop to look through the array and find the "the" and then prints the next array element. What's wrong with it?
You should use the equals() method to compare the contents of String objects, NOT the == operator.
I assume tokens[i] is a String.
If you don't understand my answer, don't ignore it, ask a question.
Lotus2013 (October 25th, 2013)
I changed it and it worked Thank youfor(int i= 0; i < tokens.length; i++){ if(tokens[i].equals("the")){ System.out.println(tokens[i+1]); }
In some cases, there are more than just one word after the word "the" that declare the position. for example:
"There are 5 tables (dimension: 4 in 5 in 3 meters) in the Mike's room." Now I want to say that, when you find the "the" then print out all of the words after that. I used this but it throws exception
for(int i= 0; i < tokens.length; i++){ if(tokens[i].equals("the")){ while(strTok.hasMoreTokens()){ System.out.println(tokens[i++]); } } }
.
Lotus2013 (October 26th, 2013)
There is also something else for example when the parser finds the wanted word ( e.g it's the position (room) ) then I want to print it in this way:
Position: Room
I mean I want the first letter to be Uppercase. How can I do this?
--- Update ---
It sounds a good idea, Thank you . But now I'm talking about the Position which is always after the word "the". I want to search for the "the" and print the words after that which is the position (in this example: Milke's room)
Here I accomplished this but it won't work for all examples. For instance if there would be more than just one word for the position ( Mike's room) then it will print just "Mike's" and this is not the Position! So I want to say that when you find the "the" PLEEEEEASE :d while this sentence has words, print all of themfor(int i= 0; i < tokens.length; i++){ if(tokens[i].equals("the")){ System.out.println(tokens[i+1]); }
Used the substring() method to pick off the first letter, make it uppercase and then use concatenation to build the word again.I want the first letter to be Uppercase
If you don't understand my answer, don't ignore it, ask a question.
Lotus2013 (October 26th, 2013)
Thank you And I also want to remove the plural s in the word. For example to make the word "tables" into "Table".
Use the substring() method
If you don't understand my answer, don't ignore it, ask a question.
Lotus2013 (October 26th, 2013)
import java.util.ArrayList; import java.util.StringTokenizer; //import java.util.StringTokenizer; public class Parser { public static void main(String args[]){ parser("There are twenty two tables (dimension: 4 in 5 in 3 meters) in the Arash's room."); } public static void parser(String str){ //Declaring the basic strings to be printed out in the console; String type = new String("Type: "); String number = new String("Numbers: "); String size = new String("Size: "); String position = new String("Position: "); //parsing the str String by split method; String[] tokens = str.split(" "); //Getting the Type of the object; int counter = 0; for(int i = 1; i < tokens.length; i++){ if(tokens[i].contains("(")){ counter = i; break; } } System.out.println(type + tokens[counter - 1].substring(0,1).toUpperCase().concat(tokens[counter -1].substring(counter)) ); //Getting the number of the object; if(str.substring(6,8).equalsIgnoreCase("is")){ System.out.println(number + "1"); } else{ int counter1 = 0; for(int i = 1; i < tokens.length; i++){ if(tokens[i].contains("are")){ counter1 = i; break; } } System.out.println(number + tokens[counter1 + 1]); } //Getting the the size of the object; int counter2 = 0; for(int i = 1; i< tokens.length; i++){ if(tokens[i].contains("(dimension")){ counter2 = i; break; } } ArrayList<Character> chars = new ArrayList<>(); for( int j = counter2; j < tokens.length;j++){ if(Character.isDigit(tokens[j].toCharArray()[0])){ chars.add(tokens[j].toCharArray()[0]); } } int lent = chars.size(); if(lent == 3){ double one = Integer.parseInt(a); System.out.println(one); } } }
Here is the code I wrote today. There's a problem with the part about size:
I said if there was this token "(dimension" in the array named token, then take the index and after that, look for a digit and put the digits in an arraylist named "chars". Now, I don't know how to access each value of the arraylist so i can multiple them and calculate the size. I want to access to chars[0], chars[1] and chars[3]. Could you please help me with this?//Getting the the size of the object; int counter2 = 0; for(int i = 1; i< tokens.length; i++){ if(tokens[i].contains("(dimension")){ counter2 = i; break; } } ArrayList<Character> chars = new ArrayList<>(); for( int j = counter2; j < tokens.length;j++){ if(Character.isDigit(tokens[j].toCharArray()[0])){ chars.add(tokens[j].toCharArray()[0]); } } int lent = chars.size(); if(lent == 3){ double one = Integer.parseInt(a); System.out.println(one); } }
Thanks in advance
The []s is array notation. With an ArrayList you use get methods to access elements in the list.. I want to access to chars[0], chars[1] and chars[3].
See the API doc for the ArrayList class.
If you don't understand my answer, don't ignore it, ask a question.
Lotus2013 (October 30th, 2013)
I did this, but it doesn't workint lent = chars.size(); if(lent == 3){ int one = chars.get(2); System.out.println(one); } }
The size of the arraylist is 3 and it's value is [4,5,3] but when I attempt to reach its elements by .get() method, it prints out 52, 53 and 51 What's wrong with this?
You are converting char to int. '4' = 52
Try this:If the ArrayList contains char values, then you need to treat them as char, not intSystem.out.println("char 4="+(int)'4'); // char 4=52
If you don't understand my answer, don't ignore it, ask a question.
Lotus2013 (October 30th, 2013)
Again, Hello
I want to know how can I say that search the whole tokens[] Array and if you find the token containing "the" then print the next tokens till it was all finished.
int counter3 = 0; for(int i = 0; i< tokens.length; i++){ if(tokens[i].contains("the")){ counter3 = i+1; break; } } ArrayList<String> newTokens = new ArrayList<>(); for( int k = counter3; k < tokens.length; k++){ newTokens.add(tokens[k]); } int index = newTokens.indexOf("."); String position1 = newTokens.get(index); System.out.println(position1);
Here, I told it to look for the token that contains "the" and I set the counter to the next index of that token that contains "the". Then I broke the for loop and constructed an ArrayList and put all the tokens in that ArrayList named newTokens. Then I tried to get the index of the token that contains "." and print the String in it using .get() method. But it throws an exception. What's wrong with it?
Thank you so much for all your helps
--- Update ---
I guess I made a mistake here and it won't give the result that I want because even if it finds the index and prints the token, the output will be this:
room.
If we print the newTokens arrayList, the output will be this:
[Mike's, room.]
How can I print its elements in a way that the output would be like this:
Mike's room.
Please copy the full text of the error message and paste it here. It has important info about the error.it throws an exception. What's wrong with it?
use a loop and the System.out.print() method to put the String on the same line as the last call to print()How can I print its elements in a way that the output would be like this:
Use a call to println() to have the next output moved to the next line.
If you don't understand my answer, don't ignore it, ask a question.
Lotus2013 (November 4th, 2013)
Here is the error message:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at java.util.ArrayList.elementData(Unknown Source) at java.util.ArrayList.get(Unknown Source) at org.bihe.parser.parser(parser.java:137) at org.bihe.parser.main(parser.java:10)
--- Update ---
I did not understand this part Would you please explain it more?
At line 137 the code called the get() method with an index that is out of bounds: -1Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.ArrayList.elementData(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at org.bihe.parser.parser(parser.java:137)
Indexes range in valid from 0 to the length-1. Add code to test that the index variable's value is in range before using it.
Try testing the print() method by calling it several times in a row to see where it puts the output.
If you don't understand my answer, don't ignore it, ask a question.
Lotus2013 (November 4th, 2013)