0

So I refactored a Linear Search code that only uses the main method. My goal is to convert it into an OOP approach. But I have trouble saving the input set of integers.

// LinearSearchDriver.java
import java.util.Scanner;

public class LinearSearchDriver {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        LinearSearch linearSearch = new LinearSearch();
        System.out.println("Enter number of elements");
        int numElements = in.nextInt();
        linearSearch.setNumberOfElements(numElements);

        System.out.println("Enter " + numElements + " integers");

        for (int count = 0; count < numElements; count++){
            int setIntegers = in.nextInt();
            linearSearch.setNumberOfIntegers(setIntegers);
        }
        
        System.out.println("Enter value to find");
        int search = in.nextInt();
        linearSearch.findValue(search);

    }
}

//LinearSearch.java
public class LinearSearch {
    private int c;
    private int n;
    private int array[];

    public void setNumberOfElements(int n) {
        this.n = n;
        this.array = new int[n];
    }

    public void setNumberOfIntegers(int y) {
        for (c=0; c < n; c++)
            array[c] = y;
    }

    public void findValue(int search) {
        for (c = 0; c < n; c++) {
            if (array[c] == search) {    /* Searching element is present */
                System.out.println(search + " is present at location " + (c + 1) + ".");
                break;
            }
        }

        if (c == n) { /* Searching element is absent */
            System.out.println(search + " is not present in array.");
        }
    }
}

Example output:

enter image description here

But when I input number 1, this is the output:

enter image description here

The program only reads number 2 output which I think, the last number is only the one that is saving to an array.

HoRn
  • 1,458
  • 5
  • 20
  • 25
peaky
  • 1
  • 1
  • 1
    [Why should I not upload images of code/data/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors-when-asking-a-question) – user16320675 Oct 03 '22 at 07:04

1 Answers1

1
for (c = 0; c < n; c++) {
    array[c] = y;
}

is where things go wrong. You're setting the last value passed to that function for every index in the Array.

You can adress this in several ways:

  1. Pass an Array to the function instead of a single argument.
  2. You can determine the current number of elements in your Array and then append the newest value "manually". See this post.
  3. Or you could simply use a dynamic structure, such as a List and append the element to that.

Here is a rough outline for the 3rd Option:

public class LinearSearch {
    private List<Integer> intList;

    public LinearSearch() {
    }

    public void setNumberOfElements(int n) {
        intList = new ArrayList<>(n); //Set the capacity here like before.
    }

    public void setNumberOfIntegers(int y) {
        //If you want your List to always only contain the initially allowed number of elements, you could implement
        // this logic here, by adding the new value and removing the "oldest" one.
        intList.add(y);
    }

    public void findValue(int search) {
        if (!intList.contains(search)) { //You can put this up here and potentially skip the looping.
            System.out.println(search + " is not present in array.");
            return;
        }
        for (int n : intList) {
            if (n == search) {
                System.out.println(search + " is present at location " + intList.indexOf(search) + ".");
                return; //Use return to exit the method, break only exits the loop in your example, and you could print both lines.
            }
        }
    }
}
Vinz
  • 483
  • 1
  • 10
  • Yes I also think that, that method is wrong. Okay I'll try. Thank you – peaky Oct 03 '22 at 06:22
  • 1
    Thank you so much! I was trying the number 3 option. And thank you for the brief instruction and comment. 'Will practice this approach! – peaky Oct 03 '22 at 06:35