Results 1 to 17 of 17
Like Tree1Likes
  • 1 Post By Norm

Thread: How to specify a certain character in a word?

  1. #1
    gabrielpr12 is offline Member
    Join Date
    Nov 2011
    Posts
    41
    Rep Power
    0

    Default How to specify a certain character in a word?

    I am trying to create a class to get a three letter acronym from a three word phrase. I can get the first two initials, but I don't know how to get the third one. How do I specify it? Here is my code:
    Java Code:
    import javax.swing.*;
    public class ThreeLetterAcronym
    {
    	public static void main(String[] args)
    	{
    		String threeWords;
    		String firstWord = "";
    		String secondWord = "";
    		String thirdWord = "";
    		int x;
    		char c;
    		threeWords = JOptionPane.showInputDialog(null,
    			"Please enter three words");
    		x = 0;
    		while(x < threeWords.length())
    		{
    			if(threeWords.charAt(x) == ' ')
    			{
    				firstWord = Character.toString(threeWords.charAt(0));
    				secondWord = Character.toString(threeWords.charAt(x + 1));
    				thirdWord = Character.toString(threeWords.charAt(x + 1));
    				//I don't know how to select the first character here^
    				x = threeWords.length();
    			}
    			++x;
    		}
    		JOptionPane.showMessageDialog(null,
    		firstWord + secondWord + thirdWord);
    	}
    }
    Last edited by Norm; 12-31-2011 at 04:44 PM. Reason: added code tags

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: How to specify a certain character in a word?

    So you just want to take the first three letters?
    x, x+1, x+2 would do then?

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default Re: How to specify a certain character in a word?

    three letter acronym from a three word phrase.
    Can you give an example?
    Your String scanning code does not look right.
    If you must do your own scanning instead of using a java method, then you need to do some debugging with your code to see what it is doing. Add some printlns to it that show the value of all the variables you are using. For example print out the values of firstword, secondword and x inside of the loop.
    Also print out the value of the String entered by the user.

  4. #4
    gabrielpr12 is offline Member
    Join Date
    Nov 2011
    Posts
    41
    Rep Power
    0

    Default Re: How to specify a certain character in a word?

    I tried using x + 2 but it gives me the second letter of the second word. I have debugged the code several times, it executes fine with no errors, just not getting me the third letter on the acronym. The problem I have is identifying the letter after the second "space". How do I do that?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default Re: How to specify a certain character in a word?

    The problem I have is identifying the letter after the second "space".
    What variable has the index of the "second space"? Are you assuming that there is only one space between words?

    What is your algorithm for finding the beginning of the next word?
    If you start with at the beginning of a word and scan to the end of the word, then scan over the intervening spaces, the next non-space would be the beginning of the next word.

  6. #6
    gabrielpr12 is offline Member
    Join Date
    Nov 2011
    Posts
    41
    Rep Power
    0

    Default Re: How to specify a certain character in a word?

    Quote Originally Posted by Norm View Post
    What variable has the index of the "second space"? Are you assuming that there is only one space between words?

    What is your algorithm for finding the beginning of the next word?
    If you start with at the beginning of a word and scan to the end of the word, then scan over the intervening spaces, the next non-space would be the beginning of the next word.
    My bad, it should be the space after the second word, how do I do that?

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default Re: How to specify a certain character in a word?

    the space after the second word
    What is your algorithm for finding the ending of a word?
    If you start with at the beginning of the second word and scan to the end of that word, then that would be the location of the space after the second word.

  8. #8
    gabrielpr12 is offline Member
    Join Date
    Nov 2011
    Posts
    41
    Rep Power
    0

    Default Re: How to specify a certain character in a word?

    Quote Originally Posted by Norm View Post
    What is your algorithm for finding the ending of a word?
    If you start with at the beginning of the second word and scan to the end of that word, then that would be the location of the space after the second word.
    I am new to Java, how do I put that on code?

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default Re: How to specify a certain character in a word?

    I am new to Java
    New to Java or new to programming?
    Before you write any code, you need a design or algorithm that describes the steps that the code needs to take to solve the problem. Once you have that, then you can write the code.
    My last two posts have asked you what the steps your code needs to take to find a location in the String that you are scanning. Have you worked that out yet?

    Make a list of the steps in pseudo code that the code needs to do to find the location you are looking for.

  10. #10
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: How to specify a certain character in a word?

    Quote Originally Posted by Norm View Post
    Make a list of the steps in pseudo code that the code needs to do to find the location you are looking for.
    I think his effort has shown some program design but he just needs a little direction.

    I think the key problem with the design is that the output can be overwritten before the loop has ended.

    The only reason the first letter is not overwritten is because you've fixed the value to charAt index 0.

    The next step for you should be to work out some more logic so that once you've got a result you don't lose it.

    First of all we know the first letter is always char index 0 so lets take that out of the loop.
    Java Code:
    String output = "" + threewords.charAt(0);
    while (...) {
        ...
    }
    System.out.println(output);
    Now all we have to do is add 2 more char letters to the string to output
    We know that a space occurs twice (before word 2 and word 3)
    So whenever we find a space, we need to add the next char (x+1) to the output
    Java Code:
    while (...) {
        if (we find a space)
            output += next char; //charAt(x+1)
    }
    Because we know that we only need 3 letters, we can stop the loop as soon as we get them,
    rather than waiting for it to go through the entire threewords string:
    Java Code:
    if (we find a space) {
        output += next char;
        if (output.length() >= 3)
            break; //end loop
    }
    Hope this helps

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default Re: How to specify a certain character in a word?

    whenever we find a space, we need to add the next char (x+1) to the output
    What if there is more than one space between the words?

    we know that we only need 3 letters,
    What if you do not know the number of words?

  12. #12
    gabrielpr12 is offline Member
    Join Date
    Nov 2011
    Posts
    41
    Rep Power
    0

    Default Re: How to specify a certain character in a word?

    Quote Originally Posted by Norm View Post
    New to Java or new to programming?
    Before you write any code, you need a design or algorithm that describes the steps that the code needs to take to solve the problem. Once you have that, then you can write the code.
    My last two posts have asked you what the steps your code needs to take to find a location in the String that you are scanning. Have you worked that out yet?

    Make a list of the steps in pseudo code that the code needs to do to find the location you are looking for.
    I am new to programming and everything that goes along with it. What is pseudo code?

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default Re: How to specify a certain character in a word?

    Try google for pseudo code.
    Basically its a list of steps or instructions that detail the steps to do a task.
    The steps should be as simple as possible.
    A silly example: How to open a door:
    Extend your hand
    grasp the door knob
    twist the door knob until the latch disengages.
    pull/or push the door far enough that you can walk thru.

  14. #14
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: How to specify a certain character in a word?

    Quote Originally Posted by Norm View Post
    What if there is more than one space between the words?


    What if you do not know the number of words?
    Those are obviously issues that he should worry about except that according to his original post he wanted to write a simple program to take 3 words only, separated by a single space and get the first letter. Once he got it up and running and tested it i'm sure he would've been able to identify those issues and work out the rest.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default Re: How to specify a certain character in a word?

    i'm sure he would've been able
    Sounds a bit optimistic.

  16. #16
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: How to specify a certain character in a word?

    Quote Originally Posted by Norm View Post
    Sounds a bit optimistic.
    You're right, that was optimistic. And realistically speaking, it is imperative that we do not allow him to create such a program with such bugs, because the thousands of vendors dying to integrate his innovative 3 string word to 3 char abbreviation software solution into their products would have been so disappointed. He would have had to come back to this forum and double-posted for a quick response and that would have been dreadful :(.

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default Re: How to specify a certain character in a word?

    I guess now we have to wait and see if the OP comes back after the holiday.
    Happy new year
    ozzyman likes this.

Similar Threads

  1. Replies: 3
    Last Post: 08-30-2011, 04:44 PM
  2. How to read the word character in image file.
    By sureshsoftengg in forum Java Software
    Replies: 1
    Last Post: 09-21-2010, 03:37 PM
  3. read the word character in image file
    By sureshsoftengg in forum Advanced Java
    Replies: 2
    Last Post: 09-21-2010, 03:36 PM
  4. [SOLVED] Help on Word and Character counting in java
    By Alistair in forum New To Java
    Replies: 2
    Last Post: 05-15-2008, 03:48 AM
  5. reading text character by character
    By bugger in forum New To Java
    Replies: 2
    Last Post: 11-09-2007, 08:54 PM

Posting Permissions

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