Reconize an Array method

Can you help me on something where I can't seem to get my code right.

It contains two flaws. I can't get it to read the Array so it compares the input. And it doesn't add a number when it is reconized.

public class Bird {

    private ArrayList<Vogels> Name;
    private final Scanner scanner;
    private int observed;
    private String R;


    public Bird (Scanner scanner) {
        Name = new ArrayList<Vogels>();
        this.scanner = scanner;
        this.observed = 0;
        this.R = "";
    }

The following method I can't seem to get it to work. I know the problem is somewhere in reading the Array, but I can't seem to get it to read it correctly.

public void Obs(){
        System.out.print("What was Observed:?");
        R = scanner.nextLine();
        if (!Name.equals(R)){
            System.out.println("Is not a bird!");
        }else{
            System.out.println("added");
            this.observed++;
        }       
}

Upvotes: 0

Views: 55

Answers (3)

sauumum
sauumum

Reputation: 1788

After looking into your codes, it looks like you want to check whether list contain the data read from file or not. If this is what you want, you can update your code as below:

 public void Obs(){
            System.out.print("What was Observed:?");
//Improve your reading from scanner
            R = scanner.nextLine();
            if (!Name.contains(Vogels.getVogels(R))){
                System.out.println("Is not a bird!");
            }else{
                System.out.println("added");
                this.observed++;
            }       
    }

Where getVogel is a method which will return Vogels based on the user provided string.

Upvotes: 0

Name is an ArrayList of Vogels and R is a String..

so this here if (!Name.equals(R)){ makes not much sense, and will return always false

you need or should implement/define something like if(Name.contains(Vogels.resolve(R))) where Vogels.resolve(R) is a static method that returns a Vogel when you give as parameter a String

Upvotes: 1

Shriram
Shriram

Reputation: 4411

You are trying to compare a string with array

if (!Name.equals(R)){

you have to get the object out of array first and get the value of the string from an object and then you have to compare. Name.get(0) -> which gives an object Vogels(you have to write a logic either for loop or some other mechanism). You have to get an value out of object and then compare with string. Better post your Vogels object.

Upvotes: 0

Related Questions