0

The error this word scrambler program gives back in the UNIX terminal after compiling and running as 'java' is

"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0"

This is even after researching how to use "FileInputStream(args[0]))" and after constant debugging in 'jbd'

What this program is supposed to do is take a random word from a text file and then scramble it.

The text file "list.txt" reads exactly like this:

5
Apple
Pie
Horse
Computer
Tree

Here's the code:

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

class AnagramGenerator {
    public static void main(String[] args) throws FileNotFoundException {
        Random rand = new Random(1234);
        Scanner in = new Scanner(new FileInputStream(args[0]));

        int size = in.nextInt();
        int selection = rand.nextInt(size);
        int i = 0;
        while (i < selection) {
            in.next();
            i++;
        }
        String word = in.next();

        StringBuffer scrambled = scramble(word, rand);
        System.out.println(scrambled);
    }

        while(sbuf.length() > 0) {
            int next = rand.nextInt(sbuf.length());
            result.append(sbuf.charAt(next));
            sbuf.deleteCharAt(next);
        }
        return result;
    }
}

Thank you in advance.

[EDIT]: Coincidentally, I looked at the above link showing how to prevent such an error. The difference between this post and the other one is that this one scans directly from a text file (the first item is the number of words in the file) while the other one uses an array within the JAVA file. Sorry for this confusion.

J. Doe
  • 31
  • 6

0 Answers0