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 12 of 12

Thread: Help with an array please!

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with an array please!

    Hello everyone,
    I am brand new to the forum as well as brand new to Java. I am taking a class and so far have been able to figure everything out until now. Basically, I need to write a program that accepts a n x n matrix that will total and display the sum of each row and column. First, the user inputs the matrix size (row and columns are the same so only 1 input is needed at this point). We need nested loops in order to populate the matrix, a method that displays the matrix as well as a method that displays the sums of the rows and columns. The program should run in a loop so that the user can do it again and exception handling is required. I won't have an issue with the loop or the exception handling as so far we have done this before and I have a basic understanding of how to do this. My issue is simply the array. Here is what the output should look like:

    Enter number of rows & columns (they will be the same): 3

    Row 1, Column 1: 1
    Row 1, Column 2: 2
    Row 1, Column 3: 9
    Row 2, Column 1: 0
    Row 2, Column 2: 1
    Row 2, Column 3: 2
    Row 3, Column 1: 0
    Row 3, Column 2: 7
    Row 3, Column 3: 1

    You Entered the Following Matrix:

    1 2 9
    0 1 2
    0 7 1

    The sum of row 1 is 12
    The sum of row 2 is 3
    The sum of row 3 is 8
    The sum of column 1 is 1
    The sum of column 2 is 10
    The sum of column 3 is 12

    I am really lost here and I would appreciate some help if anybody is able to figure this out. Thanks a bunch!
    And this is what I have so far (albeit very little):
    import java.util.Scanner;
     
    //Begin Class Main
    public class Main {
     
        //Begin Main Method
        public static void main(String[] args) {
     
     
     
     
            Scanner input = new Scanner (System.in);
     
            System.out.print("Enter the number of rows & columns (they will be"
                    + "the same): ");
            int arraySize = input.nextInt();
     
     
     
     
        } //End Main Method
     
    } //End Class Main
    Last edited by copeg; March 30th, 2011 at 02:33 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Help with an array please!

    Code tags == readable code (see my signature for details).

    What exactly is the problem? Have you read the following for how to define and use arrays ( Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics) )?

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with an array please!

    Thanks for the link and the reply Copeg. I have an idea of how to define arrays, but my problem is making them so that the user can input the amounts of rows/columns (they will always be the same). I am in my last semester of school (digital forensics) and this is the only thing that I have taken that I just can't get. I am completely lost on pretty much the whole thing.

  4. #4
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: Help with an array please!

    I got a Ton of help on array lists before from some extremely generous and patient people. Maybe you can use that as an example for learning how to do them.
    http://www.javaprogrammingforums.com...help-code.html

  5. #5
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Help with an array please!

    This support is only for you as you are totally begginer. There are definitely other ways to handle, but for your level i suggest:
    1. Declare an array of huge size.
    2. Get a numberic value from user.
    3. Limit your array to that value.

  6. #6
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with an array please!

    Thanks for the tip. I am completely not getting this Java stuff at all, which is just further frustrating me. I can't seem to figure this program out AT ALL!!! This is all that I have so far

    import java.util.Scanner;

    //Begin Class Main
    public class Main {

    //Begin Main Method
    public static void main(String[] args) {

    int arraySize;
    int rArray[row][col];


    Scanner input = new Scanner (System.in);

    System.out.print("Enter the number of rows & columns (they will be "
    + "the same): ");
    arraySize = input.nextInt();

    rArray = new int[row][col];

    }
    public static void showArray (int[] array){

    Like I said before, the array size is determined by the user and the rows and columns will always be the same therefor I only need to get one number from them. Yet I can't figure any of it out.


    } //End Main Method

    } //End Class Main

  7. #7
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Help with an array please!

    int rArray[row][col];
    What are row and col? Do you declare a 1d array the same way?
    int[size] numbers;
    Does that look right?

  8. #8
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Help with an array please!

    1. Put your code in code tags.
    2. Indent your code with proper IDE. I believe you are using wordpad somewhat...
    3.
    const row=100;
    const int col=100;
    int [row]Array[col];
    //Get input for rows first.
    //Get input for columns.
    for(int i=0;i<row;i++)
    {
         for(int j=0;j<col;j++)
         {
           array[i][j]=whatever values are;
          }
    }

  9. #9
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Help with an array please!

    If you are going to spoonfeed code at least make it correct Java code.

  10. #10
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Help with an array please!

    Mixed code and pseudocode. So, i don't try to spoonfeed him.

  11. #11
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Help with an array please!

    Well whatever you posted is doing motre harm than good. From experience I have seen clueless n00bs simply copy and paste code into their program and then cannot understand why it doesn't work.
    int [row]Array[col];
    The worse you have done is to perpetuate their misunderstanding of how to declare a 2D array. My post above (while I could have just given them the correct code) was to prompt them to think about how to correct it themselves.

  12. #12
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Help with an array please!

    Yeah that's my bad.
    I correct it.
    int [][]array=new int[row][col];
    Peace.

Similar Threads

  1. Char array to a String array with hex
    By fortune2k in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 20th, 2014, 01:01 PM
  2. prininting out a 4x4 array according to an order by a 1d array
    By fortune2k in forum Collections and Generics
    Replies: 7
    Last Post: November 25th, 2010, 12:54 PM
  3. 2d (4x4) array insdie a 1d array. (Block cipher)
    By fortune2k in forum Collections and Generics
    Replies: 13
    Last Post: November 23rd, 2010, 05:29 PM
  4. [SOLVED] Create new Array from old Array
    By satory in forum Collections and Generics
    Replies: 1
    Last Post: February 24th, 2010, 12:44 PM
  5. Storing an array into an array
    By vluong in forum Collections and Generics
    Replies: 4
    Last Post: September 22nd, 2009, 02:14 PM