Read file and change the values


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Read file and change the values

  1. #1
    Join Date
    Dec 2005
    Posts
    4

    Read file and change the values

    Hi,

    I'm really stuggling with a program i'm trying to create
    I have to normalise some data so the the values are in the range [0,1]. I want to do this by taking the greates value in each column and dividing every other value by this.
    Example: i have a txt file -
    6, 148, 72, 35, 0, 336, 0627, 50, 2
    1, 85, 66, 29, 0, 266, 0351, 31, 1
    8, 183, 64, 0, 0, 233, 0672, 32, 2
    1, 89, 66, 23, 94, 281, 0167, 21, 1
    0, 137, 40, 35, 168, 431, 2288, 33, 2
    5, 116, 74, 0, 0, 256, 0201, 30, 1

    if i take the first column, i would want to divide each value by 8.
    I've wrote a program that reads the file and puts each line into an array, but want i want is each column in an array/vector(whichever is best) and i really don't know how to do this.

    My code so far:

    import java.io.*;
    import java.util.*;

    public class FileIO
    {
    public static void main(String[] args)
    {
    ArrayList bob = loadFile("testfile.txt");
    for (int i=0; i<bob.size(); i++)
    System.out.println(i+1 + ":\t" + bob.get(i));
    }
    public static ArrayList loadFile(String fileName)
    {
    ArrayList file = new ArrayList();
    try
    {
    BufferedReader in = new BufferedReader(new FileReader(fileName));
    if (!in.ready()) throw new IOException();
    while ((line = in.readLine()) != null)
    file.add(line);
    in.close();
    }
    catch (IOException e)
    {
    System.out.println(e);
    return null;
    }
    return file;
    }
    }

    i'd want to put each column in an array then perform the calculations on each column.
    Can anyone please help me?

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Check my comments

    I have hacked up two solutions here, one with ArrayList and one with a
    matrix, but... I haven't tested it
    Code:
    import java.io.*;
    import java.util.*;
    import java.text.*;
    
    public class FileIO {
    
      public static DecimalFormat dF=new DecimalFormat("0.00");
    
      public FileIO() {}
    
      public void doArrayListSolution ()  throws Exception {
        ArrayList bob = loadFileAL("testfile.txt");
        for (int i = 0; i < bob.size(); i++) {
          System.out.println(i + 1 + ":\t" + bob.get(i));
        }
      }
      public void doMatrixSolution () throws Exception {
        double [][] fileMX = loadFileMX("testfile.txt");
        for (int i = 0; i < fileMX.length; i++) {
          for (int j=0; j<fileMX[i].length; j++) {
            System.out.print(dF.format(fileMX[i][j]+"\t"));
          }
          System.out.println();
        }
      }
      private int countRecords(String fileName)throws IOException {
        String line=null;
        int count=0;
        BufferedReader in = new BufferedReader(new FileReader(fileName));
        while ( (line = in.readLine()) != null) count++;
        in.close();
        return count;
      }
      /**
      * MAtrix
      * @param fileName
      * @return double matrix
      */
     public double [][] loadFileMX(String fileName) throws Exception {
       String line = null;
       double [][] data=new double[countRecords(fileName)][];
       BufferedReader in = new BufferedReader(new FileReader(fileName));
       int lineNo=0;
       while ( (line = in.readLine()) != null) {
         /**
          * Split line into array of strings, create double array,
          * Store row of parsed doubles in the matrix.
          */
         String[] ss = line.split(",");
         double[] dd = new double[ss.length];
         for (int i = 0; i < ss.length; i++) {
           dd[i] = Integer.parseInt(ss[i]);
         }
         data[lineNo++]=dd;
       }
       in.close();
       return data;
     }
    
      /**
       * Arraylist
       * @param fileName
       * @return arrayList of double arrays
       */
      public ArrayList loadFileAL(String fileName) throws Exception {
        String line = null;
        ArrayList file = new ArrayList();
    
        BufferedReader in = new BufferedReader(new FileReader(fileName));
        while ( (line = in.readLine()) != null) {
          /**
           * Split line into array of strings, create double array,
           * Store parsed doubles in the array.
           */
          String[] ss = line.split(",");
          double[] dd = new double[ss.length];
          for (int i = 0; i < ss.length; i++) {
            dd[i] = Integer.parseInt(ss[i]);
          }
          /**
           * stuff the double array into the arrayList.
           * Rerieve like: double [] lineVals=(double[])list.get(i);
           */
          file.add(dd);
        }
    
        in.close();
        return file;
      }
    
      public static void main(String[] args) {
        FileIO fio = new FileIO();
        try {
          fio.doArrayListSolution();
          fio.doMatrixSolution();
        }
        catch (Exception ex) {
          System.err.println("This didn't go so well...");
          ex.printStackTrace();
        }
      }
    
    }
    eschew obfuscation

Similar Threads

  1. Replies: 1
    Last Post: 02-26-2005, 10:50 AM
  2. How long before the next version??
    By _CAG in forum .NET
    Replies: 146
    Last Post: 08-12-2002, 11:40 PM
  3. Replies: 12
    Last Post: 05-18-2001, 05:05 AM
  4. NullPointerException when reading text file
    By Andrew McLellan in forum Java
    Replies: 3
    Last Post: 05-09-2001, 06:34 PM
  5. Replies: 2
    Last Post: 03-07-2001, 09:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center