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.

Page 2 of 2 FirstFirst 12
Results 26 to 49 of 49

Thread: java parser

  1. #26
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java parser

    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.

  2. The Following User Says Thank You to Norm For This Useful Post:

    Lotus2013 (October 25th, 2013)

  3. #27
    Junior Member
    Join Date
    Oct 2013
    Posts
    27
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Re: java parser

    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)

  4. #28
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java parser

    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.

  5. The Following User Says Thank You to Norm For This Useful Post:

    Lotus2013 (October 25th, 2013)

  6. #29
    Junior Member
    Join Date
    Oct 2013
    Posts
    27
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Re: java parser

    I want to say that if the token equals to the, print the next token as position. How can I say this? Using split.

  7. #30
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java parser

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

    if the token equals to the, print the next token
    I'm not sure what you are asking. If the nextToken (read into a String) is equal to "what????"
    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.

  8. The Following User Says Thank You to Norm For This Useful Post:

    Lotus2013 (October 25th, 2013)

  9. #31
    Junior Member
    Join Date
    Oct 2013
    Posts
    27
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Re: java parser

    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

  10. #32
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java parser

    "tokens" is the array of strings
    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.nextToken
    If tokens is an array of Strings then it does not have a nextToken member.
    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.

  11. The Following User Says Thank You to Norm For This Useful Post:

    Lotus2013 (October 25th, 2013)

  12. #33
    Junior Member
    Join Date
    Oct 2013
    Posts
    27
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Re: java parser

    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?

  13. #34
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java parser

    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.

  14. The Following User Says Thank You to Norm For This Useful Post:

    Lotus2013 (October 25th, 2013)

  15. #35
    Junior Member
    Join Date
    Oct 2013
    Posts
    27
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Re: java parser

    for(int i= 0; i < tokens.length; i++){
     
    			if(tokens[i].equals("the")){
    				System.out.println(tokens[i+1]);
    			}
    I changed it and it worked Thank you

  16. #36
    Junior Member
    Join Date
    Oct 2013
    Posts
    27
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Re: java parser

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

  17. #37
    Member
    Join Date
    Feb 2013
    Location
    earth
    Posts
    88
    Thanks
    12
    Thanked 9 Times in 9 Posts

    Default Re: java parser

    .

  18. The Following User Says Thank You to javaiscool For This Useful Post:

    Lotus2013 (October 26th, 2013)

  19. #38
    Junior Member
    Join Date
    Oct 2013
    Posts
    27
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Re: java parser

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

    Quote Originally Posted by javaiscool View Post
    I would searc for the word immediagely after "there is a" and throw that into a variable, then grab the numbers of the dimensions and multiply them then print it all
    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)
    for(int i= 0; i < tokens.length; i++){
     
    			if(tokens[i].equals("the")){
    				System.out.println(tokens[i+1]);
    			}
    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 them

  20. #39
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java parser

    I want the first letter to be Uppercase
    Used the substring() method to pick off the first letter, make it uppercase and then use concatenation to build the word again.
    If you don't understand my answer, don't ignore it, ask a question.

  21. The Following User Says Thank You to Norm For This Useful Post:

    Lotus2013 (October 26th, 2013)

  22. #40
    Junior Member
    Join Date
    Oct 2013
    Posts
    27
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Re: java parser

    Thank you And I also want to remove the plural s in the word. For example to make the word "tables" into "Table".

  23. #41
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java parser

    Use the substring() method
    If you don't understand my answer, don't ignore it, ask a question.

  24. The Following User Says Thank You to Norm For This Useful Post:

    Lotus2013 (October 26th, 2013)

  25. #42
    Junior Member
    Join Date
    Oct 2013
    Posts
    27
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Re: java parser

    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:

     
    				//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);
    				}
     
    				}
    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?
    Thanks in advance

  26. #43
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java parser

    . I want to access to chars[0], chars[1] and chars[3].
    The []s is array notation. With an ArrayList you use get methods to access elements in the list.
    See the API doc for the ArrayList class.
    If you don't understand my answer, don't ignore it, ask a question.

  27. The Following User Says Thank You to Norm For This Useful Post:

    Lotus2013 (October 30th, 2013)

  28. #44
    Junior Member
    Join Date
    Oct 2013
    Posts
    27
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Re: java parser

    int lent = chars.size();
    				if(lent == 3){
    				int one = chars.get(2);
    				System.out.println(one);
    				}
     
    				}
    I did this, but it doesn't work
    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?

  29. #45
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java parser

    You are converting char to int. '4' = 52
    Try this:
          System.out.println("char 4="+(int)'4');  // char 4=52
    If the ArrayList contains char values, then you need to treat them as char, not int
    If you don't understand my answer, don't ignore it, ask a question.

  30. The Following User Says Thank You to Norm For This Useful Post:

    Lotus2013 (October 30th, 2013)

  31. #46
    Junior Member
    Join Date
    Oct 2013
    Posts
    27
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Re: java parser

    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.


  32. #47
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java parser

    it throws an exception. What's wrong with it?
    Please copy the full text of the error message and paste it here. It has important info about the error.


    How can I print its elements in a way that the output would be like this:
    use a loop and the System.out.print() method to put the String on the same line as the last call to print()
    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.

  33. The Following User Says Thank You to Norm For This Useful Post:

    Lotus2013 (November 4th, 2013)

  34. #48
    Junior Member
    Join Date
    Oct 2013
    Posts
    27
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Re: java parser

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

    Quote Originally Posted by Norm View Post

    use a loop and the System.out.print() method to put the String on the same line as the last call to print()
    Use a call to println() to have the next output moved to the next line.
    I did not understand this part Would you please explain it more?

  35. #49
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java parser

    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 line 137 the code called the get() method with an index that is out of bounds: -1
    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.

  36. The Following User Says Thank You to Norm For This Useful Post:

    Lotus2013 (November 4th, 2013)

Page 2 of 2 FirstFirst 12

Similar Threads

  1. How to start a Java parser
    By Sharmeen in forum Algorithms & Recursion
    Replies: 10
    Last Post: October 6th, 2012, 01:41 PM
  2. java lexical analyzer and parser tool
    By swar sarkos in forum Java Theory & Questions
    Replies: 0
    Last Post: March 17th, 2011, 11:59 AM
  3. XMI parser in java
    By chiru in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 13th, 2011, 11:54 PM
  4. Parser for java Class
    By duarte in forum Java Theory & Questions
    Replies: 1
    Last Post: November 16th, 2010, 07:22 PM
  5. Java Parser
    By sid0009 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: August 20th, 2010, 11:06 AM

Tags for this Thread