0

i am newbee in java and trying to remove elements from list array. Have been tried many variants, but always get input error and nothing been deleted from list. Have tried take make condition like: if (list.contains(tch.getSurname()) or something like this, always get error that input error. Hope, you will help me to resolve this problem.

package com.company;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

    static Main main = new Main();
    /*public static Teachers tch = new Teachers(surname, name);*/
    public static List<Teachers> list = new ArrayList<Teachers>();

    public static void io(){
        Scanner sc = new Scanner(System.in);
        String surname = "";
        String name = "";
        Teachers tch = new Teachers(surname, name);
        for (int i=0; i<2; i++) {
            surname = sc.nextLine();
            name = sc.nextLine();
            tch = new Teachers(surname, name);
            list.add(tch);
        }
        for(Teachers nstr : list) {
            System.out.println(nstr.toString());
        }

        for(Teachers t : list) {
            String input = sc.nextLine();
            if (input == tch.getSurname()) {
                list.remove(input);
            } else {
                System.out.println("Wrong input");
            }

        }

        for(Teachers nstr : list) {
            System.out.println(nstr.toString());
        }
    }

    public static void main(String[] args) {
        main.io();


    }
}

UPDATE: So i'm tried to use iterator, i've added:

for (Iterator<Teachers> it = list.iterator(); it.hasNext();){
            Teachers t = it.next();
            if (t.equals(tch.getSurname())){
                it.remove();
            }
        }

and delete:

for(Teachers t : list) {
            String input = sc.nextLine();
            if (input.equals(tch.getSurname())) {
                list.remove(tch);
            } else {
                System.out.println("Wrong input");
            }
        }

But it also didn't help me to delete element from list array, it just duplicate my input for list.

semper fi
  • 727
  • 3
  • 17
  • 32
  • 1
    What is the exact error and where does it occur? – tnw Mar 13 '15 at 15:42
  • 1
    You have to use a `Iterator` to remove items while looping over them. – Murat Karagöz Mar 13 '15 at 15:42
  • 1
    You should read http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java to *start* with - and it looks to me like you're calling `nextLine` rather more often than you intend to... – Jon Skeet Mar 13 '15 at 15:43
  • Don't compare Strings using `==` or `!=`. Use the `equals(...)` or the `equalsIgnoreCase(...)` method instead. Understand that `==` checks if the two *object references* are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. – Hovercraft Full Of Eels Mar 13 '15 at 15:54
  • tnw, it extract sout "Wrong input", and occur in loop `for(Teachers t : list)` – semper fi Mar 13 '15 at 16:02

2 Answers2

1

Your error is if(input == tch.getSurname(). This compares the exact reference to see if it is the same. Instead, use if(input.equals(tch.getSurname())) to check the contents. Also change the reference in that loop from tch to t. You are not using the current element in the list rather the one you created to add to the list. Finally change list.remove(input) to list.remove(t). This way the actual element is being removed, no just trying to remove the surname string.

Change your for loop to

Iterator<Teachers> i = list.iterator();
while(i.hasNext()){
     Teachers t = i.next();
      ...
}

Then to remove it just use i.remove()

TameHog
  • 950
  • 11
  • 23
0

Now loop works perfectly.

for(Teachers t : list) {
                String input = sc.nextLine();
                if (input.equals(t.getSurname())) {
                    list.remove(t);
                } else {
                    System.out.println("Wrong input");
                }
            }
semper fi
  • 727
  • 3
  • 17
  • 32