The code will work if compiled in terminal.
It does not work if I use Eclipse, it simply gives me an Array Out of bounds.
It mentions the ARGS[0] as the out of bounds part.
Any suggestions?
/*
*
* Test Case 1:
1. James
2. David
3. Melanie
4. Evan
5. Blythe
6. John
7. Chris
8. Barry
9. Jasmine
10. Racheal
11. Natalie
12. Rusty
*/
import java.io.*;
import java.util.*;
public class Ch00a {
//This variable will set the size of the array.
public static int MAXNAMES = 3000;
//This will help us keep count of the array later on
public int counter = 0;
//This will help us keep track of what position we are printing.
public int position = 0;
/*
* We
*/
public void readName(String fileName, String names[]) {
try {
File file = new File(fileName);
Scanner nameReader = new Scanner(file);
while (nameReader.hasNext()){
String name = nameReader.next();
char firstLetter = name.charAt(0);
if (firstLetter <= 'M')
{
System.out.println(counter + ". " + name);
counter++;
} else {
names[position] = name;
position++;
}
}
}
catch (FileNotFoundException e) {
System.err.println("Error, the file can not be located.");
System.err.println("Please try again");
return;
}
catch (Exception e) {
e.printStackTrace();
return;
}
}
/*
* We will write names from the array, starting at position 0.
* Increase the counter as we print the information, until
* the counter is no longer less than position.
*
* **note**While loop created error, used for loop instead
* do not change. **note**
*/
public void writeNames(String names[]) {
int posinArray = counter + 1;
for (int x = 0; x < position; x++)
{
System.out.println(posinArray + ". " + names[x]);
posinArray++;
}
}
public static void main(String args[])
{
String fileName = args[0];
String names[] = new String [MAXNAMES];
Ch00a dList = new Ch00a();
dList.readName(fileName, names);
dList.writeNames(names);
}
}