Need help with School Assignment

"Cannot find Symbol" error...

Page 1 of 1

14 Replies - 948 Views - Last Post: 09 October 2008 - 03:45 PM Rate Topic: -----

#1 he4dhuntr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 09-October 08

Need help with School Assignment

Post icon  Posted 09 October 2008 - 02:13 PM

Hey guys,
I'm taking a java programing class and I'm having some problems with my second assignment of the semester. Here are the requirements for the assignment:

"A palindrome is a word that is exactly the same when spelt forwards or backwards. Example: "mom". Write a program that reads a word and determines if it is a palindrome. Continue reading and testing words until you encounter the word "quit". Treat uppercase letters as lowercase letters. Write your solution in one class: Palindrome. It should implement a method testWord(String word) that receives a word."

I honestly don't know what I'm doing right now. I've spent hours on this and my brain is just completely fried. I took a look at some examples for code online for Palindromes and that helped a bit.

The errors I'm getting right now are:
- at the "while (newPal != "quit") {" line near the end, they are incomparable types, java.lang.String[] and java.lang.String. How do I manage to compare these two?
- at the "if (isPalindrome == true) {" almost right underneath, it tells me that it "cannot find symbol variable". Anyone know how I can fix this?

My code may be way off, I really don't know at this point. Any help would be GREATLY appreciated. Thanks in advance!
Here's my code so far:

public class Palindrome {

	private String pal;
	
	public Palindrome(String initPal) {
		pal = initPal.toLowerCase();
		// Set the letters of the words to be read in lower case.
	}
	
	public boolean isPalindrome() {
		
		int left = 0;
		int right = pal.length() -1;
		while (left < right) {
			if (pal.charAt(left) != pal.charAt(right)) {
			   boolean isPalindrome = false;
			   // If the character at the far left is not the same as the one to the far right, the word is not a palindrome.
			}
			else {
				left++;
				right--;
				// Move to the next letter from the left and the next from the right until the center is reached.
			}
		}
		boolean isPalindrome = true;
		// If the center of the word is reached and all the letters matched, the word is a palindrome.
	}
	
