PDA

View Full Version : Java - Palindrom


zk0
12-12-2006, 01:35 PM
I need some help with this Java code. It's supposed to be a code that says if a text is a palindrom or not (I have no idea what's its called in english). A palindrom is a text that will be spelled the same if it's read backwards (=adolf i paris rapar sirap i floda).

This is the start code:

package your_username;

public class Step3_Lab02
{
public static void main(String[] args)
{
}
}

class Palindrome
{
public boolean isPalindrome(String str)
{
String reverseStr = (new StringBuffer(str)).reverse().toString();
return str.equalsIgnoreCase(reverseStr);
}

public String removeNoneLetters(String str)
{
String result = "";
for (int i = 0; i < str.length(); ++i)
{
if (Character.isLetter(str.charAt(i)))
{
result = result + str.charAt(i);
}
}

return result;
}
}


I am only allowed to add new code in the main-method:
public static void main(String[] args)
{
}

I've already added a Scanner to the code. It now loks like this:
package your_username;

import java.util.Scanner;

public class Step3_lab02
{

}

class Palindrome
{
public boolean isPalindrome(String str)
{
String reverseStr = (new StringBuffer(str)).reverse().toString();
return str.equalsIgnoreCase(reverseStr);
}

public String removeNoneLetters(String str)
{
String result = "";
for (int i = 0; i < str.length(); ++i)
{
if (Character.isLetter(str.charAt(i)))
{
result = result + str.charAt(i);
}
}
return result;
}
}


If you think you can help me please post! :)

nikkiH
12-12-2006, 03:02 PM
So, what's the question? You have to at least try and let us know what you're having trouble with. No one is going to do your homework FOR you. :D

Most of the code is already written here. What is the part you don't understand? Reading from an input (console, I assume) or ... ?

zk0
12-12-2006, 03:15 PM
So, what's the question? You have to at least try and let us know what you're having trouble with. No one is going to do your homework FOR you. :D

Most of the code is already written here. What is the part you don't understand? Reading from an input (console, I assume) or ... ?
What!? Isn't anyone going to do it for me!? :P

I have problem with knowing how to delete all the signs that is not letters in the text I write.

And Im a little unsure how to use the method "nextLine" in the Scanner-class.

Mink
12-12-2006, 03:35 PM
Ok the nextLine() function in the scanner class reads in all input tot he newline character. In short, it returns a String (all the next you write up until you press Enter\Return)

I am going to assume you know what objects are, so you would just create a Scanner object, and then call the nextLine() method from that object and assign the value returned to some variable. Voila, you have inputed a String from the input.


now all the palindrome code is written for you, including the function that removes the non-letters from the input.

to utilize it, you call the removeNonLetters function from a Palindrome object that you created, and then that is done for you also.

Like I said, I assume you know what objects are...if you don't, just say so and I will explain.

zk0
12-12-2006, 04:27 PM
Ok the nextLine() function in the scanner class reads in all input tot he newline character. In short, it returns a String (all the next you write up until you press Enter\Return)

I am going to assume you know what objects are, so you would just create a Scanner object, and then call the nextLine() method from that object and assign the value returned to some variable. Voila, you have inputed a String from the input.


now all the palindrome code is written for you, including the function that removes the non-letters from the input.

to utilize it, you call the removeNonLetters function from a Palindrome object that you created, and then that is done for you also.

Like I said, I assume you know what objects are...if you don't, just say so and I will explain.
Please explain further :)

Mink
12-12-2006, 05:54 PM
Ok So Objects are "instances of a class" they are what we call "reference types". This means they pass by address. But away with the Techical jargon.

First step is to declare your object. You do this like you would declare any variable

Type variableName;

so for Scanner it would be:

Scanner in;

