0

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));




}

}

11
  • Can you please remove all the comments and stuff you changed to make it work and actually show us the code that's -not- working? It's kinda hard to tell what here is just the way it is because you changed it to make it work. Commented Jul 11, 2013 at 23:35
  • Also, what is Names nameslist[] = new Names[1000]; supposed to be, that's not valid Java, far as I know. Commented Jul 11, 2013 at 23:38
  • One more thing, is Names a class for -one- name (in that case, why is it plural?) or what? Commented Jul 11, 2013 at 23:39
  • Ok I think I removed all the comments for you; my theory was to import the file which contains 1000 names and phone numbers and then create a new Name object by sending that information to the Name class which I have added above. The nameslist[] was used to create a new list that I could search through; I may be completely off on this. And I apologize Names was supposed to be Name I just accidentally wrote it that way and went with it. I'll change it. Commented Jul 11, 2013 at 23:46
  • Once I have the names on a list; I have to then be able to search through it using binary search by the name and return the phone number or the other way around. Commented Jul 11, 2013 at 23:49

1 Answer 1

1

Because you are inserting the name data into index [0] of your nameslist array every time, so every loop you are replacing your previous data and at the very end you end up with one item that equals your last entry. You need to assign the proper array index to each.

nameslist[i] = new Names(first, last, number);
Sign up to request clarification or add additional context in comments.

3 Comments

well I had the namelist[0] there just to test I was reading the file correctly; if I use the 0 I get the first name from the directory; However if I use the namelist[i] it jumps to the last name and prints the other 999 spaces null.
and I don't quite no how to to work this "if your Names class can be assigned new values without creating a new object, I would do that too."
yeah, forget about that last one, I've been working a bit in c++ so I got a little confused, it's not the case here. I've deleted it.

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.