1

I am trying to write a program that creates an array and fill it with int numbers(first method). In the end, it is supposed to see if a specific number is given in the array(second method). The problem is that the program does not run my if loops. I do not know why. The variable x is the number the program is looking for in the array and pos the position of the number in the array

public class Program {

    static int [] numbers= new int[100];
    
    public static void main(String [] args) {

        PrintWriter out = new PrintWriter(System.out);

        arrayConstruction();
        test(out);
        out.flush();
    }

    public static void arrayConstruction() {
        int x = 0;
        for (int i = 0; i < numbers.length; i++) {
            numbers[i] = x;
            x++;
        }
    }

    public static void test(PrintWriter out) {
        int x = 17;
        int pos = 0;
        if(pos != numbers.length) {
            if(numbers[pos] == x) {
                out.println("The number was found!");
                out.flush();
            }
            
            pos++;
        }
        
        else if(pos == numbers.length) {
            out.println("The number does not exist!");
            out.flush();
        }
    }
}
1
  • 2
    I think your are looking for a while loop as an "if loop" is not a thing. Commented Jan 19, 2022 at 22:30

2 Answers 2

2

You forgot to add a loop to the test method, so it checks the first array's item only. E.g. you can use while loop.

public static void test(PrintWriter out) {
    int x = 17;
    int pos = 0;

    while (true) {
        if (pos != numbers.length) {
            if (numbers[pos] == x) {
                out.println("The number was found!");
                return;
            }

            pos++;
        } else if (pos == numbers.length) {
            out.println("The number does not exist!");
            return;
        }
    }
}

I think you should redesign your code by splitting different activities with separate methods. It makes your code clear to understand.

public class Main {

    public static void main(String[] args) {
        int[] arr = createArray(100);
        System.out.println(isNumberExist(arr, 17) ? "The number was found!"
                                                  : "The number does not exist!");
    }

    public static int[] createArray(int total) {
        int[] arr = new int[total];
        Random random = new Random();

        for (int i = 0; i < arr.length; i++)
            arr[i] = random.nextInt(arr.length);

        return arr;
    }

    public static boolean isNumberExist(int[] arr, int x) {
        for (int i = 0; i < arr.length; i++)
            if (arr[i] == x)
                return true;

        return false;
    }

}
Sign up to request clarification or add additional context in comments.

2 Comments

Why not while (pos < numbers.length) or even just a for-loop which would seem to be a overall simpler construct for the problem (just saying ;))
Yep, there are different options.
0

You should add a while loop to your test method, like this:

public static void test(PrintWriter out) {
    int x = 17;
    int pos = 0;
    while(pos < numbers.length) {
        if(numbers[pos] == x) {
            out.println("The number was found!");
            out.flush();
            break;
        }
        
        pos++;
    }
    
    if(pos == numbers.length) {
        out.println("The number does not exist!");
        out.flush();
    }
}

In your method, the if statement will only be executed once.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.