or whatever you want to call it (doesn't have to be named "in")


Second step is called instantiation. this is where you create the object. You do this by allocating memory dynamically. We have a specially operator to do this for us. it is called the new operator. new operator takes one operand, which is the call to the Class constructor. Scanner's constructor takes in an InputStream, since we are using standard input, we will give it that input stream:

//instantiate the Scanner object with standard input
in = new Scanner(System.in);

now the Scanner object is declared and instantiated, it is ready for use.

lets say you want to read in a line of text from the keyboard using the nextLine method. You call methods from objects using the . operator:

//read a line from standard input
String input = in.nextLine();


Ok so quick review for creating objects. First you declare the variable, then you call the Constructor using the new operator, and you pass in any parameters the Constructor takes. A full description of the Scanner class and all of its methods/attributes can be found here (http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html)

Hope this helps.

zk0
12-12-2006, 10:21 PM
Ok So Objects are "instances of a class" they are what we call "reference types". This means they pass by address. But away with the Techical jargon.

First step is to declare your object. You do this like you would declare any variable

Type variableName;

so for Scanner it would be:

Scanner in;

or whatever you want to call it (doesn't have to be named "in")


Second step is called instantiation. this is where you create the object. You do this by allocating memory dynamically. We have a specially operator to do this for us. it is called the new operator. new operator takes one operand, which is the call to the Class constructor. Scanner's constructor takes in an InputStream, since we are using standard input, we will give it that input stream:

//instantiate the Scanner object with standard input
in = new Scanner(System.in);

now the Scanner object is declared and instantiated, it is ready for use.

lets say you want to read in a line of text from the keyboard using the nextLine method. You call methods from objects using the . operator:

//read a line from standard input
String input = in.nextLine();


Ok so quick review for creating objects. First you declare the variable, then you call the Constructor using the new operator, and you pass in any parameters the Constructor takes. A full description of the Scanner class and all of its methods/attributes can be found here (http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html)

Hope this helps.

Thank you. Im starting to understand.

Now the code looks like this:

package your_username;

import java.util.Scanner;

public class Step3_lab02
{
public static void main(String[] args)
{
Scanner userKeyboard;
userKeyboard = new Scanner(System.in);
String input = userKeyboard.nextLine();

}
}

class Palindrome
{
public boolean isPalindrome(String str)
{
String reverseStr = (new StringBuffer(str)).reverse().toString();
return str.equalsIgnoreCase(reverseStr);
}

public String removeNoneLetters(String str)
{
String result = "";
for (int i = 0; i < str.length(); ++i)
{
if (Character.isLetter(str.charAt(i)))
{
result = result + str.charAt(i);
}
}
return result;
}
}


So now I can write in a text in the console. But how do I "move" the text that I have entered from the "Scanner" to this code (where the code remove none letters):

public String removeNoneLetters(String str)
{
String result = "";
for (int i = 0; i < str.length(); ++i)
{
if (Character.isLetter(str.charAt(i)))
{
result = result + str.charAt(i);
}
}
return result;
}

And then direct the result to:

public boolean isPalindrome(String str)
{
String reverseStr = (new StringBuffer(str)).reverse().toString();
return str.equalsIgnoreCase(reverseStr);
}

Thanks for any help! :)

Mink
12-12-2006, 10:28 PM
Ok good! You say you are starting to understand more about objects. Then now what you need to do is instantiate a Palindrome object. You should know how to do this now because I showed you how to do it with a general example and with the Scanner class.

Once you have set up for Palindrome object, you are going to need to call methods from it (similar to how you called nextLine from the Scanner object)

The methods that you will be using take in String parameters to test. Can you figure out where would those Strings come from?

Like we stated before, we won't do your homework for you, but we are always here to guide and mentor you.

Good luck! If you have anymore questions feel free to ask.

zk0
12-13-2006, 01:14 PM
Ok good! You say you are starting to understand more about objects. Then now what you need to do is instantiate a Palindrome object. You should know how to do this now because I showed you how to do it with a general example and with the Scanner class.

Once you have set up for Palindrome object, you are going to need to call methods from it (similar to how you called nextLine from the Scanner object)

The methods that you will be using take in String parameters to test. Can you figure out where would those Strings come from?

Like we stated before, we won't do your homework for you, but we are always here to guide and mentor you.

Good luck! If you have anymore questions feel free to ask.
Yeah I know you wont do my "homework". And Im happy for that.

Ok I've tried to call the "removeNoneLetters":

public static void main(String[] args)
{
Scanner userKeyboard;
userKeyboard = new Scanner(System.in);

System.out.println("Enter text and press [enter]:");
String input = userKeyboard.nextLine();
input = Palindrome.removeNoneLetters(String str);
}
But I get an error in the above bold and underlined marked code:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
String cannot be resolved
Syntax error on token "str", delete this token

This is how I have thought:

The first "input" ( String input = userKeyboard.nextLine(); ) collects the text the user types in using "userKeyboard.nextLine".

The next "input" that has collected the text moves the text to "Palindrome.removeNoneLetters"...

zk0
12-13-2006, 02:02 PM
Ok, here is another go:

public static void main(String[] args)
{

Scanner userKeyboard;
userKeyboard = new Scanner(System.in);

System.out.println("Enter text and press [enter]:");
String input = userKeyboard.nextLine();

Palindrome text = Palindrome();
Palindrome textAgain = Palindrome();

text.removeNoneLetters();
textAgain.isPalindrome();
}

Error message I get:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method Palindrome() is undefined for the type Step3_lab02
The method Palindrome() is undefined for the type Step3_lab02
The method removeNoneLetters(String) in the type Palindrome is not applicable for the arguments ()
The method isPalindrome(String) in the type Palindrome is not applicable for the arguments ()

at your_username.Step3_lab02.main(Step3_lab02.java:16)

zk0
12-13-2006, 02:05 PM
I fixed the errors:

package your_username;

import java.util.Scanner;

public class Step3_lab02
{
public static void main(String[] args)
{

Scanner userKeyboard;
userKeyboard = new Scanner(System.in);

System.out.println("Enter text and press [enter]:");
String input = userKeyboard.nextLine();

Palindrome text = new Palindrome();
Palindrome textAgain = new Palindrome();

text.removeNoneLetters(input);
textAgain.isPalindrome(input);
}

}

class Palindrome
{
public boolean isPalindrome(String str)
{
String reverseStr = (new StringBuffer(str)).reverse().toString();
return str.equalsIgnoreCase(reverseStr);
}

public String removeNoneLetters(String str)
{
String result = "";
for (int i = 0; i < str.length(); ++i)
{
if (Character.isLetter(str.charAt(i)))
{
result = result + str.charAt(i);
}
}
return result;
}
}

What am I doing wrong?

Gox
12-13-2006, 07:11 PM
I see nothing wrong with your code, except that you aren't capturing the "returns" from the method calls, and reporting whether the word was a palindrome to the user.

I've added this to your main and it looks like everything is working properly.

public static void main(String[] args)
{

boolean isP = false;
Scanner userKeyboard;
userKeyboard = new Scanner(System.in);

System.out.println("Enter text and press [enter]:");
String input = userKeyboard.nextLine();

Palindrome text = new Palindrome();
Palindrome textAgain = new Palindrome();

input = text.removeNoneLetters(input);
isP = textAgain.isPalindrome(input);

if(isP)
System.out.println(input + " is a palindrome");
else
System.out.println(input + " is not a palindrome");
}

EDIT: You also don't need the two Palindrome objects, one will suffice.
This is the same as above, but a little cleaner;

public static void main(String[] args)
{

boolean isP = false;
Scanner userKeyboard;
userKeyboard = new Scanner(System.in);

System.out.println("Enter text and press [enter]:");
String input = userKeyboard.nextLine();

Palindrome text = new Palindrome();

input = text.removeNoneLetters(input);
isP = text.isPalindrome(input);

if(isP)
System.out.println(input + " is a palindrome");
else
System.out.println(input + " is not a palindrome");
}

zk0
12-14-2006, 12:38 PM
I see nothing wrong with your code, except that you aren't capturing the "returns" from the method calls, and reporting whether the word was a palindrome to the user.

I've added this to your main and it looks like everything is working properly.

public static void main(String[] args)
{

boolean isP = false;
Scanner userKeyboard;
userKeyboard = new Scanner(System.in);

System.out.println("Enter text and press [enter]:");
String input = userKeyboard.nextLine();

Palindrome text = new Palindrome();
Palindrome textAgain = new Palindrome();

input = text.removeNoneLetters(input);
isP = textAgain.isPalindrome(input);

if(isP)
System.out.println(input + " is a palindrome");
else
System.out.println(input + " is not a palindrome");
}

EDIT: You also don't need the two Palindrome objects, one will suffice.
This is the same as above, but a little cleaner;

public static void main(String[] args)
{

boolean isP = false;
Scanner userKeyboard;
userKeyboard = new Scanner(System.in);

System.out.println("Enter text and press [enter]:");
String input = userKeyboard.nextLine();

Palindrome text = new Palindrome();

input = text.removeNoneLetters(input);
isP = text.isPalindrome(input);

if(isP)
System.out.println(input + " is a palindrome");
else
System.out.println(input + " is not a palindrome");
}

Thank you. THe only thing I don't understand now is why must the boolean isP = false?
boolean isP = false;

Mink
12-14-2006, 12:51 PM
it doesn't. He did that to initialize it, which he didn't need to to begin with.

zk0
01-03-2007, 03:47 PM
Hi again!

Im still having problems with this code:

package your_username;

import java.util.Scanner;

public class Step3_lab02
{
public static void main(String[] args)
{

boolean isPalindrome = false;
Palindrome text = new Palindrome();

Scanner userKeyboard; // Låter användaren kunna skriva in text.
userKeyboard = new Scanner(System.in);

System.out.println("Ange en sträng och tryck [Enter]:");
String input = userKeyboard.nextLine(); // input är det som användaren skriver in.

text.removeNoneLetters(input);
input = text.removeNoneLetters(input); // input skickas till "removeNoneLetters" som tar bort alla tecken som inte är bokstäver .

if(isPalindrome)
System.out.println("'" + input + "'" + " är ett palindrome."); // Visas för användaren om texten är ett palindrome.
else
System.out.println("'" + input + "'" + " är inte ett palindrome."); // Visas för användaren om texten INTE är ett palindrome.
}

}

class Palindrome
{
public boolean isPalindrome(String str)
{
String reverseStr = (new StringBuffer(str)).reverse().toString();
return str.equalsIgnoreCase(reverseStr);
}

public String removeNoneLetters(String str)
{
String result = "";
for (int i = 0; i < str.length(); ++i)
{
if (Character.isLetter(str.charAt(i)))
{
result = result + str.charAt(i);
}
}
return result;
}
}


If I type in "apa" it says that it isnt a palindrome - but of course it is.

I beleve I have the error in the for loop: if(isPalindrome) But I can't understand what the problem is?

Thank you in advance!