I'm a beginner and need to convert an array which I have scanned from a .csv
, into objects. I have an array which holds the data but I need to create objects with it's values.
Below is the format of the .csv
:
Item No.,Description,Price
1234,square,10
4321,circle,5
String xfile = "****";
String input;
String inputArray[];
Scanner scan = new Scanner(new BufferedReader(new FileReader(xfile)));
input = scan.nextLine();
while (scan.hasNextLine())
{
input = scan.nextLine();
inputArray = input.split(",");
System.out.println(inputArray[2]);
}
Here is the code I have already, which reads into the .csv
file and puts it into inputArray[]
. I now need to convert inputArray[]
into an array of objects "Item" which has its own class with a constructor with "descrtiption", "itemNo" and "price" and also getters and setters.
The inputArray[]
print prints the entire list of things. I want to make each line of the .csv
a object, so to be able to set the item no, description and price of the item.
String xfile = "****";
String input;
String inputArray[];
List<Item> itemList = new ArrayList<Item>(); // to store the list of items
Scanner scan = new Scanner(new BufferedReader(new FileReader(xfile)));
input = scan.nextLine();
while (scan.hasNextLine())
{
input = scan.nextLine();
inputArray = input.split(",");
System.out.println(inputArray[2]);
// assuming your item number and price are ints
itemList.add(new Item(inputArray[1], Integer.parseInt(inputArray[0]), Integer.parseInt(inputArray[2])));
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다