Results 1 to 12 of 12

Thread: need help

  1. #1
    anteup is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default need help

    if i have four words in a string and they are all seperated by an "*" how can i get the position of all three * between the four words?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Check out the String API. There are methods there that will help you. If you still have questions after doing this, come on back and let us know which methods you think should help you the most.

    Good luck!

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Yes, read the Java docs and give a try. If you are stuck on something come back here and ask your question with what you have. As a hint, you have to deal with the index.

  4. #4
    anteup is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default ok thanks for that info,but im still having a problem

    ok i thought i had a good way to figure out the locations of the remaining *
    but i keep receiving an error message can you please take a look at my code thanks

    import javax.swing.JOptionPane;
    public class Project2
    {
    public static void main(String[]args) // main method header
    {
    //variable declarations
    String word,word1,word2,word3,word4;
    int ast1,ast2,ast3;
    int word1Num,word2Num,word3Num,word4Num;
    int totalLen;
    char word1Last; // displays the last character in word1
    String part2;

    // user input
    word = JOptionPane.showInputDialog("Enter four words, seperate each word with an * symbol "); // assignment statements

    // assignments
    totalLen = word.length();
    ast1 = word.indexOf('*'); // stores position of the first asteric as an integer
    word1 = word.substring(0,ast1); // creates substring that stores the first word as a string
    part2 = word.substring(ast1 + 1); // creates a substring to store everything after the first asteric
    ast2 = part2.indexOf('*'); // stores the second location of the * within the new string part2
    word2 = part2.substring(ast1 ,ast2 - 1); // stores the second word in a string location word2
    word1Num = word1.length(); // stores the length of characters in word1 as word1Num
    word2Num = word2.length(); // stores the length of characters in word2 as word2Num

    // print output
    System.out.println("total length is " + totalLen);
    System.out.println(word1 + " length is " + word1Num);
    System.out.println();
    System.out.println(word2 + " length is " + word2Num);




    }

    }

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    1) What are the error messages?
    2) Where are they being called from? Which lines?
    3) When posting code, please use code tags so that your code retains its formatting and is readable. To do this, you will need to paste already formatted code into the forum, highlight this code, and then press the "code" button at the top of the forum Message editor prior to posting the message. You may want to click on the Preview tab to make sure that your code is formatted correctly. Another way is to place the tag [code] at the top of your block of code and the tag [/code] at the bottom.

  6. #6
    anteup is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default woops

    Java Code:
    import javax.swing.JOptionPane;
    public class Project2 
    {
    	public static void main(String[]args) // main method header 
    	{
    		//variable declarations
    		String word,word1,word2,word3,word4;
    		int ast1,ast2,ast3;
    		int word1Num,word2Num,word3Num,word4Num;
    		int totalLen;
    		char word1Last; // displays the last character in word1
    		String part2;
    		
    		// user input
    		 word = JOptionPane.showInputDialog("Enter four words, seperate each word with an * symbol "); // assignment statements
    	 	
    		// assignments
    		totalLen = word.length(); 
    		ast1 = word.indexOf('*'); // stores position of the first asteric as an integer
    		word1 = word.substring(0,ast1); // creates substring that stores the first word as a string
    		part2 = word.substring(ast1 + 1); // creates a substring to store everything after the first asteric
    		ast2 = part2.indexOf('*'); // stores the second location of the * within the new string part2
    		word2 = part2.substring(ast1 ,ast2 - 1); // stores the second word in a string location word2
    		word1Num = word1.length(); // stores the length of characters in word1 as word1Num
    		word2Num = word2.length(); // stores the length of characters in word2 as word2Num
    		
    		// print output
    		System.out.println("total length is " + totalLen);
    		System.out.println(word1 + " length is " + word1Num);
    		System.out.println();
    		System.out.println(word2 + " length is " + word2Num);
    		
    	
    	}
    
    }
    ok it says theres an error in line 31 , its not showing the lines in eclispe for some reason but the error message displayed says

    "Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -4
    at java.lang.String.substring(Unknown Source)
    at Project2.main(Project2.java:31)"
    im not sure what that means or how to go about fixing it yet

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Also, have a look at the String methods again. There's more than one indexOf(...) method, and you may get some benefit by using the other one as well.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Here in this line you have done the mistake.

    Java Code:
    word2 = part2.substring(ast1 ,ast2 - 1);
    Did you read the Java doc. If you are using this overloaded method, first argument is the starting index of the string and it is zero base. Second argument is the end index and it's not zero base. That's why you get an exception here.

  10. #10
    kellaw is offline Member
    Join Date
    Oct 2008
    Posts
    24
    Rep Power
    0

    Default

    for (int i = 0; i < string.length(); i++){
    if(string.charAt(i) == '*')
    //do something then substring it
    }

  11. #11
    spider is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Default

    Use StringTokenizer class. The below link will help
    javaadda.blogspot.com/2008/10/how-to-break-string-into-tokens-with.html

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by spider View Post
    Use StringTokenizer class. The below link will help
    javaadda.blogspot.com/2008/10/how-to-break-string-into-tokens-with.html
    Not courage to use it, it's a legacy class to use. Regular expressions are the beast approach.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •