Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 48
  1. #21
    stringkilla is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    no. sorry but your comments do not seem logical to me.

    i am under the influence that my code (posted in comment #15) SHOULD print "ab" IF i type "ab" when prompted to "Enter a sentence"

    now, if i run the program (after your suggested code amendment in post #18 is implemented) and type "ab", i get this:

    Enter a sentence:
    ab
    [a]


    notice the "[a]" being printed instead of my DESIRED output string of "ab".

    as for you responding to my help request as: "If that's not what you want you should change your program code." this is really not helping, as i am asking for help to do just that.
    Last edited by stringkilla; 09-13-2010 at 03:37 AM.

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

    Default

    Can you explain what the following statement is supposed to be doing?
    if(current == 'a' && current+1 == 'b')
    As JosAH says, this makes no logical sense. The second part of the condition will always be true if the first part is. Did you intend to look at the next char in the String: input?

  3. #23
    stringkilla is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    yes norm i did intend to look at the next char in the String: input.

    i am intending it to do this:

    if char at position x is "a", and the next char preceding "a" is "b" then output becomes "" plus "ab" or in other words, output becomes "ab" if the user types "ab" making the if statement true.

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

    Default

    if char at position x is "a", and the next char preceding "a" is "b"
    I assume you mean: if char at position x is "a", and the next char following "a" is "b"
    In your code you get the char at location count by this: 'current = input.charAt(count);'
    So using the same method you need to get the char at count+1
    Be sure to test if the String: 'input' is long enough that there is a character at count+1

  5. #25
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,367
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by stringkilla View Post
    as for you responding to my help request as: "If that's not what you want you should change your program code." this is really not helping, as i am asking for help to do just that.
    I get the impression that you want us to do the code for you because you completely ignored my hint about that silly if-condition. You shouldn't ignore anything at all if you want the help you get to be useful.

    kind regards,

    Jos

  6. #26
    stringkilla is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    this is all i can come up with and am having no luck. any more suggestions?

    for (int count = 0; count <length; count++)
    {
    current = input.charAt(count);
    if(current == 'a' && (current+1) == 'b')
    {
    output = output + current;
    System.out.print(output);

  7. #27
    stringkilla is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    didnt get the hint either sorry :(

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

    Default

    if(current == 'a' && (current+1) == 'b')
    That's exactly the same as the previous code with the exception of the added ()s
    Where is the character you want to look at next?
    How do you get the character that is in the 'current' variable?
    Look at the code that gets the value of 'current'.
    You want to get another character from the same source(the String 'input'), but one column over.

  9. #29
    stringkilla is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    if(current == 'a' && input.charAt(count+1) == 'b') ???
    doesnt seem to do it.

  10. #30
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,367
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by stringkilla View Post
    if(current == 'a' && input.charAt(count+1) == 'b') ???
    doesnt seem to do it.
    Better already but don't start guessing; as Norm already indicated be careful with your loop boundaries.

    kind regards,

    Jos

  11. #31
    stringkilla is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    for (int count = 0; count <length; count++)
    {
    current = input.charAt(count);
    if(current == 'a')
    (
    output = output + curent;
    )

    current = input.charAt(count+1);
    if(current == 'b')
    (
    output = output + curent;
    System.out.print(output);
    )
    }

    this is not a guess, im just trying to do what you suggest and put something outside the boundaries. again, not what i want.

    i only want 'ab' to be printed only if 'b' is the next letter after 'a'.

  12. #32
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,367
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by stringkilla View Post
    for (int count = 0; count <length; count++)
    {
    current = input.charAt(count);
    if(current == 'a')
    (
    output = output + curent;
    )

    current = input.charAt(count+1);
    if(current == 'b')
    (
    output = output + curent;
    System.out.print(output);
    )
    }

    this is not a guess, im just trying to do what you suggest and put something outside the boundaries. again, not what i want.

    i only want 'ab' to be printed only if 'b' is the next letter after 'a'.
    For one thing: this doesn't even compile (parentheses instead of curly brackets); think what would happen if count == length-1; you'd get an out of bounds exception when you try to fetch the character at position count+1.

    kind regards,

    Jos

    ps. and please put your code in [code] ... [/code] tags for readability reasons.

  13. #33
    stringkilla is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    i can understand this but cannot understand how this helps my code. and im sorry but i dont know how to post code properly.

    think what would happen if count == length-1; you'd get an out of bounds exception when
    you try to fetch the character at position count+1

    for (int count = 0; count <length; count++)
    {
    current = input.charAt(count);
    if(current == 'a' && current+1 == 'b')
    {
    output = output + current;
    System.out.print(output);
    }
    }

  14. #34
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,367
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by stringkilla View Post
    i can understand this but cannot understand how this helps my code. and im sorry but i dont know how to post code properly.

    think what would happen if count == length-1; you'd get an out of bounds exception when
    you try to fetch the character at position count+1

    for (int count = 0; count <length; count++)
    {
    current = input.charAt(count);
    if(current == 'a' && current+1 == 'b')
    {
    output = output + current;
    System.out.print(output);
    }
    }
    Any particular reason why you changed your code back again to that silly if-condition? You already know that it doesn't work that way.

    kind regards,

    Jos

    ps. before you start typing your code type [code] and after you've finished typing your code type [/code]. There's also a 'Preview Post' button so you can see the result of what you did without posting (yet).

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

    Default

    Try debugging your code by using some println() statements. For example you said
    if(current == 'a' && input.charAt(count+1) == 'b') ???
    doesnt seem to do it.
    Add the following before the above if:
    System.out.println("current=" + current + ", nextChar=" + input.charAt(count+1) + "<"); // show values

    This will show the two characters that you are working with.

  16. #36
    stringkilla is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    Java Code:
    if (!(curr_char == prev_char || vowels.indexOf(curr_char)>= 0) || (intpunct.indexOf(curr_char)>= 0))
    {
    	input2 = input1.substring(i,i+1);
    	output = output.concat(input2);
    }
    if ((vowels.indexOf(curr_char)>= 0) && (input1.charAt(i+1)==' '))
    {
    	input2 = input1.substring(i,i+1);
    	output = output.concat(input2);
    }
    why do i get this? (and its only when i start or end the input string witha vowel.

    Please enter a Sentence
    like i
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind
    ex out of range: 6
    at java.lang.String.charAt(Unknown Source)
    at assignment4.main(assignment4.java:39)
    Last edited by stringkilla; 09-06-2010 at 11:43 AM.

  17. #37
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,367
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by stringkilla View Post
    Java Code:
    if (!(curr_char == prev_char || vowels.indexOf(curr_char)>= 0) || (intpunct.indexOf(curr_char)>= 0))
    {
    	input2 = input1.substring(i,i+1);
    	output = output.concat(input2);
    }
    why do i get this? (and its only when i start or end the input string witha vowel.
    If the character at position i is a vowel and i is the last valid index value in the String I can imagine that looking at position i+1 or further would throw that Exception ...

    kind regards,

    Jos

  18. #38
    stringkilla is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    Java Code:
    if ((vowels.indexOf(curr_char)>= 0) && (input1.charAt(i+1)==' '))
    	{
    		input2 = input1.substring(i,i+1);
    		output = output.concat(input2);
    	}
    it is this that is causing the error and im not sure of what to change it to. im aiming for this; if current char is a vowel and next char is a ' ', add both to output.

    and suggestions?

  19. #39
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,367
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by stringkilla View Post
    Java Code:
    if ((vowels.indexOf(curr_char)>= 0) && (input1.charAt(i+1)==' '))
    	{
    		input2 = input1.substring(i,i+1);
    		output = output.concat(input2);
    	}
    it is this that is causing the error and im not sure of what to change it to. im aiming for this; if current char is a vowel and next char is a ' ', add both to output.

    and suggestions?
    Yep, check if there actually is a next char ...

    kind regards,

    Jos

  20. #40
    stringkilla is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    Java Code:
    if ((vowels.indexOf(prev_char)>= 0) && (input1.charAt(i)==' '))
    	{
    		input2 = input1.substring(i,i+1);
    		output = output.concat(input2);
    	}
    this doesnt work either?

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. Image Manipulation
    By sh100 in forum New To Java
    Replies: 0
    Last Post: 11-03-2009, 10:18 AM
  2. Image manipulation
    By spike72 in forum Java 2D
    Replies: 1
    Last Post: 08-28-2009, 08:45 PM
  3. Array manipulation
    By Ms.Ranjan in forum New To Java
    Replies: 9
    Last Post: 07-18-2008, 09:10 PM
  4. String manipulation example (Title case)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-29-2008, 09:04 AM
  5. String Manipulation Task
    By hiranya in forum New To Java
    Replies: 1
    Last Post: 11-19-2007, 11:07 AM

Posting Permissions

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