• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Java Help Needed!!!

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!
 
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?
 
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
 
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, 😀
 
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, 😀

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.
 
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.
 
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?
 
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
 
That out of bounds error means your pointer (yes, java does have pointers) is pointing somewhere outside an array.
 
for (int i = 0 ; i < strCheck.length()-1 ; i ++) {

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

}

return false;
 
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.
 
It needs to be
while(numTimes <= strCheck.lentgh() - 1)

If it's <, it will not check the last letter in the string.
 
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.
 
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 🙂
 
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! 😀 :beer: 🙂
 
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! 😀 :beer: 🙂

Sorry for being a direct pain in the arse by offering an indirect answer 🙂
 
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! 😀 :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:
 
Back
Top