SRL Archive - First Scripts

Palindrome Checker (java)


funnyman9292

2012-10-20 13:42:10

So i made this checker but ican't figure out how to make it input once not twice… Also, i haven't been on Simba/Runescape for a long time but would like to become a productive member of these forums. I have about 2 months of experience with java so far.:) Ummm i dont know how to attach so ill just copy&paste it all.
import java.util.Scanner;
import java.lang.System.*;
//
//Weekly Project 4

public class WeeklyProject4
{
public static void main(String args[])
{
Scanner palindrome = new Scanner(System.in);
String response = "";
System.out.println("Welcome to the palindrome checker, please enter palindrome twice in order to ensure accuracy.\n");
//Palindrome is entered twice due to accuracy ( Scanner is used so this technically works)
while (!response.equalsIgnoreCase("q")) //makes sure it's not q
{
System.out.println("Please enter you're palindrome twice :");
PalindromeChecker yes = new PalindromeChecker(palindrome.nextLine());
if (palindrome.nextLine().equalsIgnoreCase("q")) response="q"; //Checks to make sure q isnt mentioned
else yes.translate();
}
}
}


import java.lang.System.*;
//

public class PalindromeChecker
{
private String checker,refined,lowercase,noSpaces,refinednew,refinednewer,total; //Alot of strings
private int bob; //Instance

public PalindromeChecker(String response)
{
checker = response;
}

public void translate()
{
lowercase = checker.toLowerCase(); //makes all lower case and trims
noSpaces = lowercase.trim();
refinednew = noSpaces.replaceAll("[!@#$%&^*()\"-=_+';:,./<>?]",""); //Replaces all chars listed with nothing
refinednewer = refinednew.replaceAll(" ",""); //Replaces all spaces with nothing
if (refinednewer.length()<2) //makes sure string is longer than one letter
{
System.out.println("the string \""+checker+ "\" is not a palindrome");
}
else checkIt(); //Moves on if works

}
public void checkIt()
{
total="";
for (bob=refinednewer.length()-1; bob>=0;bob–) //For method to get char and add to String total
{
total+=refinednewer.charAt(bob);
}
if (total.equals(refinednewer)) //checks if they are indeed equal
{
System.out.println("The string \""+ checker+ "\" is indeed a palindrome");
}
else
{
System.out.println("the string \""+checker+"\" is not a palindrome");
}
}
}

[/QUOTE]

Brandon

2012-10-20 13:43:55

No code?

funnyman9292

2012-10-20 13:48:03

There we are

J J

2012-10-20 15:36:46

Why would you type in the palindrome twice?
I'm not sure what you are doing in all those code, it can be done way easier :)
SPOILER: http://pastebin.com/p2V1rjjf

Output
Type your word and we will check if it is a palindrome
lepel
lepel is a palindrome!

Type your word and we will check if it is a palindrome
racecar
racecar is a palindrome!

Type your word and we will check if it is a palindrome
villavu
villavu is not a palindrome