简体   繁体   中英

Convert String into array in java

Below is my Java code. I'm passing a CSV file and reading the record. I want to split the records by "," and store it into an array. I don't know how to do that. If someone can give me idea about that. I'm pretty new to Java.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class parser 
{
    public static void main(String[] args) throws IOException 
    {
        FileReader fileReader = new FileReader(new File("//Users//Desktop//npi.csv"));
        BufferedReader br = new BufferedReader(fileReader);
        String line = null;
        //String[] value = line.split(",");

        /*ArrayList list = new ArrayList(Arrays.asList(line));
        System.out.println(list);*/
        int count = 0;
        while((line = br.readLine()) != null)
        {
            for(int i=0; i < line.length();i++)
            {

            }
            count ++;
            System.out.println("Record Count : "+count);
            System.out.println(line);
        }

    }
}

An example of String from the CSV file

"1234567","1","","","","ALI","DONALDSON","","DR.","","OD","","","","","","","","","","140-6002 SIT RD","STE A","EVANSVILLE","IN","8418996408","US","6013857469","9586771232","140-6002 SIT RD","SUITE A","EVANSVILLE","IN","770155917","US","6969551228","2403884068","04/13/2003","02/21/2018","","","","M","","","","","","142K01234Z","5376UD","IN","Y","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","980230944","03","IN","","WR09447","01","IN","","12532T","02","IN","GROUP","245699532","08","IN","","3HDPTO","09","IN","INDIVIDUAL","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","" ,"","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","X","","","","","","","","","","","","","","","","","","","","",""

使用“,”作为分隔符分割每一行,如下所示:

String[] records = line.split(",");

You have used Arrays.asList(line) but you should Arrays.asList(value) . Also commented code place inside the while loop.

The List is an interface, and ArrayList is an implementation. You don't need the instance type of the object to use the data. Just use an interface. Also you should know that codding to interfaces is a best practice in Java and one of the principles of OOP in general.

For the single line it require List<String> and to hold these lines you should use another list List<List<String>> . You can instantiate and add lists to it line-by-line.

In your code is a mistake: You are trying to split a String that is null . Move it into the while loop where line refers to an actual String object.

You want to split the string at "," , but I guess you also want to remove " if it's the first or last character.

This can be done using String.split(String regex) :

String[] split = x.split("(\",\"|\\A\"|\"\\z)");

This regular expression matches "," everywhere and " at the beginning/end of the string. The result array begins with an empty String if the input starts with a quote.

Note that it's much easier to output an array using

System.out.println( Arrays.toString(myArray) );

First define a list where you would want to store all the words:

List<String> list = new ArrayList<String>();

You don't need a for loop inside your while loop. Get rid of it and use the following method instead:

//Remove quotes. I think you wouldn't need them.
line = line.replace("\"", "");
//Now get the words which are separated by a comma
String[] words = line.split(",");
//Now add all the words to the main list
Collections.addAll(list, words);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM