-
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?
-
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
-
Replies: 1
Last Post: 02-26-2005, 09:50 AM
-
Replies: 146
Last Post: 08-12-2002, 10:40 PM
-
By Yuri in forum VB Classic
Replies: 12
Last Post: 05-18-2001, 04:05 AM
-
By Andrew McLellan in forum Java
Replies: 3
Last Post: 05-09-2001, 05:34 PM
-
By newToJava in forum Java
Replies: 2
Last Post: 03-07-2001, 08: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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks