Computer Science Canada Exit Problem |
Author: | Caceres [ Mon Jul 10, 2006 12:03 am ] | ||
Post subject: | Exit Problem | ||
I can't seem this program to exit properly. I'm not sure of the code to get a program to exit. But the main purpose is after the user enters their first word, they have a choice to enter a second word. If they type in 2, the program should exit. But it is not working for some reason. Any suggestions? Thanks. ~Caceres
|
Author: | wtd [ Mon Jul 10, 2006 9:27 am ] | ||
Post subject: | |||
First off, let's format this properly so it's readable. Oh, and I cleaned up your use of print and println.
You should maintain properformatting as you code. It makes things easier on yourself. Now, as for your question... You read in a character, and then you compare it to an integer. Do you understand the relationship between the two? |
Author: | Caceres [ Mon Jul 10, 2006 10:11 am ] |
Post subject: | |
That's for the proper formating. And um.. By comparing it to an integer. Do you mean I should put something like if(word = pal) Comparing the world with the reversed word? ![]() ~Caceres |
Author: | Caceres [ Mon Jul 10, 2006 10:12 am ] |
Post subject: | |
And..I was just wondering if there was a thread that speaks about proper formmating? Because I thought the way I did it was right, but I guess i've been doing it wrong all along. Lol. Thanks again. ~Caceres |
Author: | Caceres [ Mon Jul 10, 2006 10:13 am ] |
Post subject: | |
+23 bits |
Author: | wtd [ Mon Jul 10, 2006 10:18 am ] |
Post subject: | |
http://java.sun.com/docs/codeconv/ Do you understand the relationship between characters and integers in Java? |
Author: | Caceres [ Mon Jul 10, 2006 12:38 pm ] |
Post subject: | |
wtd wrote: http://java.sun.com/docs/codeconv/
Do you understand the relationship between characters and integers in Java? Yes, I understand. The "is a palindrome" and is not a palindrome, works perfectly, since i used to compareTo statement. It still works with an itneger. The real problem is when i ask AGAIN for the second time if they'd like to enter another word. If they press 2(which means no), the program should exit right there on the spot. But it still asks for the word to be entered. So basically, yes and no still give a yes response. It still asks for the word. Thanks. ~Caceres |
Author: | wtd [ Mon Jul 10, 2006 12:41 pm ] |
Post subject: | |
Oh, I know what the problem is. ![]() You get a character from the user, but then you compare it to an integer. |
Author: | Caceres [ Mon Jul 10, 2006 8:11 pm ] | ||
Post subject: | |||
Okay, I've got the program to work perfectly now.
But now my teacher said Quote: sorry, forgot to add that in order to do the routine i posted above, you use the String.length() function that returns how long the string is. you need this in order to know where to start your counters. so if the word is "hello", the String.length( ) function should return the value of 5 because there are 5 characters in the word "hello". now, you just have to learn how strings are stored. if "hello" was stored in a variable st, then st[0] would contain the 'h' character, st[1] would contain the 'e' character, and so on.
And I have no clue on how to re-arrange my program to be able to do it this way. Any suggesttions? ![]() Thanks. ~Caceres |
Author: | wtd [ Mon Jul 10, 2006 10:18 pm ] | ||
Post subject: | |||
Way more work than you need to be doing. |
Author: | Caceres [ Mon Jul 10, 2006 10:51 pm ] | ||
Post subject: | |||
wtd wrote:
Way more work than you need to be doing. I couldn't think of another way. Lol. |
Author: | wtd [ Mon Jul 10, 2006 11:32 pm ] |
Post subject: | |
Perhaps with character literals? |