Hi everybody
I have wrriten a code to save data from a text file into any array list. I am using array list for the first time. I have used only array before. In the text file I have data that is seperated by comma , I need each element to be stored in a seperate array position that is .....
1,10,0,0,3,2
in the above line number one should be stored in array position 1,1......number 10 should be stored in array position 1,2...like a two dimensional array.....here is my code...
package heuristic; import java.io.*; import java.util.Scanner; import java.text.*; import java.util.ArrayList; /** * * @author bharathwaj */ public class greedyAlgorithm { /** * @param args the command line arguments */ public static void main(String[] args) throws IOException { // Declarations ArrayList<String> patientDetails = new ArrayList<String>(); int number; readPatientDetails(patientDetails,"patientdetails.txt"); printArray(patientDetails,9); } public static void readPatientDetails(ArrayList<String>patientDetails, String fileName)throws IOException { File inFile = new File(fileName); String line; if (!inFile.exists()) { System.out.println("Cannot find " + fileName); System.exit(0); } Scanner myFile = new Scanner(inFile); int count = 0; for(int i = 0; myFile.hasNext(); i = i+1) { { patientDetails.add(myFile.nextLine()); count ++; } } myFile.close(); } public static void printArray(ArrayList<String>patientDetails, int numValues) { for (int i = 0; i < numValues; i++) { System.out.println(patientDetails); } } }