Reading Palidromes into Java

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 2536 Views - Last Post: 06 October 2010 - 08:13 PM Rate Topic: -----

#1 blairb91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 28-September 10

Reading Palidromes into Java

Posted 05 October 2010 - 02:42 PM

Hi, guys:

I'm trying to write a code in java that will read in any palindrome word or phrase a user enters and determine whether it is a palindrome or not.

I must read in a series of phrases. For each, if it is a plaindrome print "yes", otherwise print "no". Stop when the user types in "done". I also must use a variable of type 'boolean' and a compound boolean expression.

In addition, I have to ignore capitalization and spaces (I think I've included this), and assume the only punctuation marks in the input are: comma (,), period (.) and apostorphe ('), and assume that the user types at least one letter (i.e. ignoring the empty string as input).

Here is my code so far...not sure what to include from here, as the console automatically terminates:


import java.util.Scanner;
import javax.sound.sampled.DataLine.Info;
/*
 * Implementation Plan
 * 0. Copy the skeleton
 * 1. Print message
 * 2. Read entered palindrome
 * 3. Write loop
 * 4. Recognize palindrome word
 * 5. Recognize palindrome phrase
 * 6. Recognize non-palindrome
 * 7. Answer yes for palindrome
 * 8. Answer no for non-palindrome
 * 9. Run error testing
 * Test Plan
 *  
 */
public class Palindrome {

	public static void main(String[] args) {

		String phrase;
		Scanner keyboard = new Scanner(System.in);

		System.out.println("Please enter a possible palindrome(Enter done to stop)");

		// Java will read the entire line into the variable 'phrase'.
		phrase = keyboard.nextLine();

		int index;

	}

	public static boolean isPalindrome(String s) {
		s = s.toLowerCase();
		s = s.trim();

		if (isPalindrome(s))
			System.out.println("Yes");

		else
			System.out.println("No");

		if (s.length() <= 1)
			return true; // Base case
		else {
			if (s.charAt(0) == s.charAt(s.length() - 1))
				return isPalindrome(s.substring(1, s.length() - 1));
			else
				return false;

		}

	}
}




Thanks, all!

Is This A Good Question/Topic? 0
  • +

Replies To: Reading Palidromes into Java

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: Reading Palidromes into Java

Posted 05 October 2010 - 02:50 PM

First, you need a loop. This loop needs to go around your code in main and check to see if it equals "done". If it does, then stop looping. Now, you should also look into replaceAll() from the String class to replace punctuation and spaces with empty Strings.
Was This Post Helpful? 0
  • +
  • -

#3 mrsuchacz  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 31
  • Joined: 15-May 10

Re: Reading Palidromes into Java

Posted 05 October 2010 - 07:32 PM

have you learned stacks yet? ... we had this same assignment this week :)
Was This Post Helpful? 0
  • +
  • -

#4 blairb91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 28-September 10

Re: Reading Palidromes into Java

Posted 05 October 2010 - 07:47 PM

Nope, we haven't learned stacks yet!

I know I must create the loop to encompass the entire program, but I just don't know where to get started or where to put it. I think the code is out of order, but the formatting won't change it on eclipse.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Reading Palidromes into Java

Posted 05 October 2010 - 08:52 PM

You never call your isPalindome() method from main() so it is never executed

An even if it was called, the first thing that isPalindrome() does, after putting the String lower and trimmed is to call back itself. You would be in an infinite loop.
Was This Post Helpful? 0
  • +
  • -

#6 McSchemy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-March 09

Re: Reading Palidromes into Java

Posted 05 October 2010 - 09:25 PM

hey,

you may not need to enclose the whole program in a loop, just part of the static isPallindrome() method.


i'm not too sure if this is right, i didnt compile it but it's how i would approach it.

