Elevens Activity 4 APCS debugging - java

I am a beginner programmer in APCS, and I am getting this error message on my code (below). How do I fix this? I am not sure what is wrong with it because I believe all of the called variables are within the scope of the code and I declared them as public... Let me know what I should be looking for, and what is wrong please! Thank you very much.
Error:
java.lang.ArrayIndexOutOfBoundsException: 4
at Shuffler.perfectShuffle(Shuffler.java:49)
at Shuffler.main(Shuffler.java:16)
Code:
import java.util.ArrayList;
public class Shuffler
{
private static final int SHUFFLE_COUNT = 1;
private static final int VALUE_COUNT = 4;
public static void main (String[] args)
{
System.out.println ("Results of " + SHUFFLE_COUNT + " consecutive perfect shuffles:");
int[] values1 = new int[VALUE_COUNT];
for (int i = 0; i < values1.length; i++)
{
values1 [i] = i;
}
for (int j = 1; j <= SHUFFLE_COUNT; j++)
{
perfectShuffle (values1);
System.out.print(" " + j + ":");
for (int k = 0; k < values1.length; k++)
{
System.out.println (" " + values1 [k]);
}
System.out.println();
}
System.out.println();
System.out.println("Results of " + SHUFFLE_COUNT + " consecutive efficient selection shuffles:");
int[] values2 = new int[VALUE_COUNT];
for (int i = 0; i < values2.length; i++)
{
values2[i] = i;
}
for (int j = 1; j <= SHUFFLE_COUNT; j++)
{
selectionShuffle(values2);
System.out.print(" " + j + ":");
for (int k = 0; k < values2.length; k++)
{
System.out.print(" " + values2[k]);
}
System.out.println();
}
System.out.println();
}
public static void perfectShuffle (int[] values)
{
int[] temp = new int [52];
int k = 0;
for (int j = 0; j < 25; j++)
{
temp [k] = values [j];
k+=2;
}
k=1;
for (int j=26; j<values.length; j++)
{
temp [k] = values [j];
k+=2;
}
for (int j=0; j<values.length; j++)
{
values [j] = temp [j];
}
}
public static void selectionShuffle (int[] values)
{
ArrayList<Integer> temp=new ArrayList<Integer>(52);
int rando=(int) Math.random()*52;
for (int counter=0; counter<temp.size(); counter++)
{
temp.set (counter,rando);
}
}
}

Related

Using for loop to find an element that appears multiple times in a random, unsorted array

