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?
How far have you got thus far? What specifically do you need?
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;
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
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?
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
Stop banging code please and reread my reply (#3) again ...kind regards,Jos
> 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?