0

How's it going everyone. I am trying to read a file and store each String within that file into an element in the Array. The problem is, I cannot move to the next line of data. I am assuming that is when I would need to use an ArrayList, but I am having difficulty figuring out how to iterate through each text on each line and store that into an element then move on to the next line. I want to have each data stored in its own element. Also how would I call the data stored within that element? Here is my code:

Scanner readPayrollData = new Scanner(new FileReader ("employeeData.dat"));

String[] employee = new String[3];

ArrayList<String> data = new ArrayList<String>();

while(readPayrollData.hasNextLine())
{
    for(int i = 0; i < employee.length; i++)
    {   
        readPayrollData.hasNext();
        employee[i] = readPayrollData.next();       
    }

    readPayrollData.hasNextLine();          
    data.add(employee);
}

System.out.println(data);   
readPayrollData.close();
1

2 Answers 2

1
ArrayList<String> list = new ArrayList<String>();

Scanner inFile = null;
try {
inFile = new Scanner(new File("textfile.txt"));
} catch (FileNotFoundException e) {

e.printStackTrace();
}

while(inFile.hasNextLine()){
    list.add(inFile.nextLine());
}
Sign up to request clarification or add additional context in comments.

2 Comments

I'm sorry, but I am new to programming. Could you please explain how this code works?
An array is a list of data. The array's size is fixed; we must declare its size when we initialize it. The ArrayList is dynamic. We're allowed to add as many items as we want to an ArrayList. In the code above, I've declared an ArrayList of type String. I created a Scanner that took in a File with a given file name. The Scanner class has a method called .hasNextLine() that checks whether or not the current Scanner object still has input. The code inside the loop adds the next line of the Scanner to the ArrayList.
0
List<String> employee=new ArrayList<>();
Scanner fin=null;
try{
fin=new Scanner(new File("employeeData.dat"));
}catch(Exception e){
System.out.println("unable to open file");
e.printStackTrace();
}

while(fin.hasNextLine()){
employee.add(fin.nextLine());
}

fin.close();

4 Comments

I'm sorry, but I am new to programming. Could you please explain how this code works?
the array list will be used to store the string variables. i.e each line of the file.
the array list will be used to store the string variables. fin is going to read the file. im reading the file in a try and catch block because if an error occurs and the file cannot be read for some reason, then the program flow will tranfer to the catch blow and the "e.printStackTrace();" will tell you why the file was not read. the while loop is basically checking if any information is present on the next line of the file, this keeps happening untill the it becomes false and exits the loop. the add() method, adds the string to the array list
sorry, the while loop check if any information is present on the current line it is on, not on the next line

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.