Reading from files into two-dimensional arrays

Hi I'm pretty brand-spankin new when it comes to Java and the likes. I'm trying to read a file containing a square matrix of unknown size into a two-dimensional array, but I'm having troubles getting started.

Is it better to use StringTokenizer or String.split?

How about FileReader vs. FileInputStream?

And how the heck do you make a string into a double? I've tried valueOf and parseDouble, but apparently I'm not writing it properly.

Here's what I've done so far, obviously the line with the string tokenizer giving the matrix its next element is not compiling. Any help would be greatly appreciated...

(apsc256.txt)

3

12 6 -6

6 16 2

-6 2 16

import java.io.*;

import java.util.*;

import java.io.*;

import java.util.*;

public class matrixManipulations {

private static int n = 0;

public double [][] matrix = new double [n][n];

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.println("What is the name of your matrix file?");

String fileName = scan.next();

matrixManipulations m = new matrixManipulations();

m.readFile(fileName);

}

public void readFile(String fName){

String name = fName;

//String[] content = null;

BufferedReader bRead;

try{

bRead = new BufferedReader(new FileReader(name));

int Integer = new Integer(bRead.readLine());

n = Integer;

System.out.println("n = " + n);

double Double;

StringTokenizer stoke;

//String line;

//while (line != null){

for(int row = 0; row < n; row++){

for(int col = 0; col < n; col++){

stoke = new StringTokenizer(bRead.readLine(), " " , false);

System.out.println("Why");

Double = new Double(stoke.nextToken());

System.out.println("won't");

System.out.println(Double);

matrix[row][col]= Double;

System.out.println(matrix[row][col]);

System.out.println("you");

System.out.print(" " + matrix[row][col] + " ");

System.out.println("work?");

}

System.out.println("hi");

}

//}

bRead.close();

}

catch(IOException e){

System.out.println("This program can't read your file.");

e.printStackTrace();

}

[2348 byte] By [rayLO13a] at [2007-10-3 10:23:39]
# 1

String.split() uses regular expression and i think it's better and easier to use.

FileReader is very convenient for reading character files, and a FileInputStream would possibly be able to transform data along the way to provide additional functionality for your streams.

// String to Double

Try to use trim() method first before parseDouble.

Message was edited by:

Redxxiv

Redxxiva at 2007-7-15 5:45:25 > top of Java-index,Java Essentials,Java Programming...
# 2

1.) Use "code" tags when posting code

2.) Read http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html

3.) Don't call an int variable "Integer"; that's the name of a class in java.lang

4.) Same applies to "Double"

5.) Either use a primitve an Integer.parseInt() or Double.parseDouble() respectively, or use wrapper classes Integer or Double respectively. I know Java 5 has this nice little boxing-feature, but ...

quittea at 2007-7-15 5:45:25 > top of Java-index,Java Essentials,Java Programming...
# 3
Any other suggestions for string to double? I've tried everything I can think of , including trim(), and nothing works.
rayLO13a at 2007-7-15 5:45:25 > top of Java-index,Java Essentials,Java Programming...
# 4
yeah great advice, I definitely was mixed up about my wrapper classes. My compiler really doesn't like my string tokenizer though, and it also doesn't like the way I'm calling my readFile(). Any suggestions?
rayLO13a at 2007-7-15 5:45:25 > top of Java-index,Java Essentials,Java Programming...
# 5
> Any other suggestions for string to double? I've> tried everything I can think of , including trim(),> and nothing works.// Constructs a double objectjava.lang.Double double = new Double(StringRepresentationOfDoubleValue);
Redxxiva at 2007-7-15 5:45:25 > top of Java-index,Java Essentials,Java Programming...
# 6
What exactly do you want to do?
Redxxiva at 2007-7-15 5:45:25 > top of Java-index,Java Essentials,Java Programming...
# 7

> Any other suggestions for string to double? I've

> tried everything I can think of , including trim(),

> and nothing works.

There are at least two ways:

1. Primitive

double d = Double.parseDouble(yourString);

2. Wrapper class

Double d = new Double(yourString);

// d.doubleValue() delivers the primitive value

quittea at 2007-7-15 5:45:25 > top of Java-index,Java Essentials,Java Programming...