reading into a 2-d?

I am trying to input a list of integers into a 2-d array. I have one prompt to input the numbers with spaces in between each, using a string. I was wondering how to read in the individual integers into a 2-d array into each element so that i can use it later to perform matrix arithmetic? wrapper class?

[310 byte] By [sessy72024a] at [2007-10-2 5:51:49]
# 1
How far have you got thus far? What specifically do you need?
abillconsla at 2007-7-16 2:01:26 > top of Java-index,Java Essentials,New To Java...
# 2

I am try to perform matrix arithmetic by using 2d arrays to store integers. i have the user input the # of row and col in main then input method is called. Its not putting the integers into the 2d array at all because i try to print out the 2d and it prints out 0's not the integers. Im not sure how to use one prompt to input all the integers at once but then read those individual integers into seperate element of the 2d array.

public static int[][] Input(int x, int y)

{

int[][] table = new int[x][y];

String array;

int index = 1;

Scanner scan = new Scanner (System.in);

//Enters whole array in

System.out.println("Enter one 2-D array at a time(with a space with no commas in between each number): ");

array = scan.next();

while (index < array.length())

{

if(index < array.length() && array.charAt(index) != ' ')//if not a space or length of array

{

for(int row = 0; row < table.length; row++)//enters number in array

for(int col = 0; col < table[row].length; col++)

table[row][col] = array.charAt(index);;

index++;

}

else

index++;//skips spaces

}

index++;

return table;

sessy72024a at 2007-7-16 2:01:26 > top of Java-index,Java Essentials,New To Java...
# 3

Factor out the functionalities; at this very moment you're facing three

distinct problems:

1) how to read a bunch of integers from an InputStream/Reader;

2) how to create a matrix (2D array) and fill its elements one by one;

3) how to implement matrix operations on that matrix from step 2).

Think about each step separately and solve those (sub)problems

one by one.

kind regards,

Jos

JosAHa at 2007-7-16 2:01:26 > top of Java-index,Java Essentials,New To Java...
# 4

How 'bout ...

int beg = 0, end = 0;

for (int row = 0; row < table.length; row++) {

for(int col = 0; col < table[row].length; col++) {

end = array.indexOf(" ",end);

System.out.println("end: "+end);

if (end != -1 ) {

table[row][col] = Integer.parseInt(array.substring(beg, end));

}

else {

end = array.length();

table[row][col] = Integer.parseInt(array.substring(beg, end));

row = table.length;

break;

}

beg = ++end;

}

}

... or something like that?

abillconsla at 2007-7-16 2:01:26 > top of Java-index,Java Essentials,New To Java...
# 5
im getting an syntax error saying that this line of code has incompatible types.lab3_12.java:67: incompatible typesfound: intrequired: java.lang.String table[row][col] = Integer.parseInt(array.substring(beg, end));Thank you
sessy72024a at 2007-7-16 2:01:26 > top of Java-index,Java Essentials,New To Java...
# 6
Stop banging code please and reread my reply (#3) again ...kind regards,Jos
JosAHa at 2007-7-16 2:01:26 > top of Java-index,Java Essentials,New To Java...
# 7

> im getting an syntax error saying that this line of

> code has incompatible types.

>

> lab3_12.java:67: incompatible types

> found: int

> required: java.lang.String

> table[row][col] =

> ow][col] = Integer.parseInt(array.substring(beg,

> end));

>

> Thank you

table[row][col] = Integer.parseInt(array.substring(beg, end));

^

Does it show an up arrow about here? If so, have you re-defined "table" as an array of Strings?

abillconsla at 2007-7-16 2:01:26 > top of Java-index,Java Essentials,New To Java...