Be a Supporter!

Need help with a Java program.

  • 308 Views
  • 5 Replies
New Topic Respond to this Topic
amanda
amanda
  • Member since: Jul. 30, 2007
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Need help with a Java program. 2011-10-19 21:44:08 Reply

I'm creating a palindrome tester (a word that reads the same from left to right and right to left), and i need to create a while loop so i can check several phrases a user keys in. I can do the whole system out prints and stuff like that, but what would the loop look like to check and see if the word is a palindrome?

I'm thinking it has to do with something related to if (length == length-1) or something along the lines of (length/2) or something but i don't know how to create the part of the program that checks the characters. They also need to have equalsignoreCase() but I also don't know how to implement that in.

Thanks in advance!


Well, like, you couldn't, like, find it because, like, you're dumb, god
Follow me on twitter ;) | PM me whether you like me or hate me. 52 like | 17 hate

BBS Signature
thePalindrome
thePalindrome
  • Member since: Aug. 13, 2011
  • Offline.
Forum Stats
Member
Level 05
Game Developer
Response to Need help with a Java program. 2011-10-19 22:08:15 Reply

you can't really check what the user has entered in at the command window, but once they hit enter, you can use a string split (can't remember the function name), giving the split point as string.length /2...

liljim
liljim
  • Member since: Dec. 16, 1999
  • Offline.
Forum Stats
Staff
Level 28
Blank Slate
Response to Need help with a Java program. 2011-10-19 22:14:57 Reply

Try StringBuffer with reverse() on the word you're wanting to compare against and put the strings toLowerCase() before compare each of them.

for example:
Lol
loL (reversed)

obviously, both are going to equate to each other when put in lower case context. Use throwaway values if you don't want to store them, what matters to you is that you're seeing if one is exactly the same, regardless of case, in one way as it is the other.

liljim
liljim
  • Member since: Dec. 16, 1999
  • Offline.
Forum Stats
Staff
Level 28
Blank Slate
Response to Need help with a Java program. 2011-10-19 22:17:14 Reply

Oh and strip spaces and punctuation.

NinoGrounds
NinoGrounds
  • Member since: Nov. 28, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to Need help with a Java program. 2011-10-19 22:30:59 Reply

OMG it's Him !

gumOnShoe
gumOnShoe
  • Member since: May. 29, 2004
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to Need help with a Java program. 2011-10-19 22:35:24 Reply

This is a pretty common CS 101 problem to teach users how to loop. So while reverse will probably work, I'm not sure that's what this person's professor is after. If I'm off the mark, apologies.

You probably want to use a for loop:

for (<create variable>; <condition>; <change variable value>) {

}

When you create the variable, give it a value.
The condition should test that variable. If the condition evaluates to true, the loop happens again like in a while loop.
The increment step should change the value of the created variable.

To loop through an entire string:

for (int i = 0; i < myString.length(); i++) {
     char nextCharacter = myString.charAt(i);
}

Now, you want to ignore capital letters. So you probably want to use the toLowerCase() function on myString before looping through it. You will also want a different condition since you only want to go through half of the string. I'll leave you to think about what that condition might be.

Something to think about.

If the first character of your string is 0, your string is 9 characters long, then what is the position of the last character in the string? How could you express that using a simple math operation, the size of your string, and the value of i?

I think you should see things starting to come together.

You can test whether two characters are equal to each other wit hthe following:

char a = 'a';
char b = 'b';
if(a == b) {
     System.out.println("A equals B!");
} else {
     System.out.println("A does not equal B!");
}

I've given you all the tools you need:

java logical control:
for
if
else

String functions:
myString.toLowerCase();
myString.size();
myString.getCharAt(i);

// If you need it, user input would use a Scanner on System.in;

Newgrounds Anthology? 20,000 Word Max. [Submit]

Music? Click Sig:

BBS Signature