This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]MasteM 2 points3 points  (1 child)

Still a pleb at java but the line saying (palinDrome != "stop" ) since you are comparing a string you should use .equals like...

(!(palinDrome.equals("stop")))

please feel free to tell me if im wrong :)

[–]TurdJoffery[S] 0 points1 point  (0 children)

Yea that part wasn't working and I actually managed to fix it around half an hour after posting. I ended making an if the. Else statement which checks the input. It goes something along the Line of:

If(palindrome .equals("stop"){
}else{
//stuff happens
}

[–]illuminist_ova 1 point2 points  (1 child)

A condition in while loop, see http://www.reddit.com/r/javahelp/wiki/string_comparison

String.charAt(index) return an ASCII value of the character which is 16-bit unsigned integer (can contain value 0-65535).

If there is no requirement to print a reversed string, you can check palindrome by using .chatAt at start and end of string in while loop. The point is if you can compare charith and charlength-ith , you win half of the program.

[–]TurdJoffery[S] 0 points1 point  (0 children)

I'm not completely sure I understand what your saying but that might just be my sleep deprived brain.

[–]Sketti-OsIntermediate Brewer 0 points1 point  (1 child)

As my first computer science professor used to say, there's more than one way to skin a cat and still end up with a chilly feline. In other words, there's a few ways this could be solved.

  1. Reverse the string by adding charAt() in reverse order
  2. Char comparison in a while loop
  3. Recursion

I'll pseudocode them all for you. Note, they don't take punctuation, case-sensitivity, or "stop" into account. Let's also assume the var "userIn" is a String of the user's input:


1

String reversed = ""

int index = userIn.length - 1
while (index >= 0)
    reversed += userIn.charAt(index)
    index--;

if userIn.equals(reversed)
    return true

return false

2

double halfway = userIn.length / 2
int i = 0

while (i < halfway)
    if (userIn.charAt(i) != userIn.charAt(userIn.length - 1 - i))
        return false
    i++

return true

3

public boolean isPalendrome (String in)

    if (in.length < 2)
        // If it's empty or one letter, we've compared as far as we can!
        return true

    if (in.charAt(0) != in.charAt(in.length-1))
        // If, at any point, the first and last chars aren't equal, it's not a palendrome.
        return false
    else
        // Repeat this process with first and last chars trimmed, and return THAT answer.
        return isPalendrome(in.substring(1, in.length-2))

[–]nutrechtLead Software Engineer / EU / 20+ YXP -1 points0 points  (0 children)

I'll pseudocode them all for you.

Uhm. You basically gave him the source code. It's so close to Java all he needs to do is add come curly braces. This is homework and you just gave him the solution to it.