0

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");
}
1
  • Please describe in detail how it doesn't work. What errors do you see (show the logs)? Commented Mar 1, 2015 at 12:29

2 Answers 2

2

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().

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

Comments

0

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.

15 Comments

the data in my text file is separated by a comma. I want each data to be stored separately.
Ok possible too, let me adjust my answer.
@LauraSutardja I am not saying my solution fixes all of the issues, but at least you can compare all the entries in the textfile with the userinput.
i am still confused as where to put the changes. can u place your solutions in the coding i posted?
@LauraSutardja i will update my question with all of your code.
|

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.