-3

I am making an ArrayList of cars and I am having trouble how to iterate through the ArrayList and print what I ask using a scanner. Example: I am looking for an Audi in the list, I want it to look through the ArrayList and if it is in the ArrayList print "We have a %s."

This is what I have so far: public class Main {

public static void main(String[] args) {
    ArrayList<String> car = new ArrayList<>();
    car.add("Audi");
    car.add("Chevrolet");
    car.add("Dodge");
    car.add("Ford");
    car.add("Honda");
    car.add("Toyota");
    car.add("Volkswagen");

    Scanner sc = new Scanner(System.in);
    String str = sc.nextLine();

    for(String name: car)
    {
        if(name == sc){
            System.out.printf("We have a %s.", sc);
        }
    }
}    

}

AR_16
  • 3
  • 1
  • 1
    Does this answer your question? [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Ivar Nov 29 '19 at 23:20
  • 3
    A string cannot be equal to a scanner. It can be equal to the string that you read with the scanner though. And hint: List has a contains() method. – JB Nizet Nov 29 '19 at 23:20
  • 3
    `if(name == sc){`, No, you're trying to equate a String with a Scanner object, so that doesn't make logical or coding sense. You use the Scanner to extract Strings from a file or the user and then do things with the String. – Hovercraft Full Of Eels Nov 29 '19 at 23:21
  • Thank you all for your help. I am new to java lol – AR_16 Dec 01 '19 at 21:06

1 Answers1

1

I think that real problem here1 is that you don't have a clear understanding of what a Scanner does.

A Scanner reads characters from a stream (for example System.in) and turns them into various kinds of values; e.g. integers, floating point numbers, strings and so on. The basic model is:

  • call hasNextXXX to test if there is a XXX to read.
  • call nextXXX to read a XXX.

So you are trying to get a name of a car manufacturer from the Scanner. Assuming that car manufacturer names don't have spaces in them, what you are reading is a white-space delimited token. The method for reading a token is Scanner::next. It returns the token as a String with any leading or trailing whitespace removed.

Aside: String::nextLine would also work, except that it returns the complete line with all white space entered by the user before or after the name2. If the user enters (for example) Ford with a space after it, then that won't match the value in your car list. To deal with that, you would need to do something like this:

    String str = sc.nextLine().trim();  // 'trim' removes leading and 
                                        // trailing whitespace; e.g. SP,
                                        // TAB, CR and NL characters.

Once you have the name as a String, you should String::equals to compare it against other strings. Comparing strings using == is incorrect in nearly all circumstances; see How do I compare strings in Java?

For a deeper understanding of Scanner, I recommend that you take the time to read the javadocs.


Your code doesn't do the above. Instead, it reads a line (i.e. str = sc.nextLine()) and doesn't use it. Then it uses == to test if the Scanner is equal to each String in your list. That fails, because a Scanner is not a String.

Aside: in Java, == for a reference type (i.e. for objects) means "is this the same object".


1 - The other possibility is that you didn't read the code that you had written carefully enough.

2 - ... apart from the line separator sequence; i.e. CR, NL or CR + NL. This is removed automatically by nextLine: refer to the javadocs for more details.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216