Reading data into array from file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #16
    Originally posted by SkyGenius
    I can run the class. But when I use "space delimited format" for an input file, I could not write all read data to a file. The read data are displayed normally when I write it to the console.

    Code:
    for (int i = 0; i < inWL[0].size(); i++) {
    	System.out.println(inWL[0].get(i) + " " + inWL[1].get(i));
    	fout1.write(inWL[0].get(i) + " " + inWL[1].get(i) + "\n");
    }
    By the way, how could I modify my code to read a file with "tab delimited format"?

    Code:
    public static ArrayList[] readFile2(String fileName) {
    	 / * File Structure:
    		 * 	1.1 11.1
    		 * 	2.2 22.2
    		 * 3.3 33.3
    		 * 	... ... 
    		 */	
    			String line = " ";
    			ArrayList[] data = new ArrayList[2];
     
    			//for(int i = 0; i < 2; i++) {
    			data[0] = new ArrayList<String>();
    			data[1] = new ArrayList<Double>();
    			//}
     
    			try {
    				FileReader fr = new FileReader(fileName);
    				BufferedReader br = new BufferedReader(fr);
    				while((line = br.readLine()) != null) {
    					String[] theline = line.split(" ");
    					data[0].add(theline[0]);
    					data[1].add(Double.parseDouble(theline[1]));
     
    					//System.out.println(data[0] + " " + data[1]);
    				}
    			}
    			catch(FileNotFoundException fN) {
    				fN.printStackTrace();
    			}
     
    			catch(IOException e) {
    				System.out.println(e);
    			}
     
    			return data;
     
    		} //end readFile2 method
    The reading should be fine. It must be the line
    Code:
    String[] theline = line.split(" ");
    Which is spilitting on space. You can split on any character

    eg
    Code:
     String[] theLine = line.split("\t");
    to split on tab.

    You said you could not print all the data to the other file but could print all the data to the console? If so then post the code you used for printing to the file.

    Comment

    • SkyGenius
      New Member
      • Dec 2006
      • 13

      #17
      Code:
      FileWriter fout1 = new FileWriter("output1.txt");
      for (int i = 0; i < inWL[0].size(); i++) {
      	System.out.println(inWL[0].get(i) + " " + inWL[1].get(i));
      	fout1.write(inWL[0].get(i) + " " + inWL[1].get(i) + "\n");
      }

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #18
        Originally posted by SkyGenius
        Code:
        FileWriter fout1 = new FileWriter("output1.txt");
        for (int i = 0; i < inWL[0].size(); i++) {
        	System.out.println(inWL[0].get(i) + " " + inWL[1].get(i));
        	fout1.write(inWL[0].get(i) + " " + inWL[1].get(i) + "\n");
        }
        I'd advise you the BufferedWriter class to write to a file

        Code:
         
        BufferedWriter out = new BufferedWriter(new FileWriter(file));
        out.write(s);//writes the String s
        out.newLine();//goes to next line

        Comment

        • SkyGenius
          New Member
          • Dec 2006
          • 13

          #19
          Thanks so much for your advice.

          Comment

          • SkyGenius
            New Member
            • Dec 2006
            • 13

            #20
            How could I do the operation on the ArrayList? Could I transfer all the element values in ArrayList[1] to an array?
            The following is my problem. I could not do the calculation in the main method.
            Code:
            //This in main method
            
            ArrayList[] inHAVCurve = new ArrayList[3];
            String fin2 = "inHAVCurve.txt";
            inHAVCurve = readFile3(fin2);
            
            double[] a;
            			
            for (int i=0; i<inWL[0].size(); i++){
            	a[i] = inHAVCurve[1].get(i) + inHAVCurve[2].get(i);
            }
            
            public static ArrayList[] readFile3(String fileName) {
            	/* This method read file and store data in three ArrayLists[].   
            	 * File Structure:
            	 * 	1.1 11.1 111.1
            	 * 	2.2 22.2 222.2
            	 * 	3.3 33.3 333.3
            	 * 	... ...  ...
            	 */	
            		String line = "\t";
            		ArrayList[] data = new ArrayList[3];
            		
            		for(int i = 0; i < 3; i++) {
            		data[i] = new ArrayList<Double>();
            		}
            		
            		try {
            			FileReader fr = new FileReader(fileName);
            			BufferedReader br = new BufferedReader(fr);
            			while((line = br.readLine()) != null) {
            				String[] theline = line.split("\t");
            				data[0].add(Double.parseDouble(theline[0]));
            				data[1].add(Double.parseDouble(theline[1]));
            				data[2].add(Double.parseDouble(theline[2]));
            			}
            		}
            		catch(FileNotFoundException fN) {
            			fN.printStackTrace();
            		}
            		
            		catch(IOException e) {
            			System.out.println(e);
            		}
            		
            		return data;
            
            	} //end readFile3 method

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #21
              Originally posted by SkyGenius
              How could I do the operation on the ArrayList? Could I transfer all the element values in ArrayList[1] to an array?
              The following is my problem. I could not do the calculation in the main method.
              Code:
               
              //This in main method
               ArrayList[] inHAVCurve = new ArrayList[3];
              String fin2 = "inHAVCurve.txt";
              inHAVCurve = readFile3(fin2);
               
              double[] a;
               
              for (int i=0; i<inWL[0].size(); i++){
              	a[i] = inHAVCurve[1].get(i) + inHAVCurve[2].get(i);
              }
               
              public static ArrayList[] readFile3(String fileName) {
              	/* This method read file and store data in three ArrayLists[]. 
              	 * File Structure:
              	 * 	1.1 11.1 111.1
              	 * 	2.2 22.2 222.2
              	 * 	3.3 33.3 333.3
              	 * 	... ... ...
              	 */	
              		String line = "\t";
              		ArrayList[] data = new ArrayList[3];
               
              		for(int i = 0; i < 3; i++) {
              		data[i] = new ArrayList<Double>();
              		}
               
              		try {
              			FileReader fr = new FileReader(fileName);
              			BufferedReader br = new BufferedReader(fr);
              			while((line = br.readLine()) != null) {
              				String[] theline = line.split("\t");
              				data[0].add(Double.parseDouble(theline[0]));
              				data[1].add(Double.parseDouble(theline[1]));
              				data[2].add(Double.parseDouble(theline[2]));
              			}
              		}
              		catch(FileNotFoundException fN) {
              			fN.printStackTrace();
              		}
               
              		catch(IOException e) {
              			System.out.println(e);
              		}
               
              		return data;
               
              	} //end readFile3 method
              Code:
               ArrayList[] inHAVCurve  = readFile3(fin2); 
               
              double[] a;
               
                for(int j = 0; j<inHAVCurve[1].size();j++) {
              	a[i] = inHAVCurve[1].get(j) + inHAVCurve[2].get(j);
               }
              For this to work, inHAVCurve[1] and inHAVCurve[2] must have the same number of elements.

              Comment

              • SkyGenius
                New Member
                • Dec 2006
                • 13

                #22
                Yes, they have same number of elements. I tried it, but it still does not work. I got this in the console:

                Exception in thread "main" java.lang.Error : Unresolved compilation problem:
                The operator + is undefined for the argument type(s) java.lang.Objec t, java.lang.Objec t

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #23
                  Originally posted by SkyGenius
                  Yes, they have same number of elements. I tried it, but it still does not work. I got this in the console:

                  Exception in thread "main" java.lang.Error : Unresolved compilation problem:
                  The operator + is undefined for the argument type(s) java.lang.Objec t, java.lang.Objec t
                  You have to type cast the elements from the ArrayList to doubles like
                  Code:
                   
                  a[i] = (Double)inHAVCurve[1].get(j) + (Double)inHAVCurve[2].get(j);

                  Comment

                  • SkyGenius
                    New Member
                    • Dec 2006
                    • 13

                    #24
                    It still cannot run. Index of a[j] is j, not i.

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #25
                      Originally posted by SkyGenius
                      It still cannot run. Index of a[j] is j, not i.

                      Code:
                       
                      ArrayList[] inHAVCurve  = readFile3(fin2); 
                      double[] a = new double[inHAVCurve[0].size()];
                      for(int j = 0; j<inHAVCurve[1].size();j++) {
                       a[j] = (Double)inHAVCurve[1].get(j) + (Double)inHAVCurve[2].get(j);
                      }
                      You should also put an effort of trying to correct some of the errors yourself.

                      Comment

                      Working...