Results 21 to 40 of 48
- 08-27-2010, 12:58 PM #21
Member
- Join Date
- Aug 2010
- Posts
- 32
- Rep Power
- 0
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.
- 08-27-2010, 01:18 PM #22
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?
- 08-27-2010, 01:40 PM #23
Member
- Join Date
- Aug 2010
- Posts
- 32
- Rep Power
- 0
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.
- 08-27-2010, 01:46 PM #24
I assume you mean: if char at position x is "a", and the next char following "a" is "b"if char at position x is "a", and the next char preceding "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
- 08-27-2010, 01:55 PM #25
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,422
- Blog Entries
- 7
- Rep Power
- 17
- 08-27-2010, 01:57 PM #26
Member
- Join Date
- Aug 2010
- Posts
- 32
- Rep Power
- 0
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);
- 08-27-2010, 02:02 PM #27
Member
- Join Date
- Aug 2010
- Posts
- 32
- Rep Power
- 0
didnt get the hint either sorry :(
- 08-27-2010, 02:06 PM #28
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.
- 08-27-2010, 02:14 PM #29
Member
- Join Date
- Aug 2010
- Posts
- 32
- Rep Power
- 0
if(current == 'a' && input.charAt(count+1) == 'b') ???
doesnt seem to do it.
- 08-27-2010, 02:18 PM #30
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,422
- Blog Entries
- 7
- Rep Power
- 17
- 08-27-2010, 02:42 PM #31
Member
- Join Date
- Aug 2010
- Posts
- 32
- Rep Power
- 0
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'.
- 08-27-2010, 02:48 PM #32
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,422
- Blog Entries
- 7
- Rep Power
- 17
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.
- 08-27-2010, 03:10 PM #33
Member
- Join Date
- Aug 2010
- Posts
- 32
- Rep Power
- 0
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);
}
}
- 08-27-2010, 03:15 PM #34
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,422
- Blog Entries
- 7
- Rep Power
- 17
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).
- 08-27-2010, 03:30 PM #35
Try debugging your code by using some println() statements. For example you said
if(current == 'a' && input.charAt(count+1) == 'b') ???
Add the following before the above if:doesnt seem to do it.
System.out.println("current=" + current + ", nextChar=" + input.charAt(count+1) + "<"); // show values
This will show the two characters that you are working with.
- 09-06-2010, 11:39 AM #36
Member
- Join Date
- Aug 2010
- Posts
- 32
- Rep Power
- 0
why do i get this? (and its only when i start or end the input string witha vowel.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); }
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.
- 09-06-2010, 12:24 PM #37
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,422
- Blog Entries
- 7
- Rep Power
- 17
- 09-06-2010, 12:47 PM #38
Member
- Join Date
- Aug 2010
- Posts
- 32
- Rep Power
- 0
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.Java Code:if ((vowels.indexOf(curr_char)>= 0) && (input1.charAt(i+1)==' ')) { input2 = input1.substring(i,i+1); output = output.concat(input2); }
and suggestions?
- 09-06-2010, 12:49 PM #39
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,422
- Blog Entries
- 7
- Rep Power
- 17
- 09-06-2010, 01:04 PM #40
Member
- Join Date
- Aug 2010
- Posts
- 32
- Rep Power
- 0
Similar Threads
-
Image Manipulation
By sh100 in forum New To JavaReplies: 0Last Post: 11-03-2009, 10:18 AM -
Image manipulation
By spike72 in forum Java 2DReplies: 1Last Post: 08-28-2009, 08:45 PM -
Array manipulation
By Ms.Ranjan in forum New To JavaReplies: 9Last Post: 07-18-2008, 09:10 PM -
String manipulation example (Title case)
By Java Tip in forum Java TipReplies: 0Last Post: 01-29-2008, 09:04 AM -
String Manipulation Task
By hiranya in forum New To JavaReplies: 1Last Post: 11-19-2007, 11:07 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks