# Thread: 2 dimensional loop with 2 different inputs

1. Member Join Date
Aug 2010
Posts
13
Rep Power
0

## 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] = tutoredTime;
// matrix[i] = 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.  Reply With Quote

2. Member Join Date
Aug 2010
Posts
31
Rep Power
0

## //This way

public static void main(String arg[])
{
int a[][]=new int;
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();
}

}  Reply With Quote

3. Member Join Date
Aug 2010
Posts
13
Rep Power
0

##  Originally Posted by siva_vs_siva //This way

public static void main(String arg[])
{
int a[][]=new int;
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.  Reply With Quote

4. ##  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];.

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:
Java Code:
```declare ROWS as int,3
declare matrix as double[][],new double[ROWS]
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)```
Last edited by Zack; 09-03-2010 at 06:08 AM.  Reply With Quote

5. Member Join Date
Aug 2010
Posts
13
Rep Power
0

## 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] = input.nextDouble();

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

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

}

}  Reply With Quote

6. ## Try executing these two codes:
Java Code:
`System.out.printf("%d %d",3.0,3);`
Java Code:
`System.out.printf("%f %d",3.0,3);`  Reply With Quote

7. Member Join Date
Aug 2010
Location
kolkata
Posts
7
Rep Power
0

## 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] + " " + matrix[j]);
}

}
}
}
}

I'll try to solve ur exact problem...as directed..but this can also be helpful..  Reply With Quote

8. ## Read the API doc for the Formatter class. It shows the valid conversion characters.
For example: d is for integers.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•