samedi 18 octobre 2014

How to read int,text and double from a file and store it into an arraylist? JAVA


Vote count:

0




What I am trying to do is read a text file which has this lines


232131231, random name, 23.3232


I only know how to read it as a String of the whole line. I don't know how to read them individually. This is my test codes.


main



public static void main(String[] args) throws FileNotFoundException
{
int x = 0;
ArrayList<String> line = new ArrayList();
Scanner fileScanner = new Scanner(new File("C:\\Users\\LENOVO\\Downloads\\Products (1).csv"));
while (fileScanner.hasNextLine())
{
line.add(x,fileScanner.nextLine());
x++;
}



ArrayList <Inventory> productList = new ArrayList<> ();

productList.add(new Inventory (line.get(0)));

for (Inventory e : productList)
{
System.out.println (e.getName());
}
}


and my Inventory class



public class Inventory
{
String name;
public Inventory (String pname)
{

name = pname;
}

public String getName ()
{
return name;
}
}


I know the codes only read string and prints out the first line. I was just testing it anyway. It worked out. But that's not what I want to do. I want to read int,string,double and pass it on into Inventory class. Any ideas what should I do?



asked 1 min ago







How to read int,text and double from a file and store it into an arraylist? JAVA

Aucun commentaire:

Enregistrer un commentaire