1

I have this code to sort a text file using arrays in java, but it always discard the first line of the text while sorting.

Here is my code:

import java.io.*;

public class Main {

    public static int count(String filename) throws IOException {
        InputStream is = new BufferedInputStream(new FileInputStream(filename));
        try {
            byte[] c = new byte[1024];
            int count = 0;
            int readChars = 0;
            while ((readChars = is.read(c)) != -1) {
                for (int i = 0; i < readChars; ++i) {
                    if (c[i] == '\n') {
                        ++count;
                    }
                }
            }
            return count;
        } finally {
            is.close();
        }
    }

    public static String[] getContents(File aFile) throws IOException {

        String[] words = new String[count(aFile.getName()) + 1];

        BufferedReader input = new BufferedReader(new FileReader(aFile));

        String line = null; //not declared within while loop
        int i = 0;
        while ((line = input.readLine()) != null) {
            words[i] = line;
            i++;
        }

        java.util.Arrays.sort(words);
        for (int k = 0; k < words.length; k++) {
            System.out.println(words[k]);
        }
        return words;
    }

    public static void main(String[] args) throws IOException {

        File testFile = new File("try.txt");
        getContents(testFile);

    }
}

Here is the text file try.txt:

Daisy
Jane
Amanda
Barbara
Alexandra
Ezabile

the output is:

Alexandra
Amanda
Barbara
Ezabile
Jane
Daisy

To solve this problem I have to insert an empty line in the beginning of the text file, is there a way not to do that? I don't know what goes wrong?

4
  • 1
    open the text file in plain text editor there should be some character around. ? Commented Nov 26, 2011 at 13:16
  • 2
    also List<String> is more suitable to remove the overhead of count() Commented Nov 26, 2011 at 13:16
  • 2
    I run you code on windows I don't need to put an empty line in the file to get all the lines correctly sorted. Commented Nov 26, 2011 at 13:23
  • Thank you, the problem was that i have set the encoding of the text file to UTF-8 to read either english or arabic. Commented Nov 26, 2011 at 13:46

4 Answers 4

2

I compiled your code (on a Mac) and it works for me. Try opening the file in a hexeditor and see if there is some special character at the beginning of your file. That might be causing the sorting to be incorrect for the first line.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, the problem was that i have set the encoding of the text file to UTF-8 to read either english or arabic.
1

You probably have a BOM (Byte Order Marker) at the beginning at the file. By definition they will be interpreted as zero-width non-breaking-space.

So if you have

    String textA = new String(new byte[] { (byte)0xef, (byte)0xbb, (byte) 0xbf, 65}, "UTF-8");
    String textB = new String(new byte[] { 66}, "UTF-8");
    System.err.println(textA + " < " + textB + " = " + (textA.compareTo(textB) < 0));

The character should show up in your length of the strings, so try printing the length of each line.

            System.out.println(words[k] + " " + words[k].length());

And use a list or some other structure so you don't have to read the file twice.

Comments

1

Try something simpler, like this:

public static String[] getContents(File aFile) throws IOException {

    List<String> words = new ArrayList<String>();
    BufferedReader input = new BufferedReader(new FileReader(aFile));

    String line;
    while ((line = input.readLine()) != null)
        words.add(line);

    Collections.sort(words);
    return words.toArray(new String[words.size()]);

}

public static void main(String[] args) throws IOException {

    File testFile = new File("try.txt");
    String[] contents = getContents(testFile);
    for (int k = 0; k < contents.length; k++) {
        System.out.println(contents[k]);
    }

}

Notice that you don't have to iterate over the file to determine how many lines it has, instead I'm adding the lines to an ArrayList, and at the end, converting it to an array.

1 Comment

Thank you, it is much simpler, also I noticed that the error is because using the UTF-8 encoding for my text file.
0

Use List and the add() method to read your file contents. Then use Collections.sort() to sort the List.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.