-1

I'm Trying to create a code that takes a text file and puts it in alphabetical order. To do this I was trying to read the file and the add each word into an array. I have an idea of how to go about doing this but don't know exactly. Here is what I have so far:

import java.io.*;
import java.util.Scanner;

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

        if (args.length == 1){
            createArray(args[0]);
            System.exit(0);
        }

    }


    public static String createArray(String fileName) {
            File testFile = new File(fileName);
    Scanner inputFile = new Scanner(testFile);

    if (!testFile.exists()){
    System.out.println("File Doesn't Exist");
    System.exit(0);
    }

    String[] words;

    while(inputFile.hasNext()){
    for (int i=0;i<inputFile.length();i++){
    words[i] = inputFile.nextLine();
    }
    }

    return words[0];

    }
}

I know the majority is probably completely wrong but I'm so confused been working on this for 4 hours now...

Mike
  • 374
  • 1
  • 8
  • 22

2 Answers2

2
words[i] = inputFile.nextLine();

Here you are trying to store the next line from the input file into the index i of the words array. You have not declared or assigned a value to i, so Java won't know what you're trying to do.

With standard arrays, you must assign them an initial array value consisting of an explicit number of "slots" (indexes). With a Scanner, you could count the number of lines by reading them all and discarding the values. Once you have this counter, you can initialise the String[] with the appropriate size. Finally, you can read them all again and storing them into the array.

int counter = 0;

while (inputFile.hasNext()) {
    inputFile.nextLine();
    counter++;
}

inputFile = new Scanner(testFile); //to get to the beginning of the file

String[] words = new String[counter];

for (int i = 0; i < counter; i++) {
    words[i] = inputFile.nextLine();
}

This is very bad practice; reading the whole file merely to find its length is an overkill, and a waste of resources.

Therefore, it would be much better to use a collection type that automatically expands as you put elements into it, such as an ArrayList.

ArrayList<String> lines = new ArrayList<String>();

while (inputFile.hasNext()) {
    lines.add(inputFile.nextLine());
}

But it's likely that you're assignment requires you to use both Scanner and a standard String[]. In that case, you could change the size of the String[] manually:

String[] words = new String[0];

while (inputFile.hasNext()) {
    words = Arrays.copyOf(words, words.length + 1);
    words[words.length - 1] = inputFile.nextLine();
}

or

String[] words = new String[0];

while (inputFile.hasNext()) {
    String temp = new String[words.length + 1];
    System.arraycopy(words, 0, temp, 0, words.length);
    temp[temp.length - 1] = inputFile.nextLine();
    words = temp;
}
Spooky
  • 2,966
  • 8
  • 27
  • 41
  • What should I return? – Mike Oct 13 '13 at 01:36
  • @MikeOles Your method declaration (`public static String createArray(String fileName)`) states that Java should expect the method to return a `String`. However, if you wish to sort the array inside your `main` method, you'd want to return a `String[]` or an `ArrayList` (if you used the `ArrayList` example). Don't forget to update your method declaration accordingly. If you decide to return a `String[]`, you should use `return words;`. – Spooky Oct 13 '13 at 01:42
0

ArrayList words = new ArrayList(); while (inputFile.hasNextLine()){ String word = inputFile.getNextLine(); words.add(word); }

Since you don't know how big this is, you should use an arrayList. You can then sort alphabetically or whatever you need.

William Falcon
  • 9,813
  • 14
  • 67
  • 110
  • We havent learned how to use ArrayList in our class – Mike Oct 13 '13 at 01:08
  • Ok. You should tag this as HW... You can do the same with an array but you need to know how many strings you'll have. The main problem in your code is that you never increase i, so you just rewrite the string over and over again and end up with just 1 string. Also this reads one line at a time, you need to break up the string into a string array then add each of those words to your main array – William Falcon Oct 13 '13 at 01:09
  • Ok. I added a for loop to increment i but I don't know if the inputFile.length() works right. How else would I find out how many words are in the file? – Mike Oct 13 '13 at 01:15