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):
Code java:
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
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) )?
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.
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
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.
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
Re: Help with an array please!
What are row and col? Do you declare a 1d array the same way?
Does that look right?
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.
Code java:
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;
}
}
Re: Help with an array please!
If you are going to spoonfeed code at least make it correct Java code.
Re: Help with an array please!
Mixed code and pseudocode. So, i don't try to spoonfeed him.
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.
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.
Re: Help with an array please!
Yeah that's my bad.
I correct it.
Code java:
int [][]array=new int[row][col];
Peace.