	public static void main(String[] args) {
		
		String[] newPal = new String[7];
		
		newPal[0] = "redder";
		newPal[1] = "Smart";
		newPal[2] = "Kayak";
		newPal[3] = "Radar";
		newPal[4] = "SmoKe";
		newPal[5] = "quit";
		newPal[6] = "madam";
		// Enter the list of words to be checked.
		
		for (String word : newPal) {
			while (newPal != "quit") {
				// If the word is not "quit", continue the process.
				if (isPalindrome == true) {
					System.out.println(newPal + "IS a palindrome!");
				}
				else {
					System.out.println(newPal + "is NOT a palindrome!");
				}
			}
		}
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Need help with School Assignment

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 516
  • View blog
  • Posts: 5,588
  • Joined: 20-March 08

Re: Need help with School Assignment

Posted 09 October 2008 - 02:26 PM

View Posthe4dhuntr, on 9 Oct, 2008 - 02:13 PM, said:

Hey guys,
I'm taking a java programing class and I'm having some problems with my second assignment of the semester. Here are the requirements for the assignment:

"A palindrome is a word that is exactly the same when spelt forwards or backwards. Example: "mom". Write a program that reads a word and determines if it is a palindrome. Continue reading and testing words until you encounter the word "quit". Treat uppercase letters as lowercase letters. Write your solution in one class: Palindrome. It should implement a method testWord(String word) that receives a word."

I honestly don't know what I'm doing right now. I've spent hours on this and my brain is just completely fried. I took a look at some examples for code online for Palindromes and that helped a bit.

The errors I'm getting right now are:
- at the "while (newPal != "quit") {" line near the end, they are incomparable types, java.lang.String[] and java.lang.String. How do I manage to compare these two?
- at the "if (isPalindrome == true) {" almost right underneath, it tells me that it "cannot find symbol variable". Anyone know how I can fix this?

My code may be way off, I really don't know at this point. Any help would be GREATLY appreciated. Thanks in advance!
Here's my code so far:

public class Palindrome {

	private String pal;
	
	public Palindrome(String initPal) {
		pal = initPal.toLowerCase();
		// Set the letters of the words to be read in lower case.
	}
	
	public boolean isPalindrome() {
		
		int left = 0;
		int right = pal.length() -1;
		while (left < right) {
			if (pal.charAt(left) != pal.charAt(right)) {
			   boolean isPalindrome = false;
			   // If the character at the far left is not the same as the one to the far right, the word is not a palindrome.
			}
			else {
				left++;
				right--;
				// Move to the next letter from the left and the next from the right until the center is reached.
			}
		}
		boolean isPalindrome = true;
		// If the center of the word is reached and all the letters matched, the word is a palindrome.
	}
	
	public static void main(String[] args) {
		
		String[] newPal = new String[7];
		
		newPal[0] = "redder";
		newPal[1] = "Smart";
		newPal[2] = "Kayak";
		newPal[3] = "Radar";
		newPal[4] = "SmoKe";
		newPal[5] = "quit";
		newPal[6] = "madam";
		// Enter the list of words to be checked.
		
		for (String word : newPal) {
			while (newPal != "quit") {
				// If the word is not "quit", continue the process.
				if (isPalindrome == true) {
					System.out.println(newPal + "IS a palindrome!");
				}
				else {
					System.out.println(newPal + "is NOT a palindrome!");
				}
			}
		}
	}
}


You're first error is simply this.

if (newPal != "quit") //cannot do this, 2 errors here

// change the previous line to the next one
// 'word' is the String variable in your foreach loop
if (!word.equals("quit"))

// you MUST use .equals() for strings and other objects.
// the == compares memory addresses for objects.


The second error is simply this other one.

is (isPalindrome == true)
// isPalindrome is a method, not a variable.

// replace that with this.
if (isPalindrome(word))


Hope this helps! :D

This post has been edited by Locke37: 09 October 2008 - 02:31 PM

Was This Post Helpful? 1
  • +
  • -

#3 pertheusual  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 245
  • Joined: 26-January 08

Re: Need help with School Assignment

Posted 09 October 2008 - 02:32 PM

I didn't check over the logic, but I can tell you why those errors are happening.

Ooo, Locke37 got most of it.


You function also needs to be adjusted though. Right now, you are setting a boolean inside the function that has the name of the function, but you should really be returning a variable like "return true;"

Your palindrome function is also using a global variable "pal" right now, but you should really be passing a variable into it.
public boolean isPalindrome(String pal) {


And erase the "String pal;" at the top of the program.

Hope that helps!

Per
Was This Post Helpful? 0
  • +
  • -

#4 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 516
  • View blog
  • Posts: 5,588
  • Joined: 20-March 08

Re: Need help with School Assignment

Posted 09 October 2008 - 02:34 PM

View Postpertheusual, on 9 Oct, 2008 - 02:32 PM, said:

Your palindrome function is also using a global variable "pal" right now, but you should really be passing a variable into it.
public boolean isPalindrome(String pal) {


And erase the "String pal;" at the top of the program.

Hope that helps!

Per


Oh...didn't catch that one...:unsure:

Good call! :^: :)

Although I don't think it'll affect execution any...it's just wasted String space. :P

This post has been edited by Locke37: 09 October 2008 - 02:34 PM

Was This Post Helpful? 0
  • +
  • -

#5 pertheusual  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 245
  • Joined: 26-January 08

Re: Need help with School Assignment

Posted 09 October 2008 - 02:42 PM

Well, the global variable also isn't being set anywhere :P

OR on further inspection, it is being set in a constructor that is not actually being called...

Per

Oh, and it's also going to infinitely loop here
 while (newPal != "quit") {


because newPal isn't changing inside the while.

That should actually be
if(word.equals("quit")) break;



Per
Was This Post Helpful? 0
  • +
  • -

#6 he4dhuntr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 09-October 08

Re: Need help with School Assignment

Posted 09 October 2008 - 02:43 PM

Hey guys!
Thanks for the replies! That was quick!

You seem to have fixed both of my problems, except when I try the fix for the boolean, now I get this error:

"isPalindrome() in Palindrome cannot be applied to (boolean)"

Also, how do I fix that infinite loop? What would be the more extended code?

Here is my new code with the changes:
THANKS AGAIN!

public class Palindrome {

	private String pal;
	
	public Palindrome(String initPal) {
		pal = initPal.toLowerCase();
		// Set the letters of the words to be read in lower case.
	}
	
	public boolean isPalindrome() {
		
		int left = 0;
		int right = pal.length() -1;
		while (left < right) {
			if (pal.charAt(left) != pal.charAt(right)) {
			   return false;
			   // If the character at the far left is not the same as the one to the far right, the word is not a palindrome.
			}
			else {
				left++;
				right--;
				// Move to the next letter from the left and the next from the right until the center is reached.
			}
		}
		return true;
		// If the center of the word is reached and all the letters matched, the word is a palindrome.
	}
	
	public static void main(String[] args) {
		
		String[] newPal = new String[7];
		
		newPal[0] = "redder";
		newPal[1] = "Smart";
		newPal[2] = "Kayak";
		newPal[3] = "Radar";
		newPal[4] = "SmoKe";
		newPal[5] = "quit";
		newPal[6] = "madam";
		// Enter the list of words to be checked.
		
		for (String word : newPal) {
			while (!word.equals("quit")) {
				// If the word is not "quit", continue the process.
				if (isPalindrome(true)) {
					System.out.println(newPal + "IS a palindrome!");
				}
				else {
					System.out.println(newPal + "is NOT a palindrome!");
				}
			}
		}
	}
}


This post has been edited by he4dhuntr: 09 October 2008 - 02:45 PM

Was This Post Helpful? 0
  • +
  • -

#7 pertheusual  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 245
  • Joined: 26-January 08

Re: Need help with School Assignment

Posted 09 October 2008 - 02:46 PM

Much better. You just miscopied.

if(isPalindrome(word)){  


not (true)

Per
Was This Post Helpful? 0
  • +
  • -

#8 he4dhuntr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 09-October 08

Re: Need help with School Assignment

Posted 09 October 2008 - 02:49 PM

View Postpertheusual, on 9 Oct, 2008 - 02:46 PM, said:

Much better. You just miscopied.

if(isPalindrome(word)){  


not (true)

Per


Cool, but now I get this error:

"isPalindrome() in Palindrome cannot be applied to (java.lang.String)"

Any ideas?

Thanks a lot for the help guys! Much appreciated!

Here's the code again :crazy:

public class Palindrome {

	private String pal;
	
	public Palindrome(String initPal) {
		pal = initPal.toLowerCase();
		// Set the letters of the words to be read in lower case.
	}
	
	public boolean isPalindrome() {
		
		int left = 0;
		int right = pal.length() -1;
		while (left < right) {
			if (pal.charAt(left) != pal.charAt(right)) {
			   return false;
			   // If the character at the far left is not the same as the one to the far right, the word is not a palindrome.
			}
			else {
				left++;
				right--;
				// Move to the next letter from the left and the next from the right until the center is reached.
			}
		}
		return true;
		// If the center of the word is reached and all the letters matched, the word is a palindrome.
	}
	
	public static void main(String[] args) {
		
		String[] newPal = new String[7];
		
		newPal[0] = "redder";
		newPal[1] = "Smart";
		newPal[2] = "Kayak";
		newPal[3] = "Radar";
		newPal[4] = "SmoKe";
		newPal[5] = "quit";
		newPal[6] = "madam";
		// Enter the list of words to be checked.
		
		for (String word : newPal) {
			if(word.equals("quit")) break; {
				// If the word is not "quit", continue the process.
				if (isPalindrome(word)) {
					System.out.println(newPal + "IS a palindrome!");
				}
				else {
					System.out.println(newPal + "is NOT a palindrome!");
				}
			}
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#9 pertheusual  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 245
  • Joined: 26-January 08

Re: Need help with School Assignment

Posted 09 October 2008 - 02:58 PM

Okay, question first. Did your teacher tell you to use Objects?

Right now, you have a constructor at the top that sets "pal." But that constructor is not being used right now because in "main", you are not creating any "Palindrome" objects.

So, if you go without objects, then refer to my first post.

If you are supposed to use objects though, then you need to modify a few things in your "main".

Per
Was This Post Helpful? 0
  • +
  • -

#10 he4dhuntr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 09-October 08

Re: Need help with School Assignment

Posted 09 October 2008 - 03:05 PM

I don't think he explicitly told us to use objects no, but the class is called "Fundamentals of Object-Oriented Programming", so I'm guessing it has something to do with it?

If I simply take out the "private String pal;" at the top of the code (is that what you were referring to?), then I get an error on my next line "pal = initPal.toLowerCase();" where the pal is used.

Sorry for all the trouble. I'm really freakin' lost right now. I was just trying to make it so that when the boolean isPalindrome is "true" that the application writes out the word and then "IS a palindrome".

Did that clear it up? I'm open to any suggestions that'll make this thing work. I tried adding an array of object in the main method, but that didn't work out so well for me either :/ I know the concepts, I just can't seem to apply them.

Ugh.. So depressing! Thanks for the help so far though! Any ideas?
Was This Post Helpful? 0
  • +
  • -

#11 pertheusual  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 245
  • Joined: 26-January 08

Re: Need help with School Assignment

Posted 09 October 2008 - 03:20 PM

Yeah, an array of objects at the start is the way to go if it's an intro to Object Oriented programming :)
Here is a fixed up version for ya.

I changed main a bit to work with objects. Check the comments.
I also added a method called "toString." That will get used when you try to print the value.


public class Palindrome {

    private String pal;
    
    public Palindrome(String initPal) {
        pal = initPal.toLowerCase();
        // Set the letters of the words to be read in lower case.
    }
    
    public boolean isPalindrome() {
        
        int left = 0;
        int right = pal.length() -1;
        while (left < right) {
            if (pal.charAt(left) != pal.charAt(right)) {
               return false;
               // If the character at the far left is not the same as the one to the far right, the word is not a palindrome.
            }
            else {
                left++;
                right--;
                // Move to the next letter from the left and the next from the right until the center is reached.
            }
        }
        return true;
        // If the center of the word is reached and all the letters matched, the word is a palindrome.
    }
    public String toString(){
         return pal;
    }

    public static void main(String[] args) {
        
        Palindrom[] newPal = new Palindrome[7];
        
        newPal[0] = new Palindrome("redder");
        newPal[1] = new Palindrome("Smart");
        newPal[2] = new Palindrome("Kayak");
        newPal[3] = new Palindrome("Radar");
        newPal[4] = new Palindrome("SmoKe");
        newPal[5] = new Palindrome("quit");
        newPal[6] = new Palindrome("madam");
        // Enter the list of words to be checked.
        
        for (Palindrome word : newPal) {
            if(word.toString().equals("quit")) break;
//You don't need the curly brakets here
//this just says, if it is "quit", it will exit the loop right away

                // If the word is not "quit", continue the process.
                if (word.isPalindrome()) {
                    System.out.println(word + "IS a palindrome!");   //HERE, it will call toString automatically because word is an object, not a String. Otherwise it would get confused
                }
                else {
                    System.out.println(word + "is NOT a palindrome!");
                }
            
        }
    }
}




Per
Was This Post Helpful? 1
  • +
  • -

#12 he4dhuntr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 09-October 08

Re: Need help with School Assignment

Posted 09 October 2008 - 03:26 PM

K, I'm pretty much in love with you right now!! :D

I tried that whole object array thing like 3 hours ago, but I had put in this:

	public static void main(String[] args) {
		
		Palindrome[] newPal = new Palindrome[7];
		
		newPal[0] = new Palindrome();
		newPal[1] = new Palindrome();
		newPal[2] = new Palindrome();
		newPal[3] = new Palindrome();
		newPal[4] = new Palindrome();
		newPal[5] = new Palindrome();
		newPal[6] = new Palindrome();



And didn't understand why it wasn't working. I guess I was just totally out of it!! Thanks for the new code!

One last question,
The output I get when I run it is this:

Palindrome@1c5ddc9 IS a palindrome!
Palindrome@163f7a1 is NOT a palindrome!
Palindrome@1690ab IS a palindrome!
Palindrome@173ec72 IS a palindrome!
Palindrome@1a85d38 is NOT a palindrome!
Palindrome@8046f4 is NOT a palindrome!
Palindrome@1b273cc IS a palindrome!

Is there a way instead of showing "Palindrome@1c5ddc9" to show the actual word (ex: "redder IS a palindrome!")?

Thanks again! :)
Was This Post Helpful? 0
  • +
  • -

#13 pertheusual  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 245
  • Joined: 26-January 08

Re: Need help with School Assignment

Posted 09 October 2008 - 03:32 PM

You read my last post too fast :)

Because your print statement is basically going print(object+string), it will by default print out the memory address of the object. This is pretty useless. If the object has a function called toString, then it will use the string returned by that instead of the memory address.

The function is in the code I posted.

Glad to help. I'm procrastinating instead of studying for my midterm tomorrow...

Per
Was This Post Helpful? 0
  • +
  • -

#14 he4dhuntr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 09-October 08

Re: Need help with School Assignment

Posted 09 October 2008 - 03:44 PM

Got it!

Thanks a lot for your help!

I've got two midterms and this assignment due for next week :( Doing the assignment because it's supposed to help me for my midterm.. so we'll see! Thanks for all the help!

Good thing this is just one question out of three on the assignment... And it just took me ALL DAY! You'll probably see me on here again to figure out the two other parts sooner or later! YAY!

Thanks again!

Good luck with the midterm! You're a life-saver!
Was This Post Helpful? 0
  • +
  • -

#15 pertheusual  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 245
  • Joined: 26-January 08

Re: Need help with School Assignment

Posted 09 October 2008 - 03:45 PM

No problem!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1