nV News Forums

 
 

nV News Forums (http://www.nvnews.net/vbulletin/index.php)
-   Software Development (http://www.nvnews.net/vbulletin/forumdisplay.php?f=53)
-   -   Java Help: Palindrome program (http://www.nvnews.net/vbulletin/showthread.php?t=88193)

DoctorFu 03-15-07 04:23 PM

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.

Cinema4DXL 03-18-07 01:14 PM

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.

Absolution 04-24-07 04:53 PM

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();
                }
        }
}



All times are GMT -5. The time now is 07:36 PM.

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