Results 1 to 8 of 8
Thread: Can someone help with me code?
- 10-23-2011, 02:35 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 7
- Rep Power
- 0
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:
So just a load of random words pretty much. But the palindromes are: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
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
Any help will be appreciated. Thanks :DJava 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){} } }
- 10-23-2011, 02:46 PM #2
Member
- Join Date
- Oct 2011
- Posts
- 15
- Rep Power
- 0
Re: Can someone help with me code?
You could try using
instead of two for.Java Code://For each string in string array for(String str: m) { //Check if it is palindrome if(str == str.reverse()) counter++ }
- 10-23-2011, 03:05 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 7
- Rep Power
- 0
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?
- 10-23-2011, 07:20 PM #5
Member
- Join Date
- Oct 2011
- Posts
- 7
- Rep Power
- 0
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
- 10-23-2011, 07:55 PM #6
Member
- Join Date
- Oct 2011
- Posts
- 4
- Rep Power
- 0
-
Re: Can someone help with me code?
- 10-24-2011, 02:24 AM #8
Member
- Join Date
- Oct 2011
- Posts
- 15
- Rep Power
- 0
Similar Threads
-
My code was not executed properly.It will jumping to exception handling.my code is
By vinay4051 in forum EclipseReplies: 3Last Post: 08-10-2011, 09:17 AM -
servlet include method copying sorce code and executing source code as output how to
By shamkuma2k in forum Advanced JavaReplies: 0Last Post: 08-07-2011, 08:32 PM -
C server code - Java CLient Code _ TCP Connection Problem
By rmd22 in forum NetworkingReplies: 0Last Post: 02-21-2011, 11:50 AM -
can any one pls send me a sample code for calling a jsp code in swings
By sniffer139 in forum AWT / SwingReplies: 1Last Post: 03-04-2010, 11:19 AM -
Generating Code Automatically Using Custom code Template In Eclipse
By JavaForums in forum EclipseReplies: 1Last Post: 04-26-2007, 03:52 PM


LinkBack URL
About LinkBacks


Bookmarks