简体   繁体   中英

Using a scanner and split and storing the data in an array

I have a segment of code that has to scan text from a file and store it in an array of Strings. My code looks like this

 else if (e.getSource()==readButton){
            JFileChooser fileChooser = new JFileChooser("src");
        if  (fileChooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION)
        {
            empFile=fileChooser.getSelectedFile();
        }
            Scanner scan = new Scanner("empFile");
            while(scan.hasNext()){
                String[] rowData = scan.nextLine().split(":");
                if(rowData.length == 5){
                    rowData[4] = null;
                    fName = rowData[0];
                    lName = rowData[1];
                    position2 = rowData[2];
                    firstParam = Double.parseDouble(rowData[3]);
                    secondParam = Integer.parseInt(rowData[4]);
                    empNum = Integer.parseInt(rowData[5]);
                }
                else{
                fName = rowData[0];
                lName = rowData[1];
                position2 = rowData[2];
                firstParam = Double.parseDouble(rowData[3]);
                secondParam = Integer.parseInt(rowData[4]);
                empNum = Integer.parseInt(rowData[5]);
                }
                if (position2.equals("Manager")){
                    c.addEmployee(fName, lName, position2, firstParam, 0, empNum);
                }
                else if(position2.equals("Sales")){
                    c.addEmployee(fName, lName, position2, firstParam, 0, empNum);
                }
                else{
                    c.addEmployee(fName, lName, position2, firstParam, secondParam, empNum);
                }
            }

        }

And the text that is being scanned looks like this

John:Smith:Manufacturing:6.75:120:444

Betty:White:Manager:1200.00:111

Stan:Slimy:Sales:10000.00:332

Betty:Boop:Design:12.50:50:244

How can I make it scan one line store it in an array then use the addEmployee method for 6 or 5 parameters then move on to the next line. The text sometimes has 5 stuff and because there is 5 the secondParam or rowData[4] should be 0.

You set the

         rowData[4] = null;

and then after you parse it

 secondParam = Integer.parseInt(rowData[4]);

youll get numberformatexception from that;

just set it to 0:

rowData[4] = "0";

Also you checked the lenght if it is 5 but you add the 5th index which is the 6th string in the array.. it will cause null pointer exception.. just take it out

if(rowData.length == 5){
                rowData[4] = "0";
                fName = rowData[0];
                lName = rowData[1];
                position2 = rowData[2];
                firstParam = Double.parseDouble(rowData[3]);
                secondParam = Integer.parseInt(rowData[4]);
            }

UPDATED:

 Scanner scan = new Scanner("empFile");
        while(scan.hasNext()){
            String[] rowData = scan.nextLine().split(":");
            if(rowData.length == 5){
                rowData[4] = "0";
                fName = rowData[0];
                lName = rowData[1];
                position2 = rowData[2];
                firstParam = Double.parseDouble(rowData[3]);
                empNum = Integer.parseInt(rowData[4]);

                c.addEmployee(fName, lName, position2, firstParam, 0, empNum);

            }
            else{
            fName = rowData[0];
            lName = rowData[1];
            position2 = rowData[2];
            firstParam = Double.parseDouble(rowData[3]);
            secondParam = Integer.parseInt(rowData[4]);
            empNum = Integer.parseInt(rowData[5]);

             c.addEmployee(fName, lName, position2, firstParam, secondParam, empNum);

            }

        }

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