# 2 dimensional loop with 2 different inputs

• 09-02-2010, 07:09 AM
scoobyrox
2 dimensional loop with 2 different inputs
input will be:

1) tutoring time in minutes

2) earning for the tutoring time in dollars and cents

must use 2-dimensional array to store each set of tutoring time and earning

output will look like:

Minutes Earnings
60.0 10.00
120.0 40.00
90.0 15.0

so there will be 3 sets of numbers to be stored into the array.

My problem is that I do not know how to assign TWO DIFFERENT VARIABLES to the correct array cel, in a single 2-dimensional array

int ROWS = 3;
int COLUMNS = 2;

double[][] matrix = new double[ROWS][COLUMNS]

Scanner input = new Scanner(System.in);

for (int i = 0; i <ROWS; i++)
for (int j=0; j <COLUMNS; j++) {
System.out.print("Enter time tutored in minutes:");
double tutoredTime = input.nextDouble();

System.out.print("Enter payment:");
double tutorPayment = input.nextDouble();

matrix[i][j] = tutoredTime;
matrix[i][j] = tutorPayment; //Doing this line will replace tutoredTime's value in the previous line of coding. So I know this is not correct.

//I tried matrix[i][0] = tutoredTime;
// matrix[i][1] = tutorPayment;

//Not sure if above is correct either. It seems weird that var j was not utilized.

Obviously this is not the complete program but I only really need to know how to correctly store 2 different variables to the correct place in the 2 dimensional array. The tutoredTime needs to go into (0,0), (1,0), (2,0).
tutorPayment needs to go into (0,1), (1,1), (2,1).

This is my first java class. So this problem may seem very elementary to some but I sure can use some help. Thanks.
• 09-02-2010, 07:39 AM
siva_vs_siva
//This way

public static void main(String arg[])
{
int a[][]=new int[5][5];
int k=0;
Scanner scan=new Scanner(System.in);
System.out.println("Enter the time&payment..");
for(int i=0;i<5;i++)
{
a[i][k]=scan.nextInt();
a[k][i]=scan.nextInt();
}

}
• 09-03-2010, 05:54 AM
scoobyrox
Quote:

Originally Posted by siva_vs_siva
//This way

public static void main(String arg[])
{
int a[][]=new int[5][5];
int k=0;
Scanner scan=new Scanner(System.in);
System.out.println("Enter the time&payment..");
for(int i=0;i<5;i++)
{
a[i][k]=scan.nextInt();
a[k][i]=scan.nextInt();
}

}

Tried it and it didn't work. Got a exception out of bound error. Also it looks like the k value will place the value on the wrong spot. Also I need question about time and payment asked separately and assigned to separate variable.
Thanks for trying to help me.
• 09-03-2010, 06:05 AM
Zack
Quote:

Originally Posted by scoobyrox
Obviously this is not the complete program but I only really need to know how to correctly store 2 different variables to the correct place in the 2 dimensional array. The tutoredTime needs to go into (0,0), (1,0), (2,0).
tutorPayment needs to go into (0,1), (1,1), (2,1).

Believe it or not, the last two sentences of the above quote answer your question.

You know that the number of columns is always 2, so that can be hardcoded (or not, I suppose, but in this case it should be). So your declaration will be something like double[][] matrix = new double[ROWS][2];.

Now, you need a single loop to loop from 0 to ROWS-1 (that is, i=0;i<ROWS;i++), and each time you want to accept two doubles using the input.nextDouble(); method. One, the tutoredTime, will go into the format (i,0), and the other will go into (i,1), just as you said in the quote I posted above. Obviously this is just a concept but with what you have so far I think you can put this into code yourself.

Post back if you have any problems with this! ;)

PS: Here's a bit of informal pseudocode for you, to get you started:
Code:

```declare ROWS as int,3 declare matrix as double[][],new double[ROWS][2] declare i as int,0 for i=0,i<ROWS,i++     prompt for time     store time in matrix(i,0)     prompt for earnings     store earnings in matrix(i,1)```
• 09-03-2010, 06:35 AM
scoobyrox
this is what I did but..
With this new code, after entering initial set of values, it gave me format conversion error.

Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.Double

Not sure what I did wrong. I assume it is about "double" but I am not seeing the error.

I will need to display the values of the 2 dimensional array. I think my current println coding at the end should print all the values I hope.

public class Calculator {

public static void main(String[] args) {

int ROWS = 3, COLUMNS = 2;

Scanner input = new Scanner(System.in);

double[][] matrix = new double[ROWS][COLUMNS];

for (int i = 0; i < ROWS; i++) {
System.out.print("Enter tutored time in minutes: ");
matrix[i][0] = input.nextDouble();

System.out.print("Enter payment amount in dollars and cents: ");
matrix[i][1] = input.nextDouble();

System.out.printf("%d\t%d", matrix[i][0], matrix[i][1]);
System.out.printf("\r");

}

}
• 09-03-2010, 06:05 PM
Zack
Try executing these two codes:
Code:

`System.out.printf("%d %d",3.0,3);`
Code:

`System.out.printf("%f %d",3.0,3);`
• 09-03-2010, 09:21 PM
Ashish1
Try this...Only draw back of this program is, its input technique...using command line..
thus u're not able to prompt input differently...

public class Calculator
{
public static void main(String[] args)
{
int ROWS = 3, COLUMNS = 2;

double [][] matrix = new double[ROWS][COLUMNS];

for (int i = 0; i < COLUMNS; i++)
{
for(int j=0;j<ROWS;j++)
{

matrix[j][i] =Double.parseDouble(args[i+2*j]);

if(i==1)
{
System.out.println(matrix[j][0] + " " + matrix[j][1]);
}

}
}
}
}

I'll try to solve ur exact problem...as directed..but this can also be helpful..
• 09-03-2010, 10:02 PM
Norm
Read the API doc for the Formatter class. It shows the valid conversion characters.
For example: d is for integers.