简体   繁体   English

使用扫描仪将数据拆分并存储在阵列中

[英]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 约翰:史密斯:制造:6.75:120:444

Betty:White:Manager:1200.00:111 贝蒂:白:经理:1200.00:111

Stan:Slimy:Sales:10000.00:332 Stan:Slimy:Sales:10000.00:332

Betty:Boop:Design:12.50:50:244 贝蒂:大桶:设计: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. 如何使它扫描一行存储在数组中,然后对6或5个参数使用addEmployee方法,然后继续进行下一行。 The text sometimes has 5 stuff and because there is 5 the secondParam or rowData[4] should be 0. 文本有时有5个东西,并且由于有5个,secondParam或rowData [4]应该为0。

You set the 您设置

         rowData[4] = null;

and then after you parse it 然后在解析之后

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

youll get numberformatexception from that; 你会从中得到numberformatexception;

just set it to 0: 只需将其设置为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 您还检查了长度是否为5,但是添加了第五个索引(即数组中的第六个字符串)。这将导致空指针异常。

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);

            }

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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