Dealing with commas in a CSV file

后端 未结 27 2808
傲寒
傲寒 2020-11-21 06:53

I am looking for suggestions on how to handle a csv file that is being created, then uploaded by our customers, and that may have a comma in a value, like a company name.

27条回答
  •  终归单人心
    2020-11-21 07:32

    You can read the csv file like this.

    this makes use of splits and takes care of spaces.

    ArrayList List = new ArrayList();
    static ServerSocket Server;
    static Socket socket;
    static ArrayList list = new ArrayList();
    
    
    public static void ReadFromXcel() throws FileNotFoundException
    {   
        File f = new File("Book.csv");
        Scanner in = new Scanner(f);
        int count  =0;
        String[] date;
        String[] name;
        String[] Temp = new String[10];
        String[] Temp2 = new String[10];
        String[] numbers;
        ArrayList List = new ArrayList();
        HashMap m = new HashMap();
    
             in.nextLine();
             date = in.nextLine().split(",");
             name = in.nextLine().split(",");
             numbers = in.nextLine().split(",");
             while(in.hasNext())
             {
                 String[] one = in.nextLine().split(",");
                 List.add(one);
             }
             int xount = 0;
             //Making sure the lines don't start with a blank
             for(int y = 0; y<= date.length-1; y++)
             {
                 if(!date[y].equals(""))
                 {   
                     Temp[xount] = date[y];
                     Temp2[xount] = name[y];
                     xount++;
                 }
             }
    
             date = Temp;
             name =Temp2;
             int counter = 0;
             while(counter < List.size())
             {
                 String[] list = List.get(counter);
                 String sNo = list[0];
                 String Surname = list[1];
                 String Name = list[2];
                 for(int x = 3; x < list.length; x++)
                 {           
                     m.put(numbers[x], list[x]);
                 }
                Object newOne = new newOne(sNo, Name, Surname, m, false);
                 StudentList.add(s);
                 System.out.println(s.sNo);
                 counter++;
             }
    
        

    提交回复
    热议问题