简体   繁体   中英

Reading different variables from text file in Java

So I have text like this:

16783 BOB ARUM 30.5 10.00

A lot of these on different lines in a text file as a (long,string,string,double,double) I want to store these variables inside of an array and so far I have:

public class Main {

public static void main(String[] args){

    ArrayList<String> ArrEmployee = new ArrayList<String>(); // create array for employees

    try {
        Scanner txtIn = new Scanner(new File("payroll.txt"));
    } 
    catch (FileNotFoundException e) {

    }

}}

The problem I'm having though is I can't figure out a way to effectively store these values inside of my arrEmployee array in a way which I could use them later. I've figured out so far that creating a different class with a constructor could help, but I'm having a hard time understanding how to access objects in an array.

For example, if I only wanted the double at the end of the line, say it's an object stored inside an array now, how would I access that specific double?

Creating an Employee class would be helpful, so lets say you make an employee with several instance variables. If you store the last double as one of the instance variables in your for loop, you could then retrieve that value later by calling it from the object.

If you choose to use this way, you would have to change the ArrayList to hold Employees instead, and thus would have to change how you input from the file.

So if you have an Employee class with a constructor accepting (long,string,string,double,double) and the last double variable named 'D3', you could use this:

public class test {

public static void main(String[] args) {

    ArrayList<Employee> ArrEmployee = new ArrayList<Employee>(); // create array for employees

    try {
        Scanner txtIn = new Scanner(new File("payroll.txt"));
        while (txtIn.hasNext()) {
            Double D1 = txtIn.nextDouble();
            String S1 = txtIn.next();
            String S2 = txtIn.next();
            Double D2 = txtIn.nextDouble();
            Double D3 = txtIn.nextDouble();
            ArrEmployee.add(new Employee(D1,S1,S2,D2,D3));
        }
    } catch (FileNotFoundException e) {

    }
    System.out.println(ArrEmployee.get(0).getD2());//Note here how you can use method getD2() on the method get(0) of your ArrayList, if the list's type is Employee and you've implemented getD2() to return the last double

}

}

Writing your inputs as strings to an arraylist would probably look a lot like:

for (line in textFile) {
    ArrayList<String> arrList = new ArrayList();
    arrList.addAll(Array.toList(line.split(" ")));
}

Obviously then to make these strings useful you will need to convert them to their actual types, like:

double test = Double.parseDouble(arrList[3]);

This i how you could access the variables of an object stored in an array:

arrEmployee[2] = new Employee();//im leaving out the args, since i don't know what they would be

String name = arrEmployee[2].getName();//this is the name of the second employee in the array

This is just an examplecode, which implies quite a lot of the implementation, but i think it answers your question.

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