1

I have an array which is filled with inputs from the user and I need to print it. I tried but I get weird results. First im going to enter how many elements I want to input then I will enter the elements, then im going to print them.

For example: I entered 5 10 23 77 105 And I need to print it like this: Elements: 5 10 23 77 105

And also "INVALID INPUT" is in the false statement but it mixes in the true statement.

Here is what I tried so far:

import java.util.*;
import java.util.ArrayList;

public class LinearSearch {

    public static void main(String[] args) {
        int choice;
        Scanner in = new Scanner(System.in);
        System.out.println("Choose between INT(1) --- DOUBLE(2) --- STRING(3) --:");
        choice = in.nextInt();

        if(choice == 1) {
            choice(args);
        }
    }

    public static void choice(String[] args) {
        int num;
        int repeat;
        Scanner in = new Scanner(System.in);

        System.out.println("Enter the number of elements in the array");
        num = in.nextInt();

        int array[] = new int [num];

        for (int i = 0; i < array.length;  i++) {
            System.out.print("Enter the number at array index"+i+": ");
            array[i] = in.nextInt();
        }

        System.out.print("Do you want to search if the number is present in the array? YES = 1 , No = 2: ");
        int answer = in.nextInt();

        if (answer == 1) {
            Linear(array);
        }

        if (answer == 2) {
            System.out.println("Thank you for using the program");
        }   

        if (answer >= 3) {
            System.out.println("Invalid Input");
        }

        System.out.print("\nDo you want to repeat the program? Yes = 1 --- No = 2: --- ");
        repeat = in.nextInt();

        if(repeat == 1) {   
            main(args);

        }

        if (repeat == 2) {
            System.out.println("Thank you again ! ! !");
            System.out.println("Thank you again ! ! !");
            System.out.println("Thank you again ! ! !");
            System.out.println("Thank you again ! ! !");
            System.out.println("Thank you again ! ! !");
            System.out.println("Thank you again ! ! !");
            System.out.println("Thank you again ! ! !");

        }
    }

    public static void Linear(int array[]) {
        int key;
        Scanner in = new Scanner(System.in);

        System.out.print("Enter a number that you want to search: ");
        key = in.nextInt();

        for(int i = 0; i < array.length; i++) {
            if (array[i] == key) {
                System.out.println("Elements: " + array);
                System.out.println("Element " + key + " is found at index: " + i);
            } else {
                System.out.println("INVALID INPUT");
            }
        }
    }
}
Neuron
  • 5,141
  • 5
  • 38
  • 59

1 Answers1

2

You shouldn't call an Array a List. This is very confusing. For example, to print an ArrayList, your code would be working as intended.

But because you are using an array, instead of

System.out.println("Elements: " + array);

use

System.out.println("Elements: " + Arrays.toString(array));
Jörn Buitink
  • 2,906
  • 2
  • 22
  • 33