Java Help Needed!!!

AgaBoogaBoo

Lifer
Feb 16, 2003
26,108
5
81
I need some help soon so I can continue working on my CS assignment, basically it has to check in a word if there are two letters next to each other that are the same. I have to file, one is the file with the main method and another with some other methods. Right now only the one actually checking for the repetition of two letters is under concern. Basically, its looking for words like MOON and SOCCER not SUN and HOCKEY.

Here's the code for the method:

public static boolean checkCooney(String strCheck)
{
int numTimes = 0;
boolean boolCheck = false;
while(numTimes <= strCheck.length())
{
if(String.valueOf(strCheck.charAt(numTimes)) == String.valueOf(strCheck.charAt(numTimes+1)))
{

numTimes = strCheck.length() + 1;
return boolCheck = true;
}
if (numTimes == strCheck.length())
{
return boolCheck = false;
}
numTimes++;
}
return boolCheck;
}

Here is where it is returned:

boolCheck = cooneyRoutines.checkCooney(strInput);

if(!boolCheck)
{
System.err.println("No, Cooney doesn't like it");
break;
}
if(boolCheck)
{
System.err.println("Yes, Cooney likes it");
break;
}


numTimes++;

When I run it, I get a string out of range error. I have narrowed it down to an error in either the 1st if statement of the method called or the boolCheck = line in the second quotation.

Mods: I really need an answer soon! Due tomorrow!
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
Originally posted by: AgaBooga
Someone here who knows Java must be browsing...

Chill dude! Do you really need 3 bumps in a few minutes after posting in the wrong forum in the first place?

Why not spend this time debugging your code?
 

AgaBoogaBoo

Lifer
Feb 16, 2003
26,108
5
81
Originally posted by: Descartes
Originally posted by: AgaBooga
Someone here who knows Java must be browsing...

Chill dude! Do you really need 3 bumps in a few minutes after posting in the wrong forum in the first place?

Why not spend this time debugging your code?

dp
 

AgaBoogaBoo

Lifer
Feb 16, 2003
26,108
5
81
Originally posted by: Descartes
Originally posted by: AgaBooga
Someone here who knows Java must be browsing...

Chill dude! Do you really need 3 bumps in a few minutes after posting in the wrong forum in the first place?

Why not spend this time debugging your code?

I am, sorry about so many bumps, but I'm kind of worried... sorry, I'll calm it down, :D
 

hjo3

Diamond Member
May 22, 2003
7,354
4
0
How much will you PayPal me to help? My friend's really good at Java.
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
Originally posted by: AgaBooga
Originally posted by: Descartes
Originally posted by: AgaBooga
Someone here who knows Java must be browsing...

Chill dude! Do you really need 3 bumps in a few minutes after posting in the wrong forum in the first place?

Why not spend this time debugging your code?

I am, sorry about so many bumps, but I'm kind of worried... sorry, I'll calm it down, :D

Good. Now, the error is in this line:

if (String.valueOf(strCheck.charAt(numTimes)) == String.valueOf(strCheck.charAt(numTimes+1)))

as you suspected. The trouble is that the length() returns the length of the string, but the charAt() expects a 0-based offset to the start of the string. numTimes will == the string length() in its final iteration which is outside the bounds of the string.
 

AgaBoogaBoo

Lifer
Feb 16, 2003
26,108
5
81
Originally posted by: hjo3
How much will you PayPal me to help? My friend's really good at Java.

I don't have the money nor a paypal account. I just need some quick help, its probably something small, not hard or long. If you could help me, please do.
 

JetBlack69

Diamond Member
Sep 16, 2001
4,580
1
0
while(numTimes <= strCheck.length())
{
if(String.valueOf(strCheck.charAt(numTimes)) == String.valueOf(strCheck.charAt(numTimes+1)))
{

I believe that is your problem. Try MOON( .length() should be 4) It compares "MO", "OO", "ON", "N ", " " It's running out of things to compare. Also, what if the input has even number of characters? what about odd?
 

Jzero

Lifer
Oct 10, 1999
18,834
1
0
That top code block has too many returns. Return once at the end. set boolCheck to True or False as needed, then at the end of the method, return it.

But I think the issue is this:
At your last iteration, numTimes will be equal to the last letter in the string, so the first if statement will compare the last letter of the string to the next letter in the string which does not exist.

Since the last letter implicitly checked by the second-to-last letter, you need to give up before numTimes equals strCheck.length
 

Gibson486

Lifer
Aug 9, 2000
18,378
2
0
That out of bounds error means your pointer (yes, java does have pointers) is pointing somewhere outside an array.
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
for (int i = 0 ; i < strCheck.length()-1 ; i ++) {

if(strCheck.charAt(i)==strCheck.charAt(i+1))
return true;

}

return false;
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
Originally posted by: Gibson486
That out of bounds error means your pointer (yes, java does have pointers) is pointing somewhere outside an array.

Java doesn't have pointers; Java has references. Also, in his case he's not even referencing anything outside of the array, because numTimes is simply an integer, and the 1 he's adding to it isn't an offset resulting in the out-of-bounds reference; it's the charAt() method doing it.
 

Jzero

Lifer
Oct 10, 1999
18,834
1
0
It needs to be
while(numTimes <= strCheck.lentgh() - 1)

If it's <, it will not check the last letter in the string.
 

Gunslinger08

Lifer
Nov 18, 2001
13,234
2
81
Originally posted by: Jzero
It needs to be
while(numTimes <= strCheck.lentgh() - 1)

If it's <, it will not check the last letter in the string.

It shouldn't check the last letter in the string.. because the algorithim references numTimes + 1, which will give out of bounds.
 

Jzero

Lifer
Oct 10, 1999
18,834
1
0
Originally posted by: joshsquall
Originally posted by: Jzero
It needs to be
while(numTimes <= strCheck.lentgh() - 1)

If it's <, it will not check the last letter in the string.

It shouldn't check the last letter in the string.. because the algorithim references numTimes + 1, which will give out of bounds.

Yeh you're right. Confused length with index :)
 

AgaBoogaBoo

Lifer
Feb 16, 2003
26,108
5
81
Thanks all! Its solved, I used Descartes' advice and worked from there although it took a little modifying. I would have used the other suggestions you guys gave but I was busy fixing it. Thanks again! :D :beer: :)
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
Originally posted by: AgaBooga
Thanks all! Its solved, I used Descartes' advice and worked from there although it took a little modifying. I would have used the other suggestions you guys gave but I was busy fixing it. Thanks again! :D :beer: :)

Sorry for being a direct pain in the arse by offering an indirect answer :)
 

Jzero

Lifer
Oct 10, 1999
18,834
1
0
Originally posted by: Descartes
Originally posted by: AgaBooga
Thanks all! Its solved, I used Descartes' advice and worked from there although it took a little modifying. I would have used the other suggestions you guys gave but I was busy fixing it. Thanks again! :D :beer: :)

Sorry for being a direct pain in the arse by offering an indirect answer :)

You help him more by pointing him in the right direction than by just giving out the answer. :beer: