Go Back   nV News Forums > Software Forums > Software Development

Newegg Daily Deals

Reply
 
Thread Tools
Old 03-15-07, 05:23 PM   #1
DoctorFu
Registered User
 
Join Date: Mar 2007
Posts: 1
Default Java Help: Palindrome program

Hi, I'm very new to Java programming and I working on a school project. The project is to write a program that scans in a string and determines length, reverses the string and then checks to see if it's a palindrome. Here's the stab I've made so far:

Edit: I found another web site that let me save my reversed info as another separate string. Here is my final code in case anybody wants to looks at it:

Code:
import java.util.regex.*;
import java.io.*;
import java.util.Scanner;   // import java.util.Scanner to use the Scanner function


public class cmayproj01
{                           // start class cmayproj01

   public static void main(String[] args)
   {                        // start main
	   print_header();            // call from header

	   System.out.println("Please type a string that contains, at most, 30 characters:");
	   Scanner scanIn = new Scanner(System.in);
	   String strUserInput = scanIn.nextLine();
	   String strInputCopy = strUserInput;


	   System.out.println("\n");

	   String strInputTrimmed = strUserInput.replace(" ", "");

	   System.out.println( "You typed: " + strUserInput);
       System.out.println( "The trimmed version is: " + strInputTrimmed);  // remove from final program
	   System.out.print( "The length is: ");
	   int numberofchars = strInputTrimmed.length();
       System.out.println(numberofchars + " Characters.");


	   reverse(strInputTrimmed, strUserInput);         //called from reverse

/*
	   if (strInputTrimmed.compareTo(strUserInput) == 1)
	   {
		   System.out.println( "\nYou didn't type a Palindrome.");	   
	   }
	   else
	   {
		   System.out.println( "\nYou typed a Palindrome!");
	   }
 */
       
   }                    // end main


	                      // start functions
	public static void print_header()
	{                       // start print_header
		System.out.println("This is where the header goes...\n");  // insert header here!!
	}                       // end print_header
	
    public static void reverse(String strInputTrimmed, String strUserInput)
    {                       // start reverse

	
		System.out.println("The original string you typed was: " + strUserInput);
		System.out.print("The reverse is: ");

		
		if (strInputTrimmed.length() == 1)
		{
			System.out.print(strInputTrimmed);
		}
		else
		{

			int len = strInputTrimmed.length();
			char[] tempCharArray = new char[len];
			char[] charArray = new char[len];

			// put original string in an array of chars
			for (int i = 0; i < len; i++)
			{
				tempCharArray[i] = strInputTrimmed.charAt(i);
			}

			// reverse array of chars
			for (int j = 0; j < len; j++)
			{
				charArray[j] = tempCharArray[len - 1 - j];
			}

			String strInputReverse = new String(charArray);
			System.out.println(strInputReverse);

			if (strInputReverse.compareTo(strInputTrimmed) < 0)
			{
				System.out.println("\nYou didn't type a Palindrome.");
			}
			else
			{
				System.out.println("\nYou typed a Palindrome!");
			}

		}

    }                       // end reverse
}                          // end cmayproj01
I don't feel this is spam but if it is please delete.
DoctorFu is offline   Reply With Quote
Old 03-18-07, 02:14 PM   #2
Cinema4DXL
Registered User
 
Join Date: Sep 2004
Posts: 97
Default Re: Java Help: Palindrome program

have you tested this, because

if (strInputReverse.compareTo(strInputTrimmed) < 0)
{
System.out.println("\nYou didn't type a Palindrome.");
}
else
{
System.out.println("\nYou typed a Palindrome!");
}


The compareTo method only does string comparsions lexicographically. This does not ensure that you have a correct palindrome.

I suggest your program for test cases, also your program ignores a string has that spaces, does it handle other characters like ? . , (not sure what the scope of your project is) if it is to handle any kind of string input.
__________________
Athlon XP-M 2600+@(200x11.5) | Abit AN7 | 2x512 Corsair XMS PC-3200 | BFG 6800 GT OC (400/1100) w/ NV Silencer 5 | Promise Ultra133TX2 | WD Caviar SE 80GB 8 MB | Seagate Barracuda 7200.7 160GB | Maxtor DiamondMax 10 300GB | Sony 16x DVD-ROM | NEC 3520A | Antec SmartPower 2.0 500w | Koolance PC-601 | Samsung SyncMaster 997DF
Cinema4DXL is offline   Reply With Quote
Old 04-24-07, 05:53 PM   #3
Absolution
AbsoUI
 
Absolution's Avatar
 
Join Date: Dec 2004
Posts: 933
Default Re: Java Help: Palindrome program

this is quite a bit smaller
Code:
// 
// CS161L Wed 3:00 PM
// 9:00 Lecture Mon,Wed,Fri
// Lab9 - lab10.java
import java.util.*;

public class lab10
{
	public static boolean palindrone(String s, int index)
	{
		if(index == s.length())		// drop out of the loop, string is at the end
			return true;
		char first,last;
		first = s.charAt(index);
		last = s.charAt(s.length()-index-1);
		//System.out.println(first + " " + last);
		if(first == last)
			return palindrone(s,++index);
		else
			return false;
	}
	public static void main(String args[])
	{
		Scanner input = new Scanner(System.in);
		System.out.print("Enter a word, 'quit' to exit: ");
		String in = input.nextLine();
		while(!in.equals("quit"))
		{
			if(palindrone(in,0))
				System.out.println("This is a palindrone");
			else
				System.out.println("This is not a palindrone");
			System.out.print("Enter another word: ");
			in = input.nextLine();
		}
	}
}
Absolution is offline   Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


Similar Threads
Thread Thread Starter Forum Replies Last Post
"GPU has fallen off the bus" error on 650M unless a CUDA program run first amonakov NVIDIA Linux 0 06-18-12 07:34 PM
Google (partially) loses suit to Oracle over use of Java API's ViN86 Mobile Devices And Smartphones 3 05-17-12 11:25 AM
Judge Declines To Rule That Google Pillaged Java News Archived News Items 0 05-10-12 08:00 AM
Program to Identify DDR RAM modules speeds MUYA General Hardware 4 10-11-02 12:41 PM
BAH! I'm being driven insane by Java Virtual Machine Kruno General Software 9 09-01-02 10:59 PM

All times are GMT -5. The time now is 04:10 PM.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright 1998 - 2014, nV News.