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