good luck.
import java.util.Scanner;	
	public class Palindrome 
	{	 
	    public static void main(String[] args) 
	    {	 
	        String phrase;
	        Scanner keyboard = new Scanner(System.in);	 
	        System.out.println("Please enter a possible palindrome(Enter done to stop)");
	 
// Java will read the entire line into the variable 'phrase'.	        
	 
	        int index;

//read entered palindrome

		phrase = keyboard.nextLine();
		
//print entered palindrome

		System.out.println("your input was: "+phrase);

//give to ispalindrome method

		 isPalindrome(phrase);
	    }
	 
	    public static boolean isPalindrome(String s) 
	    {

//define boolean

		boolean isPalindrome = true;
	        s = s.toLowerCase();
	        s = s.trim();

//length of string

		int len = s.length();

//split the string in half

		String [] firstHalf = x.substring(0,(len/2));
		String [] secondHalf = x.substring((len/2),len);

//the loop will flip the values of the second half of the string

		for (int left=0, int right=b.length-1; left<right; left++, right--) 
		{

// exchange the first and last

    		int temp = firstHalf[left]; firstHalf[left]  = firstHalf[right]; firstHalf[right] = temp;
		}

//compare first and second halves
		for(int i = 0;i<(len/2);i++)
		{
		if(firstHalf[i] == temp[i]){isPalindrome true;}
		if(firstHalf[i] != temp[i]){return false;}
		}	 
	    }
//Prompt user to try again
	System.out.println("Would you like to go again? ");
	Scanner yesOrNo = new Scanner(System.in);
	String Y_N = yesOrNo.toString();
	if(Y_N == 'y')
	{
//return to main
	main(null);
	}
//exit
	else{System.exit(0);}
	}



Edited by macosxnerd101: Please, :code:.
Was This Post Helpful? 0
  • +
  • -

#7 blairb91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 28-September 10

Re: Reading Palidromes into Java

Posted 05 October 2010 - 10:15 PM

Hi again, all:

Thanks for the help! I've reworked the code and it is shown below.

The only thing I cannot figure out is how to read each character after the very left character and very right character is verified. Also, how do I ensure that the user can enter "done" to end the program without it reading the word?

Thanks again!

import java.util.Scanner;


public class Palindrome {

	public static void main(String[] args) {

		String phrase;
		Scanner keyboard = new Scanner(System.in);

		System.out
				.println("Please enter a possible palindrome(Enter done to stop)");

		// Java will read the entire line into the variable 'phrase'.
		phrase = keyboard.nextLine();

		int index;

		// Give to ispalindrome method

		isPalindrome(phrase);
	}

	public static void isPalindrome(String s) {

		// Define boolean

		boolean isPalindrome = true;
		s = s.toLowerCase();
		s = s.trim();

		// Length of string
		int l = 0;
		int r = s.length() - 1;
		int len = s.length();

		// Check the string

		if (s.charAt(l) == s.charAt(r))
			System.out.println("Yes");
		else
			System.out.println("No");

	}

}



Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: Reading Palidromes into Java

Posted 06 October 2010 - 03:58 AM

View PostMcSchemy, on 05 October 2010 - 11:25 PM, said:

hey,

you may not need to enclose the whole program in a loop, just part of the static isPallindrome() method.


i'm not too sure if this is right, i didnt compile it but it's how i would approach it.

good luck

[snip]


Please in the future, use code tags -> :code:

Also, at </Dream.In.Code>, we'd prefer to see see teaching going on than helping. By handing him the program, you have now stifled the ability for the OP to write it on his own with our guidance. Please offer guidance and not hand out code.

