Results 1 to 8 of 8
 09022010, 07:09 AM #1Member
 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 2dimensional 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 2dimensional 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.
 09022010, 07:39 AM #2Member
 Join Date
 Aug 2010
 Posts
 31
 Rep Power
 0
//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();
}
}
 09032010, 05:54 AM #3Member
 Join Date
 Aug 2010
 Posts
 13
 Rep Power
 0
 09032010, 06:05 AM #4
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 ROWS1 (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][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)
Last edited by Zack; 09032010 at 06:08 AM.
 09032010, 06:35 AM #5Member
 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][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");
}
}
 09032010, 06:05 PM #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);
 09032010, 09:21 PM #7Member
 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][0] + " " + matrix[j][1]);
}
}
}
}
}
I'll try to solve ur exact problem...as directed..but this can also be helpful..
 09032010, 10:02 PM #8
Similar Threads

2 Dimensional Loop problem
By scoobyrox in forum New To JavaReplies: 5Last Post: 08222010, 09:35 PM 
Creating Checkerboard from a 2 dimensional 'for' loop.
By New2Java in forum New To JavaReplies: 3Last Post: 07232009, 07:45 AM 
Creating Checkerboard from a 2 dimensional 'for' loop
By New2Java in forum New To JavaReplies: 1Last Post: 07222009, 10:10 PM 
How to create this if many inputs?
By sarahannel123 in forum New To JavaReplies: 3Last Post: 05182008, 04:22 PM 
Date Inputs
By hiranya in forum AWT / SwingReplies: 3Last Post: 11062007, 05:11 PM
Bookmarks