My goal is to print out a user input value (and its corresponding index) if it appears 1 or more times in a random generated array of 50 integers. If I search the array for a value, and it happens to appear more than once, however, only one location of the element is printed, and then the if-else statement is executed. If I remove the break at the end of the third for loop, the whole thing falls apart. I've attached an image but here is the part of it that is giving me an issue. Apologies if the code is not clean, I'm very new.
public static void main(String[] args) {
System.out.println("IDS201 HW3:\n");
System.out.println("1. Generate 50 random integer unsorted list.\n");
Scanner stdin = new Scanner(System.in);
int[] randomNumbers = new int[50];
for(int index = 0; index < randomNumbers.length; index++) {
randomNumbers[index] = (int) (Math.random()*100);
}//end for
int count = 0;
for(int i = 0; i < randomNumbers.length; i++) {
System.out.print(randomNumbers[i] + ",");
count++;
if(count == 10) {
System.out.println();
count = 0;
}
}//end for
System.out.println("\nSearch value?");
int x = stdin.nextInt();
int i;
for(i = 0; i < randomNumbers.length; i++) {
if(randomNumbers[i] == x)
break;}
if (i != randomNumbers.length) {
System.out.println("\nFound " + x + " in array [" + i + "]");}
else {
System.out.println(x + " is not in the list");}
{int temp;
int size = randomNumbers.length;
for(i = 0; i<size; i++ ){
for(int j = i+1; j<size; j++){
if(randomNumbers[i]>randomNumbers[j]){
temp = randomNumbers[i];
randomNumbers[i] = randomNumbers[j];
randomNumbers[j] = temp;
}
}
}
System.out.println("\nSmallest element of the array is: " + randomNumbers[0]);}
System.out.println("\n3. Sort the list:");
int size = randomNumbers.length;
for(i=0; i<size; i++)
{
for(int j=i+1; j<size; j++)
{
if(randomNumbers[i] > randomNumbers[j])
{
int temp = randomNumbers[i];
randomNumbers[i] = randomNumbers[j];
randomNumbers[j] = temp;
}
}
}
System.out.print("Now the Array after Sorting is :\n\n");
int count1 = 0;
for(i=0; i<size; i++)
{
System.out.print(randomNumbers[i]+ ",");
count++;
if(count == 10) {
System.out.println();
count = 0;
}
}
}
}
if statement in for loop
Implementing the comments to your question:
import java.util.Scanner;
public class NumCount {
private static final int RANDOM_NUMBER_COUNT = 50;
private static void display(int[] randomNumbers) {
int count = 0;
for (int i = 0; i < RANDOM_NUMBER_COUNT; i++) {
System.out.print(randomNumbers[i] + ",");
count++;
if (count == 10) {
System.out.println();
count = 0;
}
}
}
private static int[] generateRandomNUmbers() {
int[] randomNumbers = new int[RANDOM_NUMBER_COUNT];
for (int index = 0; index < RANDOM_NUMBER_COUNT; index++) {
randomNumbers[index] = (int) (Math.random() * 100);
}
display(randomNumbers);
return randomNumbers;
}
private static int search(int[] randomNumbers, int x) {
int i;
int count = 0;
for (i = 0; i < randomNumbers.length; i++) {
if (randomNumbers[i] == x) {
System.out.println("\nFound " + x + " in array [" + i + "]");
count++;
}
}
return count;
}
private static int[] sort(int[] randomNumbers) {
int size = randomNumbers.length;
for (int i = 0; i < size; i++) {
for (int j = i + 1; j < size; j++) {
if (randomNumbers[i] > randomNumbers[j]) {
int temp = randomNumbers[i];
randomNumbers[i] = randomNumbers[j];
randomNumbers[j] = temp;
}
}
}
return randomNumbers;
}
/**
* Start here.
*/
public static void main(String[] args) {
System.out.println("IDS201 HW3:\n");
System.out.println("1. Generate " + RANDOM_NUMBER_COUNT + " random integer unsorted list.\n");
int[] randomNumbers = generateRandomNUmbers();
System.out.print("\n2. Search value? ");
Scanner stdin = new Scanner(System.in);
int x = stdin.nextInt();
int count = search(randomNumbers, x);
if (count == 0) {
System.out.println(x + " is not in the list");
}
System.out.println("\n3. Sort the list:");
sort(randomNumbers);
System.out.print("Now the Array after Sorting is :\n\n");
display(randomNumbers);
}
}
Of-course there is no need to search for the smallest number because it will be the first element in the sorted array. Hence I removed that part of your code.

Can't return an array from method in main

