PDA

View Full Version : Where does my java while loop go wrong?




Whorehay
Mar 10, 2009, 12:46 AM
Been working at this part of the assignment for over 12 hours in a span 3 days now. Just can't get it to work. Not due for another 2 days so not too worried (probably will go to office hours tomorrow). But I thought I'd take one last crack at it before I take it in.

For this part of the assignment, we gotta capitalize every every first letter of the word and lowercase everything else using while loops, and ASCII numbers.

I feel like I'm sorta close, but not close enough to get a substantial reading from the output.

When I enter: "hello its whorehay"
my output is: "HHHHHHHHHHHHHHHHHH"
i'm assuming that the number of h's corresponds to the length of the string.. i'm stuck!

any hints? don't need to shout out the answer since i get a rush when i can figure it out by myself, but any hints toward the right direction would be greatly appreciated. thanks!



dejo
Mar 10, 2009, 12:51 AM
Don't you wanna update position in here?:
} else { // or else
first = false; // set to false
}


Also, rather than increment inside each case, increment once at the end of the while block. Or better yet, use a for loop.

Whorehay
Mar 10, 2009, 01:03 AM
when i put position++ after what you mentioned, i get the same output. i had it there before but i started playing around with the code after it didn't work. this code is just what i had last (might not be the best).

so it'd be something like?

for (position = 0; position < length; position++);

hmm i may have to try that later

dejo
Mar 10, 2009, 01:05 AM
when i put position++ after what you mentioned, i get the same output. i had it there before but i started playing around with the code after it didn't work
Wait. Upon closer examination, you sure you don't want that second "if" to be an "else if"?

Whorehay
Mar 10, 2009, 01:15 AM
Wait. Upon closer examination, you sure you don't want that second "if" to be an "else if"?


if (first == false) { // first is false

if you are talking about that line.. i couldn't figure out how to make it an else/else if without a syntax error. (else without if error). played around with the brackets and still came up with the error. so i just left it as if for now so it can at least compile.

dejo
Mar 10, 2009, 01:28 AM
if you are talking about that line.. i couldn't figure out how to make it an else/else if without a syntax error. (else without if error). played around with the brackets and still came up with the error. so i just left it as if for now so it can at least compile.
if (expression1)
{
...first block of code here...
}
else if (expression2)
{
...second block of code here...
}


You're brace matching looks a little messed up. Take, for example:
if (first == true) { // boolean for if
} if ((c >= 97) && (c <= 122)) { // if it's lowercase

That's an empty if block after the () and second if: { }
I'd suggest reformatting / re-indenting your code to make it easier to match opening and closing braces. Makes it easier to read too!

Whorehay
Mar 10, 2009, 02:02 AM
haven't been able to get for loops to work:

output becomes:

"HELLOmy NAMEis WHOREHAY"

i am CLOSE though. i can feel it. thanks for the help! i appreciate it.

Whorehay
Mar 10, 2009, 02:34 AM
got it! :) :)

thanks for the bracket tip. helped me visualize the pseudo code!

Whorehay
Mar 10, 2009, 02:49 AM
damn it feels good. just organized the entire code and it looks so great that it brings a tear to my eye.

Burtonsnow9
Mar 10, 2009, 10:07 AM
Now I don't see your code anymore, but you may want to try a different approach. Make a char array and then for each char check if it is uppercase, and based on that, and the char position (where you are in the words) you can change it to upper or lower case. Use the api for more help.


P.S. - If this was in your original code then sorry.

AUFan
Mar 10, 2009, 10:20 AM
Been working at this part of the assignment for over 12 hours in a span 3 days now. Just can't get it to work. Not due for another 2 days so not too worried (probably will go to office hours tomorrow). But I thought I'd take one last crack at it before I take it in.

For this part of the assignment, we gotta capitalize every every first letter of the word and lowercase everything else using while loops, and ASCII numbers.

I feel like I'm sorta close, but not close enough to get a substantial reading from the output.

When I enter: "hello its whorehay"
my output is: "HHHHHHHHHHHHHHHHHH"
i'm assuming that the number of h's corresponds to the length of the string.. i'm stuck!

any hints? don't need to shout out the answer since i get a rush when i can figure it out by myself, but any hints toward the right direction would be greatly appreciated. thanks!

You could simplify the problem greatly if you break it down into smaller subsections and tackle those. Perhaps use something like substrings and divide all your words up then tackle it that way instead of trying to cram everything through at once.

Whorehay
Mar 10, 2009, 01:01 PM
You could simplify the problem greatly if you break it down into smaller subsections and tackle those. Perhaps use something like substrings and divide all your words up then tackle it that way instead of trying to cram everything through at once.

that's how i solved it. i split the assignment into two sections, the counting of the characters and the printing of the changed sentence. :) haven't learned about arrays yet! still in intro to programming.

AUFan
Mar 10, 2009, 01:24 PM
Glad to hear you were able to solve it.