Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

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.","","O.D.","","","","","","","","","","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","","","","","","","","","","","","","","","","","","","","",""

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...