I'm very beginner in Java. This is my univercity work of creating a KMeans algorithm. A person writes digits in SAMPLES and number of centers in NUM_CLUSTERS. Algorithm gives cluster center Points
I need to return an array from this method but I can't:
public static int[][] getCentroid_arr(int SAMPLES[],int NUM_CLUSTERS,int centroid_arr[][]).
What exactly array? - It's shown in Console:
Points
1, 2, 7, 22, 44,
It's necessary to return an array because I want to use it for checking in JUnit test.
Instead of return centroid_arr must be this one sorted array:
printArray(arr_sort_asc(sorted_distTocentroid_arr))
After spending about 3 days with this issue I still have no idea how to resolve a problem. I can only get it in print.
I'd be thankfull for any help.
package kmeans;
import java.util.*;
import java.util.Map.Entry;
public class KM_don_touch {
private static final int NUM_CLUSTERS = 5; // Total clusters.
private static final int SAMPLES[] = new int[]{1, 2, 3, 6, 7, 9, 11, 22, 44, 50};
public static void main(String[] args) {
System.out.println("data size is: " + SAMPLES.length);
System.out.println("number of clusters is: " + NUM_CLUSTERS);
System.out.print("dataset is: ");
printArray(SAMPLES);
System.out.println();
int maxDiffData_arr[]=new int[NUM_CLUSTERS];
Kmeans_test3 get_prepare_data = new Kmeans_test3();
maxDiffData_arr=get_prepare_data.prepare_data(SAMPLES);
int centroid_arr[][]=new int[2][NUM_CLUSTERS];
for (int i=0; i<NUM_CLUSTERS; i++) {
centroid_arr[0][i] = 0;
centroid_arr[1][i] = SAMPLES[maxDiffData_arr[i]];
}
System.out.println();
System.out.print("centroid_arr 0 is: ");
printArray(centroid_arr[0]);
System.out.println();
System.out.print("centroid_arr 1 is: ");
printArray(centroid_arr[1]);
System.out.println();
getCentroid_arr(SAMPLES, NUM_CLUSTERS, centroid_arr);
}
public static void firstDiffSort(int[] difference){
int tmp[]=new int[difference.length];
tmp = arr_sort_desc(difference);
for (int i=0; i<difference.length; i++){
}
}
public static int[] arr_sort_desc(int[] arr) {
int tmp;
for (int k = 0; k < arr.length; k++) {
for (int j = k + 1; j < arr.length; j++) {
if (arr[k] < arr[j]) {
tmp = arr[k];
arr[k] = arr[j];
arr[j] = tmp;
}
}
}
return arr;
}
public static int[] arr_sort_asc(int[] arr) {
int tmp;
for (int k = 0; k < arr.length; k++) {
for (int j = k + 1; j < arr.length; j++) {
if (arr[k] > arr[j]) {
tmp = arr[k];
arr[k] = arr[j];
arr[j] = tmp;
}
}
}
return arr;
}
public static int getMinValue(int[] numbers){
int minValue = numbers[0];
for(int i=1; i<numbers.length; i++){
if(numbers[i] < minValue){
minValue = numbers[i];
}
}
return minValue;
}
public static int FindSmallest (int [] arr1){
int index = 0;
int min = arr1[index];
for (int i=1; i<arr1.length; i++){
if (arr1[i] < min ){
min = arr1[i];
index = i;
}
}
return index;
}
public static int[][] getCentroid_arr(int SAMPLES[],int NUM_CLUSTERS,int centroid_arr[][]){
int distance[][]=new int[NUM_CLUSTERS][SAMPLES.length];
int cluster[]=new int[SAMPLES.length];
int clusternodecount[]=new int[NUM_CLUSTERS];
int storedData[][]=new int[1][NUM_CLUSTERS];
int clusterelements[][]=new int[NUM_CLUSTERS][SAMPLES.length];
centroid_arr[0] = centroid_arr[1];
centroid_arr[1]=new int[NUM_CLUSTERS];
for (int i=0; i<NUM_CLUSTERS; i++) {
centroid_arr[1][i] = 0;
}
System.out.println("========== Starting to get new centroid_arr =========");
//сложность алгоритма С2
for(int i=0;i<NUM_CLUSTERS;i++){
for(int j=0;j<SAMPLES.length;j++){
distance[i][j]=Math.abs(SAMPLES[j]-centroid_arr[0][i]);
System.out.print(distance[i][j]+" ,");
}
System.out.println();
}
System.out.println("========== Just ranspose =========");
int smallerDistance=0;
int[][] numbers = new int[SAMPLES.length][NUM_CLUSTERS];
for(int i=0;i<SAMPLES.length;i++){
for(int j=0; j<NUM_CLUSTERS; j++){
numbers[i][j]=distance[j][i];
System.out.print(numbers[i][j]+", ");
}
System.out.println();
smallerDistance = FindSmallest(numbers[i]);
centroid_arr[1][smallerDistance] = centroid_arr[1][smallerDistance]+SAMPLES[i];
clusternodecount[smallerDistance]=clusternodecount[smallerDistance]+1;
cluster[i]=smallerDistance;
}
System.out.println("New clusters are ");
for (int i = 0; i < NUM_CLUSTERS; i++) {
System.out.print("C" + (i + 1) + ": ");
for (int j = 0; j < SAMPLES.length; j++) {
if (cluster[j] == i)
System.out.print(SAMPLES[j] + ", ");
}
System.out.println();
}
System.out.println("======================================== ");
System.out.println("New centroid_arr is ");
for (int k = 0; k < NUM_CLUSTERS; k++) {
centroid_arr[1][k] = centroid_arr[1][k] / clusternodecount[k];
System.out.print(centroid_arr[1][k] + ", ");
}
System.out.println();
boolean isAchived = true;
for (int j = 0; j < NUM_CLUSTERS; j++) {
if (isAchived && centroid_arr[0][j] == centroid_arr[1][j]) {
isAchived = true;
continue;
}
isAchived = false;
}
if (!isAchived) {
getCentroid_arr(SAMPLES, NUM_CLUSTERS, centroid_arr);
}
if (isAchived) {
System.out.println("======================================== ");
System.out.println(" Final Cluster is ");
for (int i = 0; i < NUM_CLUSTERS; i++) {
System.out.print("C" + (i + 1) + ": ");
for (int j = 0; j < SAMPLES.length; j++) {
if (cluster[j] == i)
System.out.print(SAMPLES[j] + ", ");
}
System.out.println();
}
for (int i=0; i<SAMPLES.length; i++){
for (int j=0; j<NUM_CLUSTERS; j++){
if (j==cluster[i])
clusterelements[j][i]=SAMPLES[i];
}
}
int distTocentroid_arr[][]=new int[NUM_CLUSTERS][SAMPLES.length];
for (int i=0; i<NUM_CLUSTERS; i++){
for (int j=0; j<SAMPLES.length; j++){
distTocentroid_arr[i][j]=Math.abs(centroid_arr[1][i]-clusterelements[i][j]);
}
}
System.out.println("Points:");
int sorted_distTocentroid_arr[] = new int[NUM_CLUSTERS];
for (int i=0; i<NUM_CLUSTERS; i++){
smallerDistance = FindSmallest(distTocentroid_arr[i]);
sorted_distTocentroid_arr[i]=clusterelements[i][smallerDistance];
}
printArray(arr_sort_asc(sorted_distTocentroid_arr));
}
return centroid_arr;
}
public int[] prepare_data(int[] arr) {
int sorted_arr[] = arr_sort_desc(SAMPLES);
int maxDiff_arr[] = new int[SAMPLES.length];
int sorted_diff[] = new int[SAMPLES.length];
maxDiff_arr[0] = Math.abs(sorted_arr[0] - sorted_arr[1]);
for (int i = 1; i < SAMPLES.length; i++) {
maxDiff_arr[i] = Math.abs(sorted_arr[i] - sorted_arr[i - 1]);
}
Map<Integer, Integer> m1= new HashMap<Integer, Integer>();
int k=0;
for(int i:maxDiff_arr){
m1.put(k++, i);
}
m1= sortByValues(m1);
int i=0;
for (int key: m1.keySet()){
sorted_diff[i++]=key;
}
return sorted_diff;
}
public static <K extends Comparable,V extends Comparable> Map<K,V> sortByValues(Map<K,V> map){
List<Map.Entry<K,V>> entries = new LinkedList<Map.Entry<K,V>>(map.entrySet());
Collections.sort(entries, new Comparator<Map.Entry<K,V>>() {
#Override
public int compare(Entry<K, V> o1, Entry<K, V> o2) {
return o2.getValue().compareTo(o1.getValue());
}
});
Map<K,V> sortedMap = new LinkedHashMap<K,V>();
for(Map.Entry<K,V> entry: entries){
sortedMap.put(entry.getKey(), entry.getValue());
}
return sortedMap;
}
public static void printArray(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + ", ");
}
}
public static void printArray2(int[][] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[1][i] + ", ");
}
}
}
Console output

