Exit Problem
Author |
Message |
Caceres
|
Posted: 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
code: | /*Assignment 3.2 A
Submitted By: Tyler Caceres
July 08, 2006*/
/* The purpose of this program is to check a word to see if it is a palindrome;
a word that is spelt the same backwards and frontware. The program will also ask
if another word needs to be checked before exiting the program.*/
import TerminalIO.KeyboardReader;
public class ass32a {
public static void main(String[]args) {
KeyboardReader reader = new KeyboardReader();
String word; //variable for the word being entered
int count; //length of word
String pal=""; //variable for the reverse form of integer word
char yn;
String word_two="";
String pal_two="";
int count_two;
System.out.println("This program shall compute if the word you have entered is a palindrome.\n");
System.out.println("A palindrome is a word that is spelt the same backwards and frontwards.\n");
System.out.print("Please enter a word: ");
word = reader.readLine();
System.out.print("\nThe word backwards is ");
for(pal="",count=word.length() - 1; count >=0; count--)
pal = pal + word.charAt(count);
System.out.print(pal);
System.out.print(".\n");
if(word.compareTo(pal)==0) {
System.out.print("The word, " + word + ", is a palindrome.\n");
}
else {
System.out.print("The word, " + word + ", is not a palindrome.\n");
}
System.out.println("Would you like to see if another word is a palindrome(1=yes, 2=no): \n");
yn = reader.readChar();
if(reader.readChar()<=1) {
System.out.print("Good bye.\n");
return;
}
else {
System.out.print("Please enter a word: \n");
word_two = reader.readLine();
System.out.print("\nThe word backwards is ");
for(pal_two="",count_two=word_two.length() - 1; count_two >=0; count_two--)
pal_two = pal_two + word_two.charAt(count_two);
System.out.print(pal_two);
System.out.print(".\n");
if(word_two.compareTo(pal_two)==0)
System.out.print("The word, " + word_two + ", is a palindrome.\n");
else
System.out.print("The word, " + word_two + ", is not a palindrome.\n"); }
}
} |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
Posted: Mon Jul 10, 2006 9:27 am Post subject: (No subject) |
|
|
First off, let's format this properly so it's readable. Oh, and I cleaned up your use of print and println.
code: | import TerminalIO.KeyboardReader;
public class Assignment32A {
public static void main(String[]args) {
KeyboardReader reader = new KeyboardReader();
String word;
int count;
String pal = "";
char yn;
String word_two = "";
String pal_two = "";
int count_two;
System.out.println("This program shall compute if the word you have" +
" entered is a palindrome.");
System.out.println();
System.out.println("A palindrome is a word that is spelt the same" +
"backwards and frontwards.");
System.out.println();
System.out.print("Please enter a word: ");
word = reader.readLine();
System.out.println();
System.out.print("The word backwards is ");
for (pal = "", count = word.length() - 1; count >= 0; count--)
pal = pal + word.charAt(count);
System.out.println(pal + ".");
if (word.compareTo(pal) == 0) {
System.out.println("The word, " + word + ", is a palindrome.");
}
else {
System.out.println("The word, " + word + ", is not a palindrome.");
}
System.out.println("Would you like to see if another word is" +
" a palindrome(1=yes, 2=no): ");
System.out.println();
yn = reader.readChar();
if (reader.readChar() <= 1) {
System.out.println("Good bye.");
return;
}
else {
System.out.println("Please enter a word: ");
word_two = reader.readLine();
System.out.println();
System.out.print("The word backwards is ");
for (pal_two = "", count_two = word_two.length() - 1;
count_two >= 0; count_two--)
pal_two = pal_two + word_two.charAt(count_two);
System.out.println(pal_two + ".");
if (word_two.compareTo(pal_two) == 0)
System.out.println("The word, " + word_two +
", is a palindrome.");
else
System.out.println("The word, " + word_two +
", is not a palindrome.");
}
}
} |
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? |
|
|
|
|
|
Caceres
|
Posted: Mon Jul 10, 2006 10:11 am Post subject: (No 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 |
|
|
|
|
|
Caceres
|
Posted: Mon Jul 10, 2006 10:12 am Post subject: (No 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 |
|
|
|
|
|
Caceres
|
Posted: Mon Jul 10, 2006 10:13 am Post subject: (No subject) |
|
|
+23 bits |
|
|
|
|
|
wtd
|
Posted: Mon Jul 10, 2006 10:18 am Post subject: (No subject) |
|
|
http://java.sun.com/docs/codeconv/
Do you understand the relationship between characters and integers in Java? |
|
|
|
|
|
Caceres
|
Posted: Mon Jul 10, 2006 12:38 pm Post subject: (No 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 |
|
|
|
|
|
wtd
|
Posted: Mon Jul 10, 2006 12:41 pm Post subject: (No subject) |
|
|
Oh, I know what the problem is.
You get a character from the user, but then you compare it to an integer. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Caceres
|
Posted: Mon Jul 10, 2006 8:11 pm Post subject: (No subject) |
|
|
Okay, I've got the program to work perfectly now.
code: | /*Assignment 3.2 A
Submitted By: Tyler Caceres
July 08, 2006*/
/* The purpose of this program is to check a word to see if it is a palindrome;
a word that is spelt the same backwards and frontware. The program will also ask
if another word needs to be checked before exiting the program.*/
import TerminalIO.KeyboardReader;
public class ass32a { //name of my file
public static void main(String[]args) {
KeyboardReader reader = new KeyboardReader();
String word; //the variable for the word that the user will enter
int count; //the variable for the word's length
String pal = ""; //the variable for the palindrome (reversed word)
char yn; //data that user inputs, either yes or no
System.out.println("This program shall compute if the word you have" +
" entered is a palindrome."); //text that user will see, states the purpose of the program
System.out.println();
System.out.println("A palindrome is a word that is spelt the same" +
"backwards and frontwards."); //state sthe definition of a palindrome
System.out.println();
System.out.println("Would you like to enter a word? (y=yes,n=no)"); //asks user if they would like to check if a word is a palindrome
yn = reader.readChar(); //the inputted data of the user(yes or no)
while(yn==89 || yn==121) { //loop information-states that while y or Y is being entered
System.out.print("Please enter a word: "); //text that user will see
word = reader.readLine(); //inputted data by the user, word that user wants to be read
for(pal = "", count = word.length() - 1; count >= 0; count--) {
pal = pal + word.charAt(count);
/*this for loop reverses the word that the user entered.
It gives the value to the variable "pal" */
}
if(word.compareTo(pal) == 0)
/* An if statement. States that if the original word that the user entered
is the same as the reversed word (variable pal), then the following statement will be
received by the user. */
System.out.println("The word, " + word + ", is a palindrome.");
else
/* If the word is not the same as the variable pal (reversed word), then
this is the statement that the user will see. */
System.out.println("The word, " + word + ", is not a palindrome.");
System.out.println("Would you like to enter another word? (y=yes,n=no)");
// asks user if they would like to check if a world is a palindrome
yn = reader.readChar(); //the inputted data of the user(yes or no)
} //finished loop
System.out.println("Good bye.");
/*text that user will see, when they do not wish to enter another word-when user exits program*/
}
} |
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 |
|
|
|
|
|
wtd
|
Posted: Mon Jul 10, 2006 10:18 pm Post subject: (No subject) |
|
|
Way more work than you need to be doing. |
|
|
|
|
|
Caceres
|
Posted: Mon Jul 10, 2006 10:51 pm Post subject: (No subject) |
|
|
wtd wrote:
Way more work than you need to be doing.
I couldn't think of another way. Lol. |
|
|
|
|
|
wtd
|
Posted: Mon Jul 10, 2006 11:32 pm Post subject: (No subject) |
|
|
Perhaps with character literals? |
|
|
|
|
|
|
|