James Lockhart
James Lockhart

Reputation: 141

trying to compare lines of code in a text file

I am currently trying to compare the lines in a textfile to find the shortest line and longest line and display how many characters are in each. The code I have listed below allows me to count all the character, words, and lines. I have no idea where to start comparing the lines? Any help would be appreciated.

import java.util.Scanner;
import java.io.*;
public class Test{

public static void main(String [] args){

System.out.println("Please enter the filename: ");
Scanner input = new Scanner(System.in);

String fileName = input.nextLine();

 FileReader fReader;
    try {
        fReader = new FileReader(fileName);
        BufferedReader reader = new BufferedReader(fReader);
        String cursor; // 
        String content = "";
        int lines = 0;
        int words = 0;
        int chars = 0;


        while((cursor = reader.readLine()) != null){
            // count lines
            lines += 1;
            content += cursor;

            // count words
            String []_words = cursor.split(" ");
            for( String w : _words)
            {
              words++;        
            }

        }
        chars = content.length();

        System.out.println("The filename is " + fileName);
        System.out.println(chars + " Characters,");
        System.out.println(words + " words and " + lines + " lines.");


    } catch (FileNotFoundException ex) {
       // Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        System.out.println("File not found!");
    } catch (IOException ex) {
        //Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
         System.out.println("An error has occured: " + ex.getMessage());
    }
}

}

Upvotes: 0

Views: 496

Answers (2)

JasonKretzer
JasonKretzer

Reputation: 192

You need 2 String variables, one to hold the shortest String and one to hold the longest String. Then as you process each line, compare the length of the current line to the shortest/longest.

If it is shorter than your shortest String, set the shortest String to the current line.

else

If it is longer than your longest String, set the longest String to the current line.

Process the results at the end on those two String variables.

Upvotes: 0

Jordi Castilla
Jordi Castilla

Reputation: 26961

You must create 2 vars to store short and long lines...

String longest = "";
String shortest = "";

Then in your existing code, compare with current line:

while((cursor = reader.readLine()) != null){
    // compare shortest and longest.
    int currentSize = cursor.lenght;

    if (currentSize > longest.lenght || longest.equals("")) {
        longest = cursor;
    } else if (currentSize < shortest.lenght || longest.equals("")) {
        shortest = cursor;
    }

    // count lines
    lines += 1;
    content += cursor;

    // count words
    String []_words = cursor.split(" ");
    for( String w : _words)
    {
        words++;        
    }

}

After the loop you can do what you need with results:

 System.out.println("Longest line has " + longest.lenght);
 System.out.println("Shortest line has " + shortest.lenght);

If you only need the sizes and not the lines you can create int variables.

int longest = 0;
int shortest = 0;

// then inside the loop
int currentSize = cursor.lenght;

if (currentSize > longest || currentSize = 0) {
    longest = currentSize;
} else if (currentSize < shortest || currentSize = 0) {
    shortest = currentSize;
}

Upvotes: 1

Related Questions