I think you are creating an array of wrong bounds- instead of int [][] list=new int [2][mo] there should be int [][] list=new int [mo][2]; ,because for every module you have marks of first and second assignment, am I right?
Java Input scanner to array multidimensional - Stack Overflow
Need help in Java specifically 2D Array
Write a program that inputs a 2D array and displays how the array elements will look in row major form and column major form.
java - Initialized a Two Dimensional Array using Scanner. When trying to display two dimensional array, array values are all nulled - Stack Overflow
Videos
I think you are creating an array of wrong bounds- instead of int [][] list=new int [2][mo] there should be int [][] list=new int [mo][2]; ,because for every module you have marks of first and second assignment, am I right?
You can try this way
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner Sc = new Scanner(System.in);
int mo, i, j, a;
System.out.println(" how many modules you have in this semester ?");
mo = Sc.nextInt();
// creat the Array
int[] tab = new int[mo];
for (a = 0; a < mo; a++) {
System.out.println(" Enter the module name : " + (a + 1));
tab[a] = Sc.nextInt();
}
int[][] list = new int[2][mo];
for (i = 0; i < 2; i++) {
for (j = 0; j < mo; j++) {
System.out.println(" Enter the marks of the " +
(i + 1) + " Assignment : " + (i + 1) + "," + j);
list[i][j] = Sc.nextInt();
}
}
}
}
String result="";//this variable for the last line which print the result
for (int i = 0; i < row; i++) {
result=result+"Data Array "+i+" :";
for (int j = 0; j < column; j++) {
System.out.println("Row [" + i + "]: Column " + j + " :");
matrix[i][j] = sc.nextInt();
result=result+matrix[i][j]+", ";
}
}
System.out.println(result);////for the final result
for(int j = 0; j < column; j++) {
System.out.println("Row ["+i+"]: Column "+j+" :");
matrix[i][j] = sc.nextInt(); //Storing input value here
System.out.println(matrix[i][j]);//Output the input value
}
I'm creating a program that will sum up the outer squares(1) as well as the inner ones(2)(3).
I've already figured out how to sum up the outer boundaries. But, I can't sum up the inner boundaries.
The current dimension is 6x6 but it also needs to be flexible depending on user preference.
Can anyone help? T.T
| 1 | 1 | 1 | 1 | 1 | 1 |
|---|---|---|---|---|---|
| 1 | 2 | 2 | 2 | 2 | 1 |
| 1 | 2 | 3 | 3 | 2 | 1 |
| 1 | 2 | 3 | 3 | 2 | 1 |
| 1 | 2 | 2 | 2 | 2 | 1 |
| 1 | 1 | 1 | 1 | 1 | 1 |
You have to add a new line to your output, when switching to next row:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
StringBuilder output = new StringBuilder();
System.out.println("Enter the number of rows & columns: ");
System.out.print("Enter the number of rows: ");
int row = input.nextInt();
System.out.print("Enter the number of columns: ");
int columns = input.nextInt();
int[][] nums = new int[row][columns];
for (int i = 0; i < row; i++) {
for (int j = 0; j < columns; j++) {
System.out.print("Number " + (j + 1) + ": ");
nums[i][j] = input.nextInt();
output.append(" ").append(nums[i][j]);
}
output.append("\n");
System.out.println("\n");
}
System.out.println(output);
}
You might want to add your new line in the outer loop like so :
for (int i = 0; i < row; i++)
{
for (int j = 0; j < columns; j++)
{
System.out.print("Number " + (j + 1) + ": ");
nums[i][j] = input.nextInt();
output.append(nums[i][j]);
}
output.append("\n");
}