I already made the code and i can input values using the JOptionPane Input Dialog.
But the problem is when printing the ouptut of the 2d array it shows like this:
The output should be like this: (can be without the equal and underline)
5 9 8 = 22
3 8 2 = 13
4 3 9 = 16
__________________________
12 20 19
This is the code. I can input values but the only problem is how can i print the matrix.
import javax.swing.JOptionPane;
public class TwoDimensional_Array {
public static void main(String[] args) {
String output = "";
int [][] num = new int [3][3];
int [] sum = new int[10];
int [] sum1 = new int [10];
System.out.println("\n3 by 3 Array - Enter Matrix Elements.");
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
String input = JOptionPane.showInputDialog(null, "Enter elements in pockets ["+ i +"]["+ j +"]:", "3 by 3 - Two-Dimensonal Array", -1);
num[i][j] = Integer.parseInt(input);
}
}
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
sum[i] = sum[i] + num[i][j];
sum1[i] = sum1[i] + num[j][i];
}
}
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
output += (num[i][j] + "\t");
}
output += ("=\t" + sum[i]); //print the sum of rows
}
output += ("__________________________________");
for(int i = 0; i < 3; i++)
{
output += (sum1[i] + "\t"); // print the sum of columns
}
JOptionPane.showMessageDialog(null, output, "Two-Dimensional Array", -1);
}
}
To get a new line in the dialog, append the \n character instead of the \t character.
Here ist some more information about the Java line separators.
Related
I tried taking input of a 6 by 6 matrix in java using the string split function when the string is input in the following way, and to print the matrix.
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
The output that I get is
Main.java:24: error: incompatible types: String[] cannot be converted to String
c[j] = b[i].split(" ");
my code:
import java.util.*;
import java.io.*;
class Solution {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
int a[][] = new int[6][6];
String b[] = new String[6];
for (int i = 0; i < 6; i++) {
b[i] = s.nextLine();
}
// initializing the 2d array a[][]
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
String c[] = new String[6];
c[j] = b[i].split(" ");
a[i][j] = Integer.parseInt(c[j]);
}
}
// printing the input array
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
System.out.print("\ta[i][j]\t");
}
}
}
}
pls, suggest how I can overcome this error
When we call split function of String return the String[]. So c[j] (which is of type String) can't be equal to String[].
Below code should be replaced as:
// initializing the 2d array a[][]
for (int i = 0; i < 6; i++) {
String[] c = b[i].split(" ");
for (int j = 0; j < 6; j++) {
a[i][j] = Integer.parseInt(c[j]);
}
}
The return type of split() function is type of array. Because you are asking java to give me each value as separate which is separated by " " (space). So java will create an array of each value and returns you the array. For storing the array you need an variable of type array. here c represent an array, but c[j] represents an single index of the array.
You can change your code like:
for (int i = 0; i < 6; i++) {
String c[] = b[i].split(" ");
for (int k = 0; k < c.length; k++) {
a[i][k] = Integer.parseInt(c[k]);
}
}
The the inputs are integer and you are converting them to integer later, I would suggest you to take input as integer like below:
class Solution {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
int a[][] = new int[6][6];
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
a[i][j] = s.nextInt();
}
}
// printing the input array
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
System.out.print("\ta[i][j]\t");
}
}
}
}
I want to create a 1D array that saves the min columns from my 2D array, but I get wrong the min values. My theseisLine array is a copy from my intP array that I store there the column numbers, so theseisLine shows the min from each column.
package themab2018;
import java.io.IOException;
import java.util.Scanner;
import java.util.Arrays;
public class THEMAB2018 {
static void displayP(int intP[][]) {
System.out.println("intP2D");
for(int i = 0; i < intP.length; i++) {
for(int j = 0; j < intP[i].length; j++)
System.out.print(intP[i][j] + " ");
System.out.println();
}
}
static void findMinCol(int intP[][] ,int n) {
int theseisLine[] = new int[n];
if(theseisLine.length < intP.length)
for(int i = 0; i < theseisLine.length; i++)
theseisLine[i] = intP[0][i];
else
for(int i = 0; i < intP.length; i++)
theseisLine[i] = intP[0][i];
int min = theseisLine[0];
System.out.println();
System.out.println("Min Array");
for(int i = 0; i < theseisLine.length; i++) {
if(theseisLine[i] < min) {
min = theseisLine[i];
}
System.out.print(min + " ");
}
}
public static void main(String[] args){
int m , n;
System.out.println("Import m , n");
do {
System.out.print("Give m ");
m = scannerUserInput.getInteger();
System.out.print("Give n ");
n = scannerUserInput.getInteger();
} while (m < 1 && n < 1);
int intP2D[][] = new int [m][n];
int theseisLine[] = new int[n];
for(int i = 0; i < intP2D.length; i++){
for(int j = 0; j < intP2D[i].length; j++) {
intP2D[i][j] = (int) (Math.random() * (10 - 1)+1)+1;
}
}
displayP(intP2D);
findMinCol(intP2D, m);
}
}
The result that I get example :
Import m , n
Give m 3
Give n 4
intP2D
3 9 7 2
8 9 7 6
5 8 8 7
Min Array
3 3 3
The result that i want in this example :
Import m , n
Give m 3
Give n 4
intP2D
3 9 7 2
8 9 7 6
5 8 8 7
Min Array
3 8 7 2
Firstly, while calling findMinCol(intP2D, m); you need to pass the count of column i.e. n and not count of row.
Secondly, while calculating the min element in the column, you are comparing all element with int min = theseisLine[0]; which might not be correct.
import java.io.IOException;
import java.util.Scanner;
import java.util.Arrays;
public class THEMAB2018 {
static void displayP(int intP[][]) {
System.out.println("intP2D");
for(int i = 0; i < intP.length; i++) {
for(int j = 0; j < intP[i].length; j++)
System.out.print(intP[i][j] + " ");
System.out.println();
}
}
static void findMinCol(int intP[][] ,int n) {
int theseisLine[] = new int[n];
for(int i = 0; i < theseisLine.length; i++)
theseisLine[i] = intP[0][i];
System.out.println();
System.out.println("Min Array");
for(int i = 0; i < theseisLine.length; i++) {
for (int j = 1; j < intP.length; j++) {
if (intP[j][i] < theseisLine[i]) {
theseisLine[i] = intP[j][i];
}
}
System.out.print(theseisLine[i] + " ");
}
}
public static void main(String[] args){
int m , n;
System.out.println("Import m , n");
m = 3; n = 4;
int intP2D[][] = new int [m][n];
int theseisLine[] = new int[n];
for(int i = 0; i < intP2D.length; i++){
for(int j = 0; j < intP2D[i].length; j++) {
intP2D[i][j] = (int) (Math.random() * (10 - 1)+1)+1;
}
}
displayP(intP2D);
findMinCol(intP2D, **n**);
}
}
the output I am getting:
intP2D
2 5 8 7
10 6 9 3
7 9 3 8
Min Array
2 5 3 3
I don't know what you want to check in your finding method with the if-else block. You don't need it anyway to find the minimum of each column, that's why I removed it. A simple nested for loop solves your problem.
Additionally, you don't need to pass the number of columns as a parameter, because it can be easily queried from the matrix. I still left it in to avoid having to change the method call.
static void findMinCol(int intP[][] ,int n) {
int rows = intP.length;
int cols = intP[0].length;
int[] result = new int[cols];
for(int i = 0; i < cols; i++){
int min = Integer.MAX_VALUE;
for(int j = 0; j < rows; j++){
if(min > intP[j][i]){
min = intP[j][i];
}
}
result[i] = min;
}
for(int i = 0; i < result.length; i++) {
System.out.print(result[i] + " ");
}
}
I am able to print the 2 2D arrays with the numbers from user input and I am also able to print the odd numbers, but I am having trouble combining the two forms of code so that the odd numbers stay consistent with their cell and not just in one print line. How would I print the odd numbers leaving the non odds as blanks in 2 3x3 arrays?
Heres the code for printing the array:
public static void display ( int[][] FirstArray, int[][] SecondArray)
{
//Print first array
System.out.print("Array1: \n");
for (int row = 0; row < FirstArray.length; row++)
{
for(int column = 0; column < FirstArray[row].length; column++)
{
System.out.print(FirstArray[row][column] + " ");
}
System.out.println();
}
//Print second array
System.out.print("Array2: \n");
for (int row = 0; row < SecondArray.length; row++)
{
for(int column = 0; column < SecondArray[row].length; column++)
{
System.out.print(SecondArray[row][column] + " ");
}
System.out.println();
}
}
ex output:
array 1: array2
3 3 3 4 4 4
3 3 3 4 4 4
3 3 3 4 4 4
Here is the code for printing the odd numbers without in the 3x3 format like the code above:
public static void display(int[][] FirstArray, int[][] SecondArray)
{
int count=0;
for (int i = 0; i < FirstArray.length; i++) {
for (int j = 0; j < FirstArray.length; j++)
{
if(FirstArray[i][j]%2==1)
{
System.out.println(m1[i][j]);
}
}
}
for (int i = 0; i < SecondArray.length; i++) {
for (int j = 0; j < SecondArray.length; j++)
{
if(SecondArray[i][j]%2==1)
{
System.out.println(SecondArray[i][j]);
}
}
}
ex output:
3 3 3 3 3 3 3 3 3 (odd numbers displayed but in one line)
Ex output of what Im looking for(assuming i entered in even numbers too):
3 3 3
3 3 3
3 3
You can redress your print statements in both the loops as :
for (int i = 0; i < FirstArray.length; i++) {
for (int j = 0; j < FirstArray.length; j++) {
if(FirstArray[i][j]%2==1) {
System.out.print(m1[i][j] + " "); // odd number and space
} else {
System.out.print(" "); // blank space for even numbers
}
}
System.out.println(); // next line for next row
}
Scanner sc = new Scanner(System.in);
System.out.println("Enter numbers of rows : ");
int n = sc.nextInt();
System.out.println("Enter number of colmns : ");
int m = sc.nextInt();
int[][] array = new int[n][m];
System.out.println("Enter elements of array : ");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.print(" X[ " + i + "," + j + "] = ");
array[i][j] = sc.nextInt();
}
}
System.out.print("Even numbers on position :");
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
if (array[i][j] % 2 != 0) {
System.out.print(array[i][j] + " ");
}
}
}
user input n number of rows in 2d array
Elements are stored in an array of size [n][4].
Input Format:
First line of the input is an integer ānā that denotes the number of rows.
Next n lines contain the elements of the row
Output Format:
if 2 rows match then separate it by hyphen
If no rows is same, print "None".
If the array size is negative, print "Invalid Input" and terminate the program.
Sample Input 1:
5
3 1 2 4
2 4 5 1
3 1 2 4
2 4 5 1
3 1 2 4
Sample Output 1:
1-3
1-5
2-4
3-5
Sample Input 2:
3
3 1 2 4
2 4 5 1
3 4 2 5
Sample Output 2:
None
Sample Input 3:
-5
Sample Output 3:
Invalid Input
i have tried the following code but it didnt work
import java.util.Arrays;
import java.util.Scanner;
import java.lang.*;
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if(n <= 0)
{
System.out.println("Invalid Input");
}
else
{
int[][] data = new int[n][4];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < 4; j++)
{
data[i][j] = sc.nextInt();
}
}
Object[] array = new Object[4];
Object[] array1 = new Object[4];
int idx = 0;
int idx1 = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < 4; j++)
{
array[idx] = data[i][j];
array1[idx1] = data[i + 1][j];
idx++;
idx1++;
if (Arrays.deepEquals(array, array1))
{
System.out.println(i + "-" + j);
}
else
System.out.println("None");
}
}
} int[][] data = new int[n][4];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < 4; j++)
{
data[i][j] = sc.nextInt();
}
}
Object[] array = new Object[4];
Object[] array1 = new Object[4];
int idx = 0;
int idx1 = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < 4; j++)
{
array[idx] = data[i][j];
array1[idx1] = data[i + 1][j];
idx++;
idx1++;
if (Arrays.deepEquals(array, array1))
{
System.out.println(i + "-" + j);
}
else
System.out.println("None");
}
}
}}}
you are taking only the 1st elements in array and array1 in each step not the entire row.
what you need to do is you have to take entire 1 row and compare with the other rows more 'for' loops
I am trying to write a nested for loop that will print out the values of the following code in a specific order:
public static void main(String[] args) {
int[][] array2d = new int[3][5];
for (int i = 0; i < array2d.length; i++) {
for (int j = 0; j < array2d[0].length; j++) {
array2d[i][j] = (i * array2d[0].length) + j + 1;
}
}
for (int x = 0; x <= 4; x++) {
for (int y = 0; y <= 2; y++) {
System.out.println(array2d[y][x]);
}
}
}
}
The current array prints the way I want it, but each printout on a separate line.
I want the output (on a single line) to be this:
1 6 11 2 7 12 3 8 13 4 9 14 5 10 15
Thanks for the help.
You can use System.out.print instead:
System.out.print(array2d[y][x] + " ");
Replace println with print and it should work
String s = "";
for (int i = 0; i < array2d.length; i++) {
for (int j = 0; j < array2d[i].length; j++) {
s += array2d[i][j] + " ";
}
}
System.out.println(s);
public static void main(String[] args) {
int[][] array2d = new int[3][5];
for (int i = 0; i < array2d.length; i++) {
for (int j = 0; j < array2d[0].length; j++) {
array2d[i][j] = (i * array2d[0].length) + j + 1;
}
}
StringBuilder builder = new StringBuilder();
for (int x = 0; x <= 4; x++) {
for (int y = 0; y <= 2; y++) {
builder.append(array2d[y][x]);
if(!(x == 4 && y == 2)){
builder.append(" ");
}
}
}
System.out.println(builder.toString());
}
You basically had it right, except for changing the println to be print and formatting the string how you want. I changed it a little to show how the StringBuilder works. Whenever possible I use a StringBuilder because it is more convenient.