So I'm trying to add a names to a new list from a file called directory.txt that has 1000 objects that contain a first name, last name, and a phone number; something like this (Dodge, Nick 765-123-2312). When I run the program below without a "for loop" I can add the first object off the .txt file successfully and it prints it out. However when I add a for loop like, for(int i =0; i < 1000; i++), it for some reason jumps to the end off the list and inputs the 1000 object in the first spot and skips the rest. I can't figure this out! Thanks for the help.
new code;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import bsu.edu.cs121.names.Names;
import bsu.edu.cs121.quickSort.QuickSort;
public class NameTester {
public static void main(String[] args)throws FileNotFoundException {
ArrayList<Names> namelist= new ArrayList<Names>();
Scanner file = new Scanner(System.in);
System.out.println("Please enter the name of the phone book file: ");
String newFile = file.next();
File inputFile = new File("/Users/Latif/Desktop/workspace/CS121 Project4/src/" + newFile);
Scanner readFile = new Scanner(inputFile);
while (readFile.hasNextLine()){ //start while
String lastName = readFile.next();
String firstName = readFile.nextLine();
String phoneNumber = readFile.nextLine();
namelist.add(new Names(firstName, lastName, phoneNumber));
}
QuickSort newSort = new QuickSort(namelist);
System.out.println(namelist.get(1) + " " + namelist.get(2));
}
}
Names nameslist[] = new Names[1000];supposed to be, that's not valid Java, far as I know.Namesa class for -one- name (in that case, why is it plural?) or what?