Results 1 to 8 of 8
  1. #1
    scoobyrox is offline Member
    Join Date
    Aug 2010
    Posts
    13
    Rep Power
    0

    Default 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.

  2. #2
    siva_vs_siva is offline Member
    Join Date
    Aug 2010
    Posts
    31
    Rep Power
    0

    Default

    //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();
    }


    }

  3. #3
    scoobyrox is offline Member
    Join Date
    Aug 2010
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by siva_vs_siva View Post
    //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.

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Quote Originally Posted by scoobyrox View Post
    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:
    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; 09-03-2010 at 07:08 AM.

  5. #5
    scoobyrox is offline Member
    Join Date
    Aug 2010
    Posts
    13
    Rep Power
    0

    Default 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");

    }

    }

  6. #6
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    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);
    That should help you determine where your error is; this should also help.

  7. #7
    Ashish1 is offline Member
    Join Date
    Aug 2010
    Location
    kolkata
    Posts
    7
    Rep Power
    0

    Default

    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..

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Read the API doc for the Formatter class. It shows the valid conversion characters.
    For example: d is for integers.

Similar Threads

  1. 2 Dimensional Loop problem
    By scoobyrox in forum New To Java
    Replies: 5
    Last Post: 08-22-2010, 10:35 PM
  2. Replies: 3
    Last Post: 07-23-2009, 08:45 AM
  3. Replies: 1
    Last Post: 07-22-2009, 11:10 PM
  4. How to create this if many inputs?
    By sarahannel123 in forum New To Java
    Replies: 3
    Last Post: 05-18-2008, 05:22 PM
  5. Date Inputs
    By hiranya in forum AWT / Swing
    Replies: 3
    Last Post: 11-06-2007, 06:11 PM

Posting Permissions

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