Having some issues with this... I've never done any java before but have an assignment to take a .txt and put it into an ArrayList and print it (plus other functions but I'll do those myself).
My code to put it into the arraylist
Code
public static ArrayList<String> initArrayOfStrings(String filePath, int num) {
int size = num;
ArrayList<String> lines = new ArrayList<String>();
File names = new File(filePath);
try {
Scanner scanner = new Scanner(names);
while (scanner.hasNextLine()) {
lines.add(scanner.nextLine());
// String line = scanner.nextLine(); - not in arraylist
}
scanner.close();
} catch (FileNotFoundException e) {
System.out.println("Can't find file");
}
return lines;
}
This just returns an ArrayList which is then passed to a function to print and I just have a standard system.out.println(arrayListname); to print it out.
The output I'm getting should have a string on every line... but instead it looks like this: all on one line and two spaces between each name.
name1 name2 name3 name4 etc etc
This post was edited by HoneyBadger on Sep 8 2014 03:04pm