| | |
Palindromes
![]() |
•
•
Join Date: Dec 2008
Posts: 11
Reputation:
Solved Threads: 0
The program is intended to read a file with a few lines of strings to determine whether or not they are palindromes. I'm getting an error while compiling - tagged where the error shows up. I'm not sure whether or not it is going to read the lines correctly from the file or not either since I can't compile it.
[code]
import java.io.*;
public class Palindromes
{
public static BufferedReader inFile;
public static void main (String[]args)throws IOException
{
inFile = new BufferedReader(new FileReader("palindrome.dat"));
System.out.println("Enter a word or phrase: ");
String s = inFile.readLine();
s = s.toLowerCase();
String toTest = s;
int index;
for(int index = 0;index < s.length();index++)
{
if(Character.isLetterOrDigit(s.charAt(index))
cleanStr += s.charAt(index); //error shows up here - it is looking for a "{" ?
}
if(isPalindrome(toTest))
System.out.println(s + " IS A PALINDROME");
else
System.out.println(s + " IS NOT A PALINDROME");
}
public static boolean isPalindrome(String s)
{
if (s.length() <= 1)
return true; // Base case
else
{
if (s.charAt(0) == s.charAt(s.length() - 1))
return isPalindrome(s.substring(1, s.length() - 1 ) );
else
return false;
}
}
[code]
[code]
import java.io.*;
public class Palindromes
{
public static BufferedReader inFile;
public static void main (String[]args)throws IOException
{
inFile = new BufferedReader(new FileReader("palindrome.dat"));
System.out.println("Enter a word or phrase: ");
String s = inFile.readLine();
s = s.toLowerCase();
String toTest = s;
int index;
for(int index = 0;index < s.length();index++)
{
if(Character.isLetterOrDigit(s.charAt(index))
cleanStr += s.charAt(index); //error shows up here - it is looking for a "{" ?
}
if(isPalindrome(toTest))
System.out.println(s + " IS A PALINDROME");
else
System.out.println(s + " IS NOT A PALINDROME");
}
public static boolean isPalindrome(String s)
{
if (s.length() <= 1)
return true; // Base case
else
{
if (s.charAt(0) == s.charAt(s.length() - 1))
return isPalindrome(s.substring(1, s.length() - 1 ) );
else
return false;
}
}
[code]
•
•
Join Date: Aug 2006
Posts: 137
Reputation:
Solved Threads: 11
Hi there, I'm not entirely sure why you're running that for loop that contains the undeclared variable. Remember that the end of line terminator is not returned by the readLine() method of BufferedReader. Also, if you just want to get rid of leading/following whitespaces (like a space) then you can use the trim() method of the String class: s = s.trim()
You don't even use the cleanStr as the parameter of your static method.
One comment about the recursion you've used, in a programming language such as Java, recursion is not always a better solution than a for loop. But I'm guessing the question required the use of recursion? Anyway, don't get too frustrated, Java is rather beautiful
You don't even use the cleanStr as the parameter of your static method.
One comment about the recursion you've used, in a programming language such as Java, recursion is not always a better solution than a for loop. But I'm guessing the question required the use of recursion? Anyway, don't get too frustrated, Java is rather beautiful
•
•
Join Date: Dec 2008
Posts: 11
Reputation:
Solved Threads: 0
This is my program now -- and yes unfornately I had to use recursion. I got the program working except it is only reading the first line of the file and I'm not sure where to put another readLine for it to read the rest of the file. My revised program
Java Syntax (Toggle Plain Text)
import java.io.*; import java.util.*; public class PalindromesHelp { public static BufferedReader inFile; public static void main (String[]args)throws IOException { inFile = new BufferedReader(new FileReader("palindrome.dat")); String s = inFile.readLine(); s = s.toLowerCase(); String toTest = ""; for(int index = 0;index < s.length();index++) { if(Character.isLetterOrDigit(s.charAt(index))) toTest= toTest.toLowerCase(); } if(isPalindrome(toTest)) System.out.println(s + " IS A PALINDROME"); else System.out.println(s + " IS NOT A PALINDROME"); } public static boolean isPalindrome(String s) { if (s.length() <= 1) return true; // Base case else { if (s.charAt(0) == s.charAt(s.length() - 1)) return isPalindrome(s.substring(1, s.length() - 1 ) ); else return false; } } }
•
•
Join Date: Aug 2006
Posts: 137
Reputation:
Solved Threads: 11
Oh yes I was actually going to comment on that. As it stands your program would only cater for the first line because you're only asking it to read the first line. In order to read the entire file what structure do you think would be required?
On that note, a reminder that the readLine() method returns null once you're reached the end of the file. Well that's actually more of a hint than a reminder hehe
Oh and something that you might want to look into is the Scanner class: it makes reading data from a file so much easier!
On that note, a reminder that the readLine() method returns null once you're reached the end of the file. Well that's actually more of a hint than a reminder hehe
Oh and something that you might want to look into is the Scanner class: it makes reading data from a file so much easier!
Last edited by PoovenM; Dec 17th, 2008 at 4:36 pm.
•
•
Join Date: Aug 2006
Posts: 137
Reputation:
Solved Threads: 11
Aww I can see that you need sleep hehe you'd need a looping structure to read the entire file! I guess I could have given you a better hint!
The basic idea is that you'd read from the file while the input is not null. What you'd do is use a loop that looked something like this:
Where ... refers to the code after the line String s = inFile.readLine();
If you wanted to be a bit more fancy you could have just had:
If you need help understanding this, please let me know. The basic idea of a loop is to initialize, check the test variable/condition and finally to change the test variable/condition within the loop body. Using this, you'd never go wrong!
The basic idea is that you'd read from the file while the input is not null. What you'd do is use a loop that looked something like this:
java Syntax (Toggle Plain Text)
String s = inFile.readLine(); //the loop should start after this line while (s !=null) { ... s = inFile.readLine(); //here you change the value of s for checking }
If you wanted to be a bit more fancy you could have just had:
java Syntax (Toggle Plain Text)
while(s=readLine() != null) { ...//notice that s is not updated in the body of the loop }
If you need help understanding this, please let me know. The basic idea of a loop is to initialize, check the test variable/condition and finally to change the test variable/condition within the loop body. Using this, you'd never go wrong!
•
•
Join Date: Dec 2008
Posts: 11
Reputation:
Solved Threads: 0
I am actually getting a compiling error now at the start of my boolean saying it is an illegal start of an expression?????
Java Syntax (Toggle Plain Text)
inFile = new BufferedReader(new FileReader("palindrome.dat")); String s = inFile.readLine(); while( s !=null) { s=inFile.readLine(); } String lowCase = s.toLowerCase(); String toTest = ""; int index; for(int index = 0;index < s.length();index++) { if(Character.isLetterOrDigit(s.charAt(index))) toTest= toTest.toLowerCase(); } if(isPalindrome(toTest)) System.out.println(s + " IS A PALINDROME"); else System.out.println(s + " IS NOT A PALINDROME"); } public static boolean isPalindrome(s) { if (s.length() <= 1) return true; // Base case else { if (s.charAt(0) == s.charAt(s.length() - 1)) return isPalindrome(s.substring(1, s.length() - 1 ) ); else return false; } } }
![]() |
Similar Threads
- Help with palindromes (C++)
- Palindromes...but determining using variables (C)
- 5 digit palindromes Array (Java)
- I created another Stack that tells if word is palindrome, need help (Java)
- Stack- need help (Java)
- palindrome (Java)
- Scan text file to find all words of 4 characters or less (Shell Scripting)
- creating our own C++ strcat() function, code reqd (C++)
- palendromes with a string tokeniser (Java)
Other Threads in the Java Forum
- Previous Thread: Java Beginner Need Help!
- Next Thread: Problem with Arrays
| Thread Tools | Search this Thread |
6 actuate android api applet application applications array arrays automation balls bank binary bluetooth business c++ chat class clear client code codesnippet collections component coordinates database defaultmethod development dice doctype dragging ebook eclipse educational error formatingtextintooltipjava fractal froglogic game givemetehcodez graphics gui hql html ide ideas image infinite ingres integer internet intersect invokingapacheantprogrammatically j2me java javaprojects jni jpanel jtextarea julia linux list map method methods mobile mysql netbeans newbie openjavafx parameter php problem program programming project recursion recursive repositories scanner scrollbar sell server set sms sort sorting sql sqlserver state storm string sun superclass swing swt threads tree websites windows






