0

I'm fairly new to programming and I recently wrote something to utilize a scanner class to fill an object array from a text file. Essentially, I can re-write this text file or add new info and won't have to change the code. I suppose my question is this: is there an easier/more preferred method to doing this? I'm trying to learn the coding nuances.

import java.io.*; import java.util.*; public class ImportTest { public static void main(String[] args) throws IOException { Scanner s = null; Scanner k = null; ArrayList myList = new ArrayList<String>(); ArrayList myList2 = new ArrayList<String>(); ArrayList myList3 = new ArrayList<Student>(); try { s = new Scanner(new BufferedReader(new FileReader("testMe.txt"))); while (s.hasNext()) { myList.add(s.nextLine()); } } finally { if (s != null) { s.close(); } } System.out.println("My List 1:"); for(int i=0; i<myList.size(); i++) { System.out.println(i+". "+myList.get(i)); } for(int x=0; x<myList.size(); x++) { try { k = new Scanner(myList.get(x).toString()); while (k.hasNext()) { myList2.add(k.next()); } } finally { if (k != null) { k.close(); } } String name; int age; double money; name=myList2.get(0).toString(); age=Integer.parseInt(myList2.get(1).toString()); money=Double.parseDouble(myList2.get(2).toString()); Student myStudent=new Student(name, age, money); myList3.add(myStudent); myList2.clear(); } System.out.println("Test of list object: "); for(int i=0; i<myList3.size(); i++) { System.out.println(i+". "+myList3.get(i).toString()); } } } 

2 Answers 2

1

I would read the file line by line and parse every line directly. This way you do not need 3 lists, 2 scanners and multiple iterations:

String line = ""; BufferedReader br = new BufferedReader(new FileReader("test.txt")); ArrayList<Student> students = new ArrayList<Student>(); while( (line = br.readLine()) != null) { String[] tmp = line.split("\\s+"); //split line by spaces //this needs bounds & error checking etc. students.add(new Student(tmp[0], Integer.parseInt(tmp[1]), Double.parseDouble(tmp[2]))); } 

In Java 7 you can use the new file functions to read all lines at once:

List<String> allLines = Files.readAllLines("test.txt", Charset.defaultCharset()); 

Do not forget to close the reader or use try-with-resources (since java 1.7)

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, working inside the scanner while-loop simplifies your code, removing the need for ArrayList. I must caution that any heavy duty work done inside the loop can create I/O performance bottleneck in a large scale application.
0

Correct me if I am wrong, testMe.txt file contains Student information which are name, age, money, and you want read those values.

Best way to do it is you should serialize your Student objects into the the testMe.txt with the help of ObjectOutputStream. As well you can read those value using ObjectInputStream, so in this way you can able to get Student objects itself(no need to hnadle String).

In case you do want to serialize the data into file, you should store the data in some predefined format like as comma(,) or semi-colon(;) seperated.

For Example -

emp1, 24, 20000 emp emp2, 25, 24000 emp3, 26, 26000 

In this case while reading the string you can split it with seperation character and get the actual information.

Code snippet:

List<Student> students = new ArrayList<Student>(); ... try(scanner = new Scanner(new BufferedReader(new FileReader("testMe.txt")))){ while (scanner.hasNext()){ String data[] = scanner.nextLine().split(","); Student student = new Student(data[0],data[1],data[2]); students.add(student); } } 

Try-with-resource will automatically handle the resouce, you dont need to explicitly close it. This features available in java since 1.7.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.