4

I'm trying to write some code that scans for palindromes in an input file, but it gets strings from each word instead of each line. An example would be racecar would show up as racecar= palindrome or too hot to hoot = palindrome but instead it will go too= not a palindrome, hot= not a palindrome etc.

Here is what I am doing to read the file currently

File inputFile = new File( "c:/temp/palindromes.txt" );
Scanner inputScanner = new Scanner( inputFile );
while (inputScanner.hasNext())
{
    dirtyString = inputScanner.next();

    String cleanString = dirtyString.replaceAll("[^a-zA-Z]+", "");

    int length  = cleanString.length();
    int i, begin, end, middle;

    begin  = 0;
    end    = length - 1;
    middle = (begin + end)/2;

    for (i = begin; i <= middle; i++) {
        if (cleanString.charAt(begin) == cleanString.charAt(end)) {
            begin++;
            end--;
        }
        else {
            break;
        }
    }
}
Bill Woodger
  • 12,702
  • 4
  • 35
  • 43
  • 3
    Did you read `Scanner` api? Perhaps this method solve [Scanner#nextLine()](http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextLine()) – nachokk Dec 02 '13 at 00:07
  • Reading a file? I definitely would use a [`BufferedReader`](http://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html) – Justin Dec 02 '13 at 00:08
  • You should always format your code. I made an edit to it, and since the code wasn't complete, I had to assume that you meant to add two `}`. If you format your code, you will be able to find such errors. – Justin Dec 02 '13 at 00:12

3 Answers3

3

You need to do the following changes

change

while (inputScanner.hasNext()) // This will check the next token.

and 

dirtyString  = inputScanner.next(); // This will read the next token value.

to

while (inputScanner.hasNextLine()) // This will check the next line.

and dirtyString = inputScanner.nextLine(); // This will read the next line value.

inputScanner.next() will read the next token

inputScanner.nextLine() will read a single line.

Prabhakaran Ramaswamy
  • 23,910
  • 10
  • 51
  • 62
1

To read a line from a file you should use the nextLine() methond rather than the next() method.

The difference between the two is

nextLine() - Advances this scanner past the current line and returns the input that was skipped.

While

next() - Finds and returns the next complete token from this scanner.

So you'll have to change your while statement to include nextLine() so it would look like this.

while (inputScanner.hasNextLine()) and dirtyString = inputScanner.nextLine();
user2612619
  • 1,057
  • 3
  • 11
  • 26
0
FileReader f = new FileReader(file);
BufferedReader bufferReader = new BufferedReader(f);
String line;
//Read file line by line and print on the console
while ((line = bufferReader.readLine()) != null)   {
        System.out.println(line);
}

The above code segment reads input from file line by line, if it is not clear, please see this for complete program code

cruze
  • 21
  • 1