Re: Testing for a Palindrome
Quote:
where "pal" method is executed is incorrect,
Please show what your code does and explain what is incorrect.
Please edit your post, select the code and wrap it in code tags. Use the Go Advanced button and then the #icon.
Re: Testing for a Palindrome
I'm not sure how to use the advanced.. but here goes..
so this is the part that isn't correct:
} else if (function.equals("pal")){
text = palindrome(String text);
the other part is this:
if (charLeft == charRight){
left++;
right--;}
if Character.isLetter(charLeft){
left++;}
if Character.isLetter(charRight){
right--;}
else break;
}
what i want it to do is to skip any periods, commas, spaces, etc so that it is only comparing characters. so if my text input is "madam, i'm adam" it will return saying "this is a palindrome"
Re: Testing for a Palindrome
String str = "hello";
System.out.println(String str);
Surely you can see what is wrong in the above code.
Re: Testing for a Palindrome
should it be:
String str = "hello";
System.out.println(str);
Re: Testing for a Palindrome
Quote:
I'm not sure how to use the advanced.
There is a button below the input box with a label "Go Advanced" >>>>>>>>>>>>>> press it.
Quote:
his is the part that isn't correct:
What is the code doing now? what are the values of charLeft and charRight as the code is executed?
What value is returned by the isLetter() methods?
Do some debugging by adding printlns to your code to see what the values for the above are.
Print them out so you can see what your code is seeing.
Re: Testing for a Palindrome
i've been working with the debugger for hours now..
i've changed it a bit but here is what i'm getting for a response:
Code :
Enter the text to be edited: atoyota
Edit: pal
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Syntax error on token "if", new expected
Character.isLetter cannot be resolved to a type
Syntax error, insert "}" to complete ClassBody
Syntax error, insert ";" to complete Statement
Syntax error on token "if", new expected
Syntax error on token "--", invalid VariableDeclarator
Syntax error on token "else", ; expected
Syntax error on token "}", delete this token
at Assignment6.part2.palindrome(part2.java:119)
at Assignment6.part2.main(part2.java:38)
for some reason it doesn't like my if statement? i'm sure this is just how i'm writing it, not so much that its completely wrong
Re: Testing for a Palindrome
Where is the if statement that the compiler is having problems with?
Re: Testing for a Palindrome
ok i'm moving forward... i got it to run all the way through.. but it now thinks that everything is a palindrone.. leading me to believe that it is skipping parts.. here is the updated code:
Code :
package Assignment6;
import java.util.Scanner;
/**
* Name: Ryan Sampson
* Course: COS 160 TuTh 4:10-5:30
* Project: String Processing
* Filename: part2.java
* */
public class part2 {
public static void main(String[] args) {
System.out.print("Enter the text to be edited: ");
Scanner console = new Scanner(System.in); //Creates console scanner
String text = console.nextLine(); // input string Text (initial text)
System.out.print("Edit: ");
String command = console.nextLine(); // Command string input
String function = command.substring(0,3).toLowerCase(); //takes first three letters and makes "function"
String pars = command.substring(3).trim(); //creates "pars" as the rest of the string, this is the parameters
//works up to insert
while (!function.equals("sto")){ //creates while and creates stop point (sto)
if (function.equals("ins")){ //if it equals insert
text = insert(text, pars); // use text to insert parameters goes to INSERT METHOD
System.out.println("The changed text is: " + text);} //prints the updated text
else if (function.equals("rep")){ //if it equals replace
text = replaceFirst(text,pars);
System.out.println("The changed text is: " + text);
}
else if (function.equals("del")) {
text = delete(text, pars);
System.out.println("The changed text is: " + text);
} else if (function.equals("pal")){
text = palindrome(text);
}
else {
System.out.println("The function " + function + " is not defined. Original text is not changed.");
System.out.println("The unchanged text is: " + text);
}
System.out.print("Edit: ");
command = console.nextLine();
function = command.substring(0,3).toLowerCase();
pars = command.substring(3).trim();
}
System.out.println("Program stopped");
}
public static String insert(String text, String parameters){
String first = getString(parameters);
parameters = parameters.replaceFirst("\"" + first + "\"","");
int index = Integer.parseInt(parameters.trim());
text = text.substring(0, index) + first + text.substring(index);
return text;
}
public static String replaceFirst(String text, String parameters){
String first = getString(parameters);
parameters = parameters.substring(parameters.indexOf(" "), parameters.length()).trim();
String second = getString(parameters);
text = text.replaceFirst(first,second);
return text;
}
public static String delete(String text, String parameters){
String firstNumber = parameters.substring(0, parameters.indexOf(" "));
int firstindex = Integer.parseInt(firstNumber);
parameters = parameters.substring(parameters.indexOf(" "), parameters.length()).trim();
int secondindex = Integer.parseInt(parameters.trim());
String subString1 = text.substring(0,firstindex);
String subString2 = text.substring(firstindex, text.length());
String subString3 = text.substring(firstindex, secondindex + 1);
subString2 = subString2.replaceFirst(subString3, " ");
text = subString1 + subString2;
return text;
}
public static String getString(String parameters){
int nextQuote = parameters.indexOf('"');
parameters = parameters.substring(nextQuote + 1);
nextQuote = parameters.indexOf('"');
String returnString = parameters.substring(0, nextQuote);
return returnString;
}
public static String palindrome(String text){
int left, right;
char charLeft, charRight;
text.toLowerCase();
{
left = 0;
right = text.length() - 1;
while (left < right){
charLeft = text.charAt(left);
charRight = text.charAt(right);
if (charLeft == charRight){
left++;
right--;}
else Character.isLetter(charLeft);{
left++;
Character.isLetter(charRight);
right--;}
}
System.out.println();
if (left < right)
System.out.println ("This text is not a palindrome: " + text);
else
System.out.println ("This text is a palindrome: " + text);
return text;
}
}
}
If i use text "madam, i'm adam" and run pal, it says its a palindrome.. which is correct. but it says that for everything.. not sure whats going on?? and i appreciate the help a ton guys!! thanks
Re: Testing for a Palindrome
You need to debug your code. Add some printlns to it so you can see the values that the program is seeing and using to make decisions. Print out the values of all the variables that it is using, especially left, right, charLeft and charRight.
Re: Testing for a Palindrome
so using the debugger... its going thru the letters correctly, the problem is it gets the left and it gets the right, but even though left == right is there, and if they aren't the same, it keeps going .. do i have something in the wrong order?
Re: Testing for a Palindrome
Quote:
do i have something in the wrong order?
That question should be answered by your debugging your code. Look at all the values and see which ones your code is not handling correctly.
Use a piece of paper to write down what the values should be.
Then compare them with the values the program sees when it is executed.
Re: Testing for a Palindrome
Your usage of {}s is terrible. You need to align the ending } vertically in the same column underneath the beginning of the statement with the starting {
Also useful is to add comments on the } that end loops and methods.