Can someone help with me code?
Hi, all. So new to programming, just doing it as a hobby. But at the moment, I am trying to make one piece of code. So, what I want it do do, is, open up a text file, then check, identify, and count the number of palindromes (Words the same backwards e.g. did, abba, otto). But I am doing something wrong with that code.
I have a text file. It has 10 palindromes in it. Here is what it is:
Quote:
zerimar ramirez reviver of lawns from wassamassaw south carolina used a rotavator to cut through the marram grass invading gardens near the beach
otto sees otto said otto as he passed a mirror He did eh responded his mother avid diva his cousin exclaimed repel a leper on seeing his hand I can cure that said his grandmother from navan meath don't be silly they're not straw warts they won't rot from your witch-doctoring replied otto's mother
So just a load of random words pretty much. But the palindromes are:
reviver
wassamassaw
a
rotavator
marram
otto
sees
did
navan
i
But, in my code, when I run it, it is very wrong.
Firstly, it picks words together, and says they are a ppalindrome, example is avid diva. It is appearing that this is 2 palindromes. When it isnt even one.
It is also, something I'll need to add or something, but its bringing palindromes that I have more than one of, and counting for each one. I want it, so, for example, Otto, is only counted once, and not three times.
And also, when the window comes up, the result, does it twice, with different results thrice. Im not explaining this part well, but even if the other 2 were to be corrected, I'd be extremely happy.
Here is the code I have now
Code:
import java.io.*;
public class PC {
public static void main(String[] args) {
try {
FileInputStream fstream = new FileInputStream("C:/Test1.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine = null;
while ((strLine = br.readLine()) != null) {
String reverse = new
StringBuffer(strLine).reverse().
toString();
int i,j,counter=0;
String m[]=strLine.split(" ");
String[] word=reverse.split(" ");
System.out.println("The palindrome words are:");
for(i=0;i<m.length;i++) {
for(j=word.length-1;j>=0;j--) {
if(m[i].equalsIgnoreCase(word[j])) {
System.out.println(m[i]);
counter++;
break;
}
}
}
System.out.println("Number of palindromes:"+counter);
}
}
catch(IOException e){}
}
}
Any help will be appreciated. Thanks :D
Re: Can someone help with me code?
You could try using
Code:
//For each string in string array
for(String str: m)
{
//Check if it is palindrome
if(str == str.reverse())
counter++
}
instead of two for.
Re: Can someone help with me code?
Hi, I did this, but there is a different problem now. It brings up the error:
The method reverse() is undefined for the type String
I am very new to this, so there is probably a simple explanation to this. Maybe I am going to fast...
Re: Can someone help with me code?
Quote:
Originally Posted by
dheaven
You could try using
Code:
//For each string in string array
for(String str: m)
{
//Check if it is palindrome
if(str == str.reverse())
counter++
}
instead of two for.
Why in the name of all things holy would you recommend that he use == to compare Strings? He was doing the String comparison right, and now you recommend he do it wrong??
Re: Can someone help with me code?
Anyone else have any more advice. Looks so many times over it, but I dunno what to do, but dont want to leave it :p
Re: Can someone help with me code?
Quote:
Originally Posted by
dheaven
You could try using
Code:
//For each string in string array
for(String str: m)
{
//Check if it is palindrome
if(str == str.reverse())
counter++
}
instead of two for.
It was my understanding that you should use .equals() when comparing strings. Is this wrong?
Code:
for(String str: m) {
if(str.equals(str.reverse()))
counter++
}
Re: Can someone help with me code?
Quote:
Originally Posted by
Kami
It was my understanding that you should use .equals() when comparing strings. Is this wrong?
Code:
for(String str: m) {
if(str.equals(str.reverse()))
counter++
}
You are right, your "helper" is most definitely wrong.
Re: Can someone help with me code?
Sorry about that, I wrote it in a hurry.
Still, I don't think the sarcasm was necessary (especially when you're a moderator).
The code should look like this:
Code:
for(String str: m) {
if(str.equals(new StringBuilder(str).reverse().toString()))
counter++;
}