Can not get precise results for the hourglass algorithm

Here is the link for definition of the hourglass problem:
https://www.hackerrank.com/challenges/30-2d-arrays
I wrote the following program:
package day11;
import java.util.Scanner;
public class Solution {
public static void main(String ... args){
Scanner scan = new Scanner(System.in);
int[][] arr = new int[6][6];
int maxHourGlassValue = 0;
int temp = 0;
int currMax = 0;
int k = 0, l = 0;
for(int i = 0 ; i < 6 ; i++){
for(int j = 0 ; j < 6 ; j++){
arr[i][j] = scan.nextInt();
}
}
for(int i = 1 ; i < 5 ; i++){
for(int j = 1 ; j < 5 ; j++){
if(maxHourGlassValue < currMax){
maxHourGlassValue = currMax;
}
}
}
System.out.println(maxHourGlassValue);
}
}
I could only run 6 out of 8 given test cases. What could possibly go wrong ????
Try this code, i did not write that code, i just copy it and modified it from here
import java.util.Scanner;
public class Test {
public static void main(String ... args){
Scanner scan = new Scanner(System.in);
int size = 6;
int[][] m = new int[size][size];
//numbers input
for(int i=0; i<size; i++)
{
for(int j=0; j<size; j++)
{
m[i][j] = scan.nextInt();
}
}
int temp = 0, MaxSum = -99999;
for (int i=0; i<size; ++i) {
for (int j=0; j<size; ++j) {
if (j+2 < size && i+2 < size) {
temp = m[i][j] + m[i][j+1] + m[i][j+2] + m[i+1][j+1] + m[i+2][j] + m[i+2][j+1] + m[i+2][j+2];
if (temp >= MaxSum) {
MaxSum = temp;
}
}
}
}
System.out.println(MaxSum);
}
}
Below is the code which will run successfully for all the test cases of hourglass problem.
public static void main(String[] args) {
try (Scanner scan = new Scanner(System.in)) {
int[][] arr = new int[6][6];
int maxHourGlassValue = -63;//Assigning (-9*7=)-63 which is the minimum possible value of "hourglass sum".
//Reading inputs.
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
arr[i][j] = scan.nextInt();
}
}
//Logic.
/**
* Index of both i and j will run from 1 to 4 (one less than n-1 where n = 6)
* So for each i and j iteration calculating the sum of hourglass.
*/
int iHGValueTemp = 0;
for (int i = 1; i < 5; i++) {
for (int j = 1; j < 5; j++) {
iHGValueTemp = arr[i][j] + /*Main element*/
arr[i - 1][j - 1] + arr[i - 1][j] + arr[i - 1][j + 1]+ /*Top three elements of main element.*/
arr[i + 1][j - 1] + arr[i + 1][j] + arr[i + 1][j + 1]; /*Bottom three elements of main element.*/
if (iHGValueTemp > maxHourGlassValue) {
maxHourGlassValue = iHGValueTemp;
}
}
}
//Output.
System.out.println(maxHourGlassValue);
}
}
I have written description within code in the comments only. Please refer to that and discuss with me if any doubt.

