I updated my code from last time so please check it again.
I have made some of the variables in my class public so I can test out what is causing the problem.
I know where the problem is coming from, but IDK how to solve it.
It will be underlined in the code.
Not really awesome, but it's my first OO program with separate classes and methods instead of just one huge class of messy code. Basically, the goal of this program is to check two strings and see if they are palindromes or not. A palindrome is basically an inverse string.
Palindrome example:
He was
Was he
or
Never odd or even
Even or odd never
So after a user enters two strings, the program check to see if they are equal after resorting the second string and comparing it with the first.
If I'm not clear enough, tell me so I can explain this further.
Code:
import java.util.Scanner;
public class main {
public static void main(String[] args) {
boolean isPalendrome;
Scanner io = new Scanner(System.in);
String str1;
String str2;
System.out.println("Please enter the first part of your string");
str1 = io.nextLine();
System.out.println("Enter second part of your string");
str2 = io.nextLine();
PChecker pc = new PChecker(str1, str2);
isPalendrome = pc.checker();
System.out.println("Test: " + isPalendrome); //TEST
System.out.println("Test: " + pc.compare);
System.out.println("Test: " + pc.newStr);
if (isPalendrome == true)
System.out.println("The two strings are inverse so this is a palendrome");
else if (isPalendrome == false)
System.out.println("It is not a palendrome!");
}
}
I HAVE UNDERLINED THE LINE THAT I LAST TRACED TO BE THE PROBLEM
public class PChecker implements Comparable<String> {
private String myStr1;
private String myStr2;
private String[] sort;
public boolean checker;
public int compare;
public String newStr = null; //this is where new string will be stored
public PChecker(String str1, String str2){
myStr1 = str1;
myStr2 = str2;
sort = new String[getSpace()];
}
private int getSpace() {
int space = 1; //if no spaces, just word, needs 1 space for it, zero will cause error
for(int x = 0; x < myStr2.length(); x++){
if(myStr2.substring(x, x+1).equals(" "))
space++;
}
return space;
}
public boolean checker(){
compare = compareTo(resort());
if(compare == 0)
return checker = true;
else return checker = false;
}
public int compareTo(String str2) {
if (myStr1.equalsIgnoreCase(str2) ){
return 0;}
else return 1;
}
//my first awesome algorithm to sort ****. I'm awesome :D
public String resort(){
for (int x = 0; x < myStr2.length(); x++){ //for x is less than the length of string
final String BLANK = " ";
int count = 0; //count incremented for letters, and skipped for words
if(!(myStr2.substring(x, x+1).equals(BLANK))){ //store any non space/letters in array
sort[count] += myStr2.substring(x, x+1); //add them up in the same element of array
}
else if (myStr2.substring(x, x+1).equals(BLANK)){
count++;
sort[count] += BLANK;
count++;
}
}
for (int x = sort.length - 1; x == 0; x--)
[U]newStr += sort[x];
return newStr;[/U]
}
}
Edited by An Alien, 25 April 2011 - 09:32 AM.


Sign In
Create Account


Back to top









