DoctorFu
03-15-07, 06:23 PM
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:
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.
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:
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.