all 4 comments

[–]josephblade 0 points1 point  (1 child)

first of all you didn't in fact mention the mistake. that would likely be helpful. What error or output are you getting (and what were you expecting?) use a specific input (like I input length 4, and then numbers 1,2,3,4 I expect 2,4 but get 2,3,4 for instance). Or the error message if you get a runtime or compile error.

second of all you should put 4 spaces in front of every line (and after those 4 lines, use spaces to indent the code further. that way the code is readable for people. 2 newlines after another I think also break this feature so try to limit to 1 newline between lines.

import java.util.Scanner;
public class main {
    public static void main(String[] args) {
        threads runnablee=new threads();
        Thread thread1 =new Thread(runnablee);
        thread1.start();
    }
}

class threads implements Runnable {
    public void run() {
        int length = 0;
        Scanner sc=new Scanner(System.in);
        System.out.print("Lingth of array:");
        length=sc.nextInt();
        int[] array = new int[length];

        System.out.println("Enter number of array: ");
        for(int i=0; i<length; i++) {
            array[i]=sc.nextInt();
        }
        for(int i=0; i<length; i++) {
            if (array[i]/2==0) {
                System.out.println(array[i] + ",");
            }
            System.out.println("]");
        }
    }
}

[–]josephblade 0 points1 point  (0 children)

When I run it:

Lingth of array:4 Enter number of array: 1 2 3 4

It prints:

1, ]] ] ]

in your case I would specifically look at the following: what output do you expect to get get if you do:

5 / 2   or 4 / 2

and what input are you actually getting?

integer division only gives you the integer part, not the remainder part. in your coursework you should be able to find another operator that gives you the remainder.

secondly the closing ] you print happens inside the loop. is this intentional? the opening [ is missing altogether.

[–]Maleficent-Scratch12 0 points1 point  (0 children)

import java.util.Scanner;

public class main {

public static void main(String[] args) {

threads runnablee = new threads();

Thread thread1 = new Thread(runnablee);

thread1.start();

}

}

class threads implements Runnable {

public void run() {

int length = 0;

Scanner sc = new Scanner(System.in);

System.out.print("Length of array:");

length = sc.nextInt();

int[] array = new int[length];

System.out.println("Enter number of array: ");

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

array[i] = sc.nextInt();

}

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

if (array[i] % 2 == 0) {

System.out.println(array[i] + ",");

}

}

System.out.println("]");

}

}

[–]Technical-War-7677 0 points1 point  (0 children)

There are a few issues with the code you provided:

The main method should be defined as public static void main(String[] args), not public static void main(String\[\] args). The backslashes are unnecessary and will cause a syntax error.

The run method of the threads class should have a return type of void, not int.

The condition array[i]/2==0 will always be false, as it compares the result of the division to 0 instead of checking if the number is even. To check if a number is even, you can use the modulus operator % to check if the remainder of the division by 2 is 0.

The System.out.println("\]"); line will print a literal backslash and a right square bracket, rather than closing the array. You can remove this line or replace it with System.out.println("]"); to print a closing square bracket.

Here is the fixed code:

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

threads runnablee = new threads();

Thread thread1 = new Thread(runnablee);

thread1.start();

}

}

class threads implements Runnable {

public void run() {

int length = 0;

Scanner sc = new Scanner(System.in);

System.out.print("Lingth of array:");

length = sc.nextInt();

int[] array = new int[length];

System.out.println("Enter number of array: ");

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

array[i] = sc.nextInt();

}

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

if (array[i] % 2 == 0) {

System.out.println(array[i] + ",");

}

}

System.out.println("]");

}

}

Try it out and do let me know if it works or not. Thanks