Pregunta

Basically I have to classes interacting with one another in this situation one company and one the driver, this code is written in the driver. So I am using a file reader to scan a text file that looks like this with a space between each line.

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

And the code is as follows. The addEmployee method of the company class has a (string, string, string, double, int, int) parameter. The text file it reads in has a colons inbetween each part, so howe can I add it to the arraylist of objects. And keep going until all of them are read. Sorry if my questions difficult to understand, if you want me to elaborate let me know in the comments. I just didn't want to make the question too long.

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()){
                scan.next().split(":");
                if (position.equals("Manager")){
                    c.addEmployee(fName, lName, position2, Double.parseDouble(firstParam2), 0, Integer.parseInt(empNum2));
                }
                else if(position.equals("Sales")){
                    c.addEmployee(fName, lName, position2, Double.parseDouble(firstParam2), 0, Integer.parseInt(empNum2));
                }
                else{
                    c.addEmployee(fName, lName, position2, Double.parseDouble(firstParam2), Integer.parseInt(secondParam2), Integer.parseInt(empNum2));
                }
            }
¿Fue útil?

Solución

This line:

scan.next().split(":");

Will return an array of Strings that you're not storing anywhere. Turn it into:

String[] rowData = scan.next().split(":");

And use every item in the array as you wish, for example to fill your variables or directly as arguments for your class constructor. Providing a sample of the former:

fName = rowData[0];
lName = rowData[1];
position = rowData[2];
firstParam2 = rowData[3];
secondParam2 = rowData[4];
empNum2 = rowData[5];
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top