I have a text file that looks like this,

John, 27, Kids, Married
Jane, 22, No Kids, Single

and so on...

I have read in the text file as seen below,

Scanner scan = new Scanner(System.in);

System.out.print ("Enter the name of the input file: ");
fileName = scan.nextLine();
fileScan = new Scanner(new File("in.dat"));

while (fileScan.hasNext())

lineScan = new Scanner (fileName);
String name=lineScan.next();
String team = lineScan.next();
int year = scan.nextInt();
String condition = lineScan.next();
double price = scan.nextInt();

What I am trying to do is figure out how I can scroll down through the text file and add each line to an array.

My Array would have i.e MyArray[] = (String name,String team,int year ,String condition ,double price )

I am totally confused on how to get that text file into an array...

Edited by Nick Evan: Fixed formatting

13 Years
Discussion Span
Last Post by paradox814

well if we are trying to do that, then i would avoid putting a delimeter of a comma, since that will break up the lines at every single comma. A good delimiter would be the end of line signal, \n (for window systems)
(if the file is saved in *nix it would be \r\n )

String line = sc.next();

once you have all the lines of code in an array if you wanted to seperate them you could simply use StringTokenizer

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.