Question

Can you help me with this code in Java??? import java.util.Scanner; public class Main { public...

Can you help me with this code in Java???

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

int rows = 3;

int columns = 4;

int[][] arr = new int[rows][columns];

for(int i = 0; i < rows; ++i) {

for(int j = 0; j < columns; ++j) {

System.out.println("Enter a value: ");

arr[i][j] = scan.nextInt();

}

}

System.out.println("The entered matrix:");

for(int i = 0; i < rows; ++i) {

for(int j = 0; j < columns; ++j) {

System.out.print(" " + arr[i][j]);

}

System.out.println();

}

sumColumns(columns,rows);

scan.close();

}

public static void sumColumns(int columns, int rows){

int[][] arr = new int[rows][columns];

for(int i = 0; i < 4; ++i) {

int total=0;

for(int j = 0; j < 3; ++j) {

total += arr[j][i];

}

System.out.println("Sum of column " + i + " is " + total);

}

}

}

My output is:

The entered matrix:
9 1 2 4
2 2 8 0
3 3 3 3
Sum of column 0 is 0
Sum of column 1 is 0
Sum of column 2 is 0
Sum of column 3 is 0

The total is supposed to be the sum of the columns, but it is not changing in my for loop.

The output is supposed to be:

The entered matrix:
9 1 2 4
2 2 8 0
3 3 3 3
Sum of column 0 is 14
Sum of column 1 is 6
Sum of column 2 is 13
Sum of column 3 is 7

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program Screenshot:

Sample Ouput:

Program code:

import java.util.Scanner;

public class Main {

   public static void main(String[] args) {

       Scanner scan = new Scanner(System.in);

       int rows = 3;

       int columns = 4;

       int[][] arr = new int[rows][columns];

       for(int i = 0; i < rows; ++i) {

           for(int j = 0; j < columns; ++j) {

               System.out.println("Enter a value: ");

               arr[i][j] = scan.nextInt();

           }

       }

       System.out.println("The entered matrix:");

       for(int i = 0; i < rows; ++i) {

           for(int j = 0; j < columns; ++j) {

               System.out.print(" " + arr[i][j]);

           }

           System.out.println();

       }

       sumColumns(columns,rows, arr);

       scan.close();

   }

   public static void sumColumns(int columns, int rows, int[][] arr){

       for(int i = 0; i < columns; ++i) {

           int total=0;

           for(int j = 0; j < rows; ++j) {

               total += arr[j][i];

           }

           System.out.println("Sum of column " + i + " is " + total);

       }

   }

}

Add a comment
Know the answer?
Add Answer to:
Can you help me with this code in Java??? import java.util.Scanner; public class Main { public...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • draw a flew chart for this code // written by Alfuzan Mohammed package matreix; import java.util.Scanner;...

    draw a flew chart for this code // written by Alfuzan Mohammed package matreix; import java.util.Scanner; public class Matrix {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);    System.out.print("Enter number of rows: first matrix ");    int rows = scanner.nextInt();    System.out.print("Enter number of columns first matrix: ");    int columns = scanner.nextInt();    System.out.print("Enter number of rows: seconed matrix ");    int rowss = scanner.nextInt();    System.out.print("Enter number of columns seconed matrix:...

  • Need help with the UML for this code? Thank you. import java.util.Scanner;    public class Assignment1Duong1895...

    Need help with the UML for this code? Thank you. import java.util.Scanner;    public class Assignment1Duong1895    {        public static void header()        {            System.out.println("\tWelcome to St. Joseph's College");        }        public static void main(String[] args) {            Scanner input = new Scanner(System.in);            int d;            header();            System.out.println("Enter number of items to process");            d = input.nextInt();      ...

  • import java.util.Scanner; public class SCAN { public static void main(String[ ] args) { int x, y,...

    import java.util.Scanner; public class SCAN { public static void main(String[ ] args) { int x, y, z; double average; Scanner scan = new Scanner(System.in); System.out.println("Enter an integer value"); x = scan.nextInt( ); System.out.println("Enter another integer value"); y = scan.nextInt( ); System.out.println("Enter a third integer value"); z = scan.nextInt( ); average = (x + y + z) / 3; System.out.println("The result of my calculation is " + average); } } What is output if x = 0, y = 1 and...

  • import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        //...

    import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Welcome to the Triangle Maker! Enter the size of the triangle.");        Scanner keyboard = new Scanner(System.in);    int size = keyboard.nextInt();    for (int i = 1; i <= size; i++)    {    for (int j = 0; j < i; j++)    {    System.out.print("*");    }    System.out.println();    }    for (int...

  • I need a java flowchart for the following code: import java.util.*; public class Main {   ...

    I need a java flowchart for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");       ...

  • the code needs to be modifed. require a output for the code Java Program to Implement...

    the code needs to be modifed. require a output for the code Java Program to Implement Insertion Sort import java.util.Scanner; /Class InsertionSort * public class Insertion Sort { /Insertion Sort function */ public static void sort( int arr) int N- arr.length; int i, j, temp; for (i-1; i< N; i++) j-i temp arrli; while (j> 0 && temp < arrli-1) arrli]-arrli-1]; j-j-1; } arrlj] temp; /Main method * public static void main(String [] args) { Scanner scan new Scanner( System.in...

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

  • This is the contents of Lab11.java import java.util.Scanner; import java.io.*; public class Lab11 { public static...

    This is the contents of Lab11.java import java.util.Scanner; import java.io.*; public class Lab11 { public static void main(String args[]) throws IOException { Scanner inFile = new Scanner(new File(args[0])); Scanner keyboard = new Scanner(System.in);    TwoDArray array = new TwoDArray(inFile); inFile.close(); int numRows = array.getNumRows(); int numCols = array.getNumCols(); int choice;    do { System.out.println(); System.out.println("\t1. Find the number of rows in the 2D array"); System.out.println("\t2. Find the number of columns in the 2D array"); System.out.println("\t3. Find the sum of elements...

  • Fix this program package chapter8_Test; import java.util.Scanner; public class Chapter8 { public static void main(String[] args)...

    Fix this program package chapter8_Test; import java.util.Scanner; public class Chapter8 { public static void main(String[] args) { int[] matrix = {{1,2},{3,4},{5,6},{7,8}}; int columnChoice; int columnTotal = 0; double columnAverage = 0; Scanner input = new Scanner(System.in); System.out.print("Which column would you like to average (1 or 2)? "); columnChoice = input.nextInt(); for(int row = 0;row < matrix.length;++row) { columnTotal += matrix[row][columnChoice]; } columnAverage = columnTotal / (float) matrix.length; System.out.printf("\nThe average of column %d is %.2f\n", columnAverage, columnAverage); } } This program...

  • I need the following java code commented import java.util.Scanner; public class Main { public static void...

    I need the following java code commented import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner (System.in); int productNo=0; double product1; double product2; double product3; double product4; double product5; int quantity; double totalsales=0; while(productNo !=0) System.out.println("Enter Product Number 1-5"); productNo=input.nextInt(); System.out.println("Enter Quantity Sold"); quantity=input.nextInt(); switch (productNo) { case 1: product1=1.00; totalsales+=(1.00*quantity); break; case 2: product2=2.00; totalsales+=(2.00*quantity); break; case 3: product3=6.00; totalsales+=(6.00*quantity); break; case 4: product4=23.00; totalsales+=(23.00*quantity); break; case 5: product5=17.00; totalsales+=(17.00*quantity); break;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT