Matthew
Matthew

Reputation: 35

Why won't it recognize my method?

import java.lang.String;
public class Word
{
    /** 
     * constructs a Word with String value s
     * @param s is string value of Word
     */
    public Word(String s)
    {
        original = s;
    }

    /**
     * reverses letters in original string 
     * @return a string that is a reverse of original
     */
    public String reverse()
    {
        String temp = original;
        String areverse = "";
        int x;
        for (x = temp.length() ; x>0 || x==0 ; x -- )
        {   
            areverse = temp.substring(x);
        }
        return areverse;
    }

    /**    
     * determines is word is a palindrome
     * @return true if word is a palindrome, false otherwise
     */
    public boolean isPalindrome()
    {
        boolean flag = false;
        String temp = original;
        if (temp.equals(temp.reverse()))
            flag = true;
        return flag;

    }

    /** 
     * Alternate method to determine if word is a palindrome
     * @return true if word is a palindrome, false otherwise    
     */
    public boolean isPalindrome2()
    { 
        String temp = original;
        int x = temp.length();
        boolean flag = false;
        int y = 0;
        while (temp.subtring(y).equals(temp.substring(x)) && (x>0 || x==0))
        {
            x--;
            y++;
        }
        if (x==0)
            flag=true;
        return flag;


    }

    private String original;
}

I have to write this program that finds the reverse of a word, and determines if a word is a palindrome in two different ways. I was only given the method names and then the comments about the methods, but all the code in the methods is mine. When I use the reverse() method in the first palindrome method, bluej tells me it cannot find the variable or method 'reverse', even though I define it earlier in the code. What's my problem? thanks

Upvotes: 0

Views: 89

Answers (3)

FacelessTiger
FacelessTiger

Reputation: 89

The problem is that you're setting temp to a string, however the reverse method is not inside the string class but rather your class but you're trying to find it in the string when you did

temp.reverse();

You can get around this by making the reverse method take in a string AND return a string, the string it takes in is what it's reversing and the return is the reversed string.

public String reverse(String string)

Then you call the method in YOUR class

if (temp.equals(reverse(temp)))
    flag = true;

So the new reverse method would look like

public String reverse(String string)
{
    String areverse = "";
    for (int x = string.length(); x>0; x--)
    {   
        areverse += string.charAt(x - 1);
    }
    return areverse;
}

Upvotes: 0

kimy82
kimy82

Reputation: 4465

You should be using

new StringBuilder(original).reverse().toString()

in order to get the reverse. The method reverse doesn´t exist in String type.

Upvotes: 0

Stefan
Stefan

Reputation: 1730

You are invoking reverse on the String object "temp". You have defined the reverse-method in the Word class, so you need to invoke it on a Word object.

Upvotes: 1

Related Questions