Switching rows on 2d-array generated at random

This is a 2d array which is generated at random, and stores 20 integers from 100-1000. I got that part right, I'm trying to switch the rows of this 2d array and not having much luck, i tried the temp but for some reason it comes out as column array...
public static void main (String args[]){
int [][] randArray = new int[2][10];
Random rnd = new Random();
for(int i=0; i<randArray.length; i++)
for(int j=0; j<randArray[i].length; j++)
randArray[i][j] = 100 + rnd.nextInt(900);
for (int i=0; i < randArray.length; i++){
System.out.println("Row " + i);
for (int j=0; j < randArray[i].length; j++)
System.out.print(randArray[i][j]+ " ");
System.out.println();
}
System.out.println("Switch row ");
int temp = 0;
for (int j=0; j < randArray[0].length; j++){
temp = randArray[0][j];
randArray[0][j] = randArray[1][j];
randArray[1][j] = temp;
//System.out.println(randArray[i][j]+ " ");
}
}
}
Switch the rows as in row1 [1,2,3,4,5] row2 [6,7,8,9,10] to row1[6,7,8,9,10] row2[1,2,3,4,5]
You can use Arrays.deepToString(Object[]) to print the array. Display the array after you perform your swap. Something like,
int[][] randArray = new int[2][10];
Random rnd = new Random();
for (int i = 0; i < randArray.length; i++) {
for (int j = 0; j < randArray[i].length; j++) {
randArray[i][j] = 100 + rnd.nextInt(900);
}
}
System.out.println(Arrays.deepToString(randArray));
System.out.println("Switch row ");
for (int j = 0; j < randArray[0].length; j++) {
int temp = randArray[0][j];
randArray[0][j] = randArray[1][j];
randArray[1][j] = temp;
}
System.out.println(Arrays.deepToString(randArray));
Your code is working actually I fixed only the compile error;
public static void main(String args[]) {
int[][] randArray = new int[2][10];
Random rnd = new Random();
for (int i = 0; i < randArray.length; i++)
for (int j = 0; j < randArray[i].length; j++)
randArray[i][j] = 100 + rnd.nextInt(900);
for (int i = 0; i < randArray.length; i++) {
System.out.println("Row " + i);
for (int j = 0; j < randArray[i].length; j++)
System.out.print(randArray[i][j] + " ");
System.out.println();
}
System.out.println("Switch row ");
int temp = 0;
for (int j = 0; j < randArray[0].length; j++) {
temp = randArray[0][j];
randArray[0][j] = randArray[1][j];
randArray[1][j] = temp;
//System.out.println(randArray[i][j]+ " "); no i variable
}
//I added only below lines to print your result
for (int i = 0; i < randArray.length; i++) {
System.out.println("Row " + i);
for (int j = 0; j < randArray[i].length; j++)
System.out.print(randArray[i][j] + " ");
System.out.println();
}
}

