Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: 2 dimensional array coding

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 2 dimensional array coding

    I have been trying to learn java coding from a book and have run into a coding question that I'm confused about. Can someone help?
    I have a one dimensional array of positive integers. I'm trying to create another array with the same length and scan the original array for finding the maximum integer. I am then needing to copy that maximum integer in first position of the created array and set the maximum integer in the original array to zero. For second round, I need to write code to find the maximum integer in the original array and copy that in second position of created array and set the maximum integer in the original array to zero. I need to repeat this until all numbers in original array is zero. My created array should be sorted in positive integer numbers. I need to write this sort method that accepts a one dimensional array of integers that return the sorted array testing this method using the main method.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,340
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: 2 dimensional array coding

    Can you post your code with questions about the problems you are having?

    Where is the 2 dimensional array in all this?

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: 2 dimensional array coding

    The book that I'm going through provides questions and for the user to write the code. Part of my problem is that I don't know where to start, confused as to what it is really asking for. Just trying to learn java on the side so I have no really knowledge or training.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,340
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: 2 dimensional array coding

    Just trying to learn java
    That's what books are for. Read about a technique and then key the examples and change them this way and that way to see what happens.
    Does the book provide any examples you can type in?

  5. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: 2 dimensional array coding

    Yes, I went through those however, I'm going through the examples at the back and the book doesn't give these solutions. I wanted to see how the coding for this was to see if it made sense.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,340
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: 2 dimensional array coding

    As I suggested before, type in some examples from the book and make changes to them and see what happens.

  7. #7
    Junior Member
    Join Date
    Jul 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: 2 dimensional array coding

    Ok, could you help with this one? I want to Write a program that asks user for the two dimensions of a two dimensional integer matrix and then the program keep asking all elements of the matrix by row and column from user and then printing the content of the matrix at the end.
    This is what I have so far. The print coding is in correct, not sure how to print what the person enters.

    Scanner input = new Scanner(System.in);

    int[][] m = new int[3][4];
    System.out.println("Enter 2 dimensions" + m.length);

    for (int i = 0; i < m.length; i++);
    int i = 0;
    for (int j = 0; j < m[i].length; j++)m [i][j] = input.nextInt();

    System.out.print([i][j]);

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,340
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: 2 dimensional array coding

    how to print what the person enters.
    First you need to read what the user enters.
    Read the API doc for the Scanner class. It has methods to read input from the user.
    Also search here on the forum for Scanner examples.
    Once you get the what was entered into a variable, you'd use the println() method call to print it.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,340
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: 2 dimensional array coding

    Please put your code in code tags like this:
    Scanner input = new Scanner(System.in);
     
    int[][] m = new int[3][4];
    System.out.println("Enter 2 dimensions" + m.length);
     
    for (int i = 0; i < m.length; i++)  {  // removed ; here and added {
      // remove this line, i is defined above     int i = 0;
      for (int j = 0; j < m[i].length; j++) {
         m [i][j] = input.nextInt();  // get next input from user
         System.out.print(m[i][j]);  // show what the user entered
      } // end for(j)
    } // end for(i)

  10. The Following User Says Thank You to Norm For This Useful Post:

    Bighairjersey (July 23rd, 2011)

Similar Threads

  1. Implementing a two-dimensional boolean array
    By eNxy in forum What's Wrong With My Code?
    Replies: 40
    Last Post: July 16th, 2011, 07:30 AM
  2. Two-dimensional boolean array?
    By tcmei in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 17th, 2011, 09:32 PM
  3. Single Dimensional Array Help!
    By Allicat in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 15th, 2011, 01:01 PM
  4. Two-Dimensional Array and Loops
    By astrojunk in forum Java Theory & Questions
    Replies: 2
    Last Post: February 11th, 2011, 07:18 AM
  5. 2 dimensional array alternative ???
    By zeeshanmirza in forum Java SE APIs
    Replies: 1
    Last Post: February 23rd, 2010, 06:18 PM