OT:
To make the proper change, add a while loop that loops forever, but have an if statement after the input that checks the input. If the input is "done", then we break out of the loop. This way is MUCH simpler than the method used.

	while(true) {
		System.out.println("Please enter a possible palindrome(Enter done to stop)");

		// Java will read the entire line into the variable 'phrase'.
		phrase = keyboard.nextLine();
                if (phrase.equalsIgnoreCase("done") 
                    break;

		// Give to ispalindrome method
		isPalindrome(phrase);
    	}


Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10566
  • View blog
  • Posts: 39,107
  • Joined: 27-December 08

Re: Reading Palidromes into Java

Posted 06 October 2010 - 06:28 AM

Just use the StringBuilder class for the reverse() method. Create a StringBuilder from a String, then invoke reverse() and toString(). Now test to see if the original String equalsIgnoreCase() the new String from the StringBuilder.
Was This Post Helpful? 0
  • +
  • -

#10 McSchemy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-March 09

Re: Reading Palidromes into Java

Posted 06 October 2010 - 09:14 AM

sorry, just trying to help. sometimes the whole 'do it yourself and find no concrete help' is frustrating to people new at java. i know that i just need to see it once and it clicks for other programs i am assigned.

for trying to get the program to exit when the user enters 'done' u gotta test the phrase string.. something like:
String test = phrase.toLowerCase();
if(test == "done")
{System.exit(0);}


This post has been edited by McSchemy: 06 October 2010 - 09:16 AM

Was This Post Helpful? 0
  • +
  • -

#11 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: Reading Palidromes into Java

Posted 06 October 2010 - 09:29 AM

It is possible to determine if a string is a palindrome without changing it into another data type isn't it?
Was This Post Helpful? 0
  • +
  • -

#12 blairb91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 28-September 10

Re: Reading Palidromes into Java

Posted 06 October 2010 - 09:52 AM

Still can't figure out out to test each letter correctly...every method I've tried it incorrect and garners a ton of error messages. Help!




import java.util.Scanner;


public class Palindrome {

	public static void main(String[] args) {

		String phrase;
		Scanner keyboard = new Scanner(System.in);
		System.out.println("Please enter a possible palindrome(Enter done to stop)");

		// Java will read the entire line into the variable 'phrase'.
		phrase = keyboard.nextLine();
		String test = phrase.toLowerCase();
		if(test == "done")
		
		{System.exit(0);}
		
		int index;

		// Give to ispalindrome method

		isPalindrome(phrase);
	}

	public static void isPalindrome(String s) {

		// Define boolean

		boolean isPalindrome = true;
		s = s.toLowerCase();
		s = s.trim();

		// Length of string
		int l = 0;
		int r = s.length() - 1;
		int len = s.length();


		// Check the string

		if (s.charAt(l) == s.charAt(r))
			System.out.println("Yes");
		else
			System.out.println("No");

	}

}


Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10566
  • View blog
  • Posts: 39,107
  • Joined: 27-December 08

Re: Reading Palidromes into Java

Posted 06 October 2010 - 10:13 AM

Hint- use a for loop. You need to iterate from 0-n/2. Detect if the charAt(i) == charAt(str.length()-1-i), assuming your counter variable in the loop is i.
Was This Post Helpful? 0
  • +
  • -

#14 blairb91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 28-September 10

Re: Reading Palidromes into Java

Posted 06 October 2010 - 10:28 AM

I did, but it still doesn't seem to be working


import java.util.Scanner;

/*
 * Name: Blair Brown
 * Section: COMP 110.003
 * Program 6 - Palindrome
 * 
 * Implementation Plan
 * 0. Copy the skeleton
 * 1. Print message
 * 2. Read entered palindrome
 * 3. Write loop
 * 4. Recognize palindrome word
 * 5. Recognize palindrome phrase
 * 6. Recognize non-palindrome
 * 7. Answer yes for palindrome
 * 8. Answer no for non-palindrome
 * 9. Run error testing
 * Test Plan
 *  Input	palindrome and non-palindrome
 * Typical Case 
 * 	anna 	true
 * 	hannah 	true 
 *  
 * Boundary Conditions
 * don't nod 	true
 * no fun 		false
 * done			false
 */
public class Palindrome {

	public static void main(String[] args) {

		String phrase;
		Scanner keyboard = new Scanner(System.in);
		System.out
				.println("Please enter a possible palindrome(Enter done to stop)");

		// Java will read the entire line into the variable 'phrase'.
		phrase = keyboard.nextLine();
		new StringBuffer(phrase);
		String test = phrase.toLowerCase();

		if (test == "done")

		{
			System.exit(0);
		}

		int index;

		// Give to ispalindrome method

		isPalindrome(phrase);
	}

	public static boolean isPalindrome(String s) {

		// Define boolean

		boolean isPalindrome = true;
		s = s.toLowerCase();
		s = s.trim();

		// Length of string
		int l = 0;
		int r = s.length() - 1;
		int len = s.length();

		// Check the string
		while (l < r) {
			if (s.charAt(l) != s.charAt(r)) {
				return false;
			}
			if (s.charAt(l) == s.charAt(r))
				System.out.println("Yes");
			l++;
			r--;
			
				return true;
		}
		
		return isPalindrome;
		

	}
}


Was This Post Helpful? 0
  • +
  • -

#15 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10566
  • View blog
  • Posts: 39,107
  • Joined: 27-December 08

Re: Reading Palidromes into Java

Posted 06 October 2010 - 11:46 AM

You don't want to be returning true in the loop. Otherwise, it will return true if the first and last characters are equal. Return true after the loop, which means you won't have returned false, so the String is a palindrome.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2