Cannot get the addition to output [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Everything is good, but i just cant get the addition to show up? When I run the program it is blank when it comes to the addition of the matrixes part. Thanks in advance. BtW does anyone know how I would make this display right column justified?
public static void displayMatrixes(int[][] matrix1, int[][] matrix2, int[][] resultsMatrix) {
System.out.println("This is how i want it to output");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(matrix1[i][j] + " ");
}
System.out.print("+ ");
for (int j = 0; j < 3; j++) {
System.out.print(matrix2[i][j]+ " ");
}
System.out.print("= ");
for (int j = 0; j < 3; j++) {
System.out.print(resultsMatrix[i][j]+ " ");
}
System.out.println();
}
)
This is my code
import java.util.Scanner;
public class MatrixAdd
{
public static void main(String arg[])
{
Scanner input = new Scanner(System.in);
int a[][]= new int[3][3];
int b[][] = new int[3][3];
int row, column;
System.out.println("\nEnter Matrix A: \n");
for (int i = 0 ; i < 3 ; i++){
for (int j = 0; j<3 ; j++){
a[i][j] = input.nextInt();
}
}
System.out.println("\nEnter Matrix B: \n");
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
b[i][j] = input.nextInt();
}
}
System.out.println("\nMatrix A + Matrix B = Matrix C: \n");
int[][] resultingMatrix = addMatrix(a, b);
}
public static int[][] addMatrix(int[][] a, int[][] b){
int[][] result = new int[a.length][a[0].length];
for (int i = 0; i < result.length; i++) {
for (int j = 0; j < result[0].length; j++){
result[i][j]=a[i][j] + b[i][j];
}
}
for (int i = 0; i < a.length; i++) {
char plus = '+';
for (int j = 0; j < a[0].length; j++) {
System.out.print(" " + a[i][j]);
}
if (i == a.length / 2)
System.out.print(" " + plus + " ");
else {
System.out.print(" ");
}
for (int j = 0; j < b[0].length; j++) {
System.out.print(" " + b[i][j]);
}
if (i == a.length / 2)
System.out.print(" = ");
else {
System.out.print(" ");
}
for (int j = 0; j < result[0].length; j++) {
System.out.print(" " + " " + result[i][j]);
}
System.out.println();
}
return result;
}//end of add matrices
}//end of class
I doubt that the given program compiles. In this line: int[][] resultsMatrix = displayMatrixes(a, b); you are expecting an int[][], but in your method displayMatrixes you are not returning anything. You are also expecting a 3rd parameter which you are not passing.
Also, the displayMatrixes method has no return value, which since you are returning something at the end, you must have. Try it again like so:
public static void main(String arg[]) {
Scanner input = new Scanner(System.in);
int a[][] = new int[3][3];
int b[][] = new int[3][3];
int row, column;
System.out.println("\nEnter Matrix A: \n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
a[i][j] = input.nextInt();
}
}
System.out.println("\nEnter Matrix B: \n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
b[i][j] = input.nextInt();
}
}
System.out.println("\nMatrix A + Matrix B = Matrix C: \n");
displayMatrixes(a, b);
}
public static void displayMatrixes(int[][] a, int[][] b) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(a[i][j] + " ");
}
System.out.print("+ ");
for (int j = 0; j < 3; j++) {
System.out.print(b[i][j] + " ");
}
System.out.print("= ");
for (int j = 0; j < 3; j++) {
System.out.print((a[i][j] + b[i][j]) + " ");
}
System.out.println();
}
}
}
printf with The "%3d" specifier means a minimum width of three spaces, which, by default, will be right-justified.
for your right alignment question you can at http://alvinalexander.com/programming/printf-format-cheat-sheet

Categories

Resources