Ian Santos - 1 year ago 66

Java Question

this is a pattern forming in java. Anyway, the instructions is to show odd numbers from 1-9 from user input into a rectangular pattern.

This is my code so far.. Any suggestions?

`//Library Imports`

import java.util.Scanner;

import java.util.Arrays;

//Main

public class blah {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

Strict run = new Strict();

//Input

int[][] m1 = new int[3][3];

int[][] m2 = new int[3][3];

System.out.print("Enter 9 numbers for List 1 / Enter 9 numbers for List 2\n");

for (int arr1 = 0; arr1 < m1.length; arr1++) {

for (int arr2 = 0; arr2 < m1[arr1].length; arr2++) {

m1[arr1][arr2] = input.nextInt();

}

}

for (int arr1 = 0; arr1 < m2.length; arr1++) {

for (int arr2 = 0; arr2 < m2[arr1].length; arr2++) {

m2[arr1][arr2] = input.nextInt();

}

}

//Displays only those values of the arrays that are odd in rectangular form (row by row for each array).

System.out.print("\n");

System.out.print("Display odd values in a rectangular form \n ");

run.display(m1, m2);

}

}

class Strict {

//5.) Displays only those values of the arrays that are odd in rectangular form (row by row for each array).

public static void display(int[][] m1, int[][] m2) {

for (int i = 0; i < m1.length; i++) {

for (int j = 0; j < m1.length; j++) {

if ((m1[i][j] % 2) == 1) {

if ((m1[i][j] / 10 == 0)) {

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

} else {

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

}

} else {

System.out.print(" ");

}

System.out.print("");

//Statements (m1=[i][j] = m2[i][j])

}

System.out.print(" ");

System.out.print(" ");

}

}

}

The output in my code is:

`Display odd values in a rectangular form`

1 3 5 7 9

The result should be like this. (I think I'm almost near the output..)

*********************Update***************************************

I tried to revise the for loop:

`//5.) Displays only those values of the arrays that are odd in rectangular form (row by row for each array).`

public static void display(int[][] m1, int[][] m2) {

for (int i = 0; i < m1.length; i++) {

for (int j = 0; j < m1.length; j++) {

if ((m1[i][j] % 2) == 1) {

if ((m1[i][j] / 10 == 0)) {

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

} else {

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

}

} else {

System.out.print(" ");

}

System.out.print(" ");

//Statements (m1=[i][j] = m2[i][j])

}

System.out.println();

}

System.out.print("\n");

for (int k = 0; k < m1.length; k++) {

for (int l = 0; l < m1.length; l++) {

if ((m2[k][l] % 2) == 1) {

if ((m2[k][l] / 10 == 0)) {

System.out.print(" " + m2[k][l] + " ");

} else {

System.out.print((m2[k][l] + " "));

}

} else {

System.out.print(" ");

}

System.out.print(" ");

//Statements (m1=[i][j] = m2[i][j])

}

System.out.println();

}

}

The current output is:

`Display odd values in a rectangular form`

1 3 //This line is crooked. Can't determine where

5

7 9

1 3

5

7 9

******************************Program solved**********************************

Last problem solved by fixing main method

`System.out.print("Display odd values in a rectangular form \n ");`

into

`System.out.print("Display odd values in a rectangular form \n");`

The result is now:

`Display odd values in a rectangular form`

1 3

5

7 9

1 3

5

7 9

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

Print `"\n"`

every time when `(i + 1) % 3 == 0`

. The solution as I see:

```
int[] ints = IntStream.range(1, 10).toArray();
for (int i = 0; i < ints.length; i++)
System.out.print((ints[i] % 2 == 0 ? " " : ints[i]) +
((i + 1) % 3 == 0 ? "\n" : ""));
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**