Array into an array of objects in Java

JoelArgls

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.

Jason
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] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Creating an array of objects with constructor in Java

분류에서Dev

Passing an array of objects to a method in java

분류에서Dev

Filter and uniq array of objects

분류에서Dev

Javascript array of objects undefined

분류에서Dev

creating array of objects

분류에서Dev

Compare objects in an array

분류에서Dev

Changing the css of objects in a array

분류에서Dev

Underscore JS difference on array and array with objects

분류에서Dev

How to push an array into an array of objects using AngularJs

분류에서Dev

Given array of parameters, make array from array of objects

분류에서Dev

Push() overwrites existing objects in an array

분류에서Dev

jOOQ JSON formatting as array of objects

분류에서Dev

Sort an array of objects by multiple properties

분류에서Dev

Changing object at NSMutable Array of Objects

분류에서Dev

Sort an array of objects by multiple properties

분류에서Dev

XNA drawing and creating an array of objects

분류에서Dev

Create dynamic array of objects for VBO

분류에서Dev

How to create and return an array of objects?

분류에서Dev

Performant way to filtering an array of objects

분류에서Dev

Populating Object array with new Objects

분류에서Dev

filter an array of objects based on an object with an array as it's property value in react

분류에서Dev

Angular ng-repeat binding to array of objects in array

분류에서Dev

How to combined array of objects one to another array using javascript

분류에서Dev

JavaScript: Add JSON objects from one array to another array conditionally

분류에서Dev

Pass a java array to javascript

분류에서Dev

Byte Array to Decimal Java

분류에서Dev

Boolean array comparisons Java

분류에서Dev

Linked List to Array in java

분류에서Dev

displaying a reversed array? java

Related 관련 기사

뜨겁다태그

보관