Josh
Josh

Reputation: 163

Putting a file onto a linked list in java

I need help with putting strings from a file into a list (in Java). Here is my code:

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

public class MyProgram7{
public static void main(String[] args){

    //Declare Variables
    File file = new File ("data7_names_Fall_2011.txt");
    Scanner input = new Scanner(file);
    Prog7Methods pm = new Prog7Methods();

    MyList<String> list = new MyArrayList<String>();
  }
}

I want to put each name in the list, so that I can append and insert and such. Here is my file:

Lee Keith Austin Kacie Jason Sherri Jordan Corey Reginald Brian Taray Christopher Randy Henry Jeremy Robert Joshua Robert Eileen Cassandra Albert Russell Ethan Cameron Tyler Alex Kentrell rederic

I know I probably need to use a for loop but I just cannot figure out a way to go about this...

Upvotes: 0

Views: 1294

Answers (1)

Prince John Wesley
Prince John Wesley

Reputation: 63698

...
List<String> list = new ArrayList<String>();
// add to list
while (input.hasNext()) {
    list.add(input.next());
}
// display
for (String s : list) {
    System.out.println(s);
}

Upvotes: 1

Related Questions