Laura Sutardja
Laura Sutardja

Reputation: 25

Searching in arraylist<String[]> from user input

I want to search in an arraylist from a user input but my if condition doesn't seem to work. Using boolean and .contains() doesn't work for my programme either. This is the coding:

String phone;
phone=this.text1.getText();
System.out.println("this is the phone: " + phone);

BufferedReader line = new BufferedReader(new FileReader(new File("C:\\Users\\Laura Sutardja\\Documents\\IB DP\\Computer Science HL\\cs\\data.txt")));
    String indata;

    ArrayList<String[]> dataArr = new ArrayList<String[]>(); 

    while ((indata = line.readLine()) != null) { 

        String[] club = new String[2]; 
        String[] value = indata.split(",", 2);  
        //for (int i = 0; i < 2; i++) { 

            int n = Math.min(value.length, club.length);
              for (int i = 0; i < n; i++) { 
               club[i] = value[i]; 
        }
          boolean aa = dataArr.contains(this.text1.getText());

          if(aa==true)
          text2.setText("The data is found.");
          else
          text2.setText("The data is not found.");

        dataArr.add(club);
    }


    for (int i = 0; i < dataArr.size(); i++) {
        for (int x = 0; x < dataArr.get(i).length; x++) {
            System.out.printf("dataArr[%d][%d]: ", i, x);
            System.out.println(dataArr.get(i)[x]);
        }
    }


 }
 catch ( IOException iox )
{
  System.out.println("Error");
}

Upvotes: 0

Views: 1624

Answers (2)

WonderWorld
WonderWorld

Reputation: 966

There is a lot wrong with the program. I assume you want to read a textfile and store each line in the arraylist. To do this you have to split each line of the textfile and store that array in the arrayList.

String[] value;
while ((indata = line.readLine()) != null) { 
    value = indata.split(",");
    dataArr.add(value);
}

Now you have the contents of the file in the arrayList. Next you want to compare the userinput with each element of the arraylist.

int j = 0;
for (int i = 0; i < dataArr.size(); i++) {
    String[] phoneData = dataArr.get(i);
    if (phoneData[1].equals(phone)) { // i am assuming here that the phone number is the 2nd element of the String[] array, since i dont know how the textfile looks.
        System.out.println("Found number.");
        club[j++] = phoneData[1];
    } else if (i == dataArr.size()-1) {
        System.out.println("Didn't find number.");
    }
}

Edit: As requested:

    String phone;
    phone = "38495";
    System.out.println("this is the phone: " + phone);

    BufferedReader line = new BufferedReader(new FileReader(new File("list.txt")));
    String indata;

    ArrayList<String[]> dataArr = new ArrayList<>();
    String[] club = new String[2];
    String[] value;// = indata.split(",", 2);
    while ((indata = line.readLine()) != null) {
        value = indata.split(",");
        dataArr.add(value);
    }
    int j = 0;
    for (int i = 0; i < dataArr.size(); i++) {
        String[] phoneData = dataArr.get(i);
        if (phoneData[1].equals(phone)) {
            System.out.println("Found number.");
            club[j++] = phoneData[1];
            break;
        } else if (i == dataArr.size()-1) {
            System.out.println("Didn't find number.");
        }
    }

I hope this makes sense now.

Upvotes: 0

davide
davide

Reputation: 1948

Your dataArr is a list of String[], and you are searching for a String. The two are different kind of objects.

I don't really know how the content of the club array looks like, but you should either change dataArr in order to hold plain String, or to write a method which looks iteratively in dataArr for a String[] containing the output of this.text1.getText().

Upvotes: 2

Related Questions