0

i have created an array named as brand and also created an if else statement. I want whenever a user put the value from array, the if else statement will print the statement i have witten below, my code is given below

import java.util.Scanner;

public class test {
public static void main(String[] args) {

// TODO Auto-generated method stub
    
   Scanner sc = new Scanner (System.in);
   String[] brand = {"phone","mobile","cell"};
   System.out.print("find me some ");
   String product = sc.next();
   if (product == brand) {
       System.out.println("please enter your brand, Phone brands available : Apple, Samsung");
   }
    }
}

please help me out in the code given below

  • You want to check that the input from user is in the array. https://stackoverflow.com/questions/1128723/how-do-i-determine-whether-an-array-contains-a-particular-value-in-java – Tim Sep 02 '22 at 16:47
  • @Naman Aadhar: Strings are not checked with ==. You need to use the .equal function like in `if(brand[0].equals(product)) {`. And you need to do it with every index, thus, you need to place this in a `for(String s:brand) if(s.equals(product))`. Also... brand doesn't contain "Apple" and "Samsung"! You should reallly check what you need do do. – Luca Scarcia Sep 02 '22 at 16:58

2 Answers2

0

If I didn't misunderstood you, you want to check if the array contains the user input. If that's correct, you could do the following:

import java.util.Scanner;

public class test {
public static void main(String[] args) {

// TODO Auto-generated method stub
    
   Scanner sc = new Scanner (System.in);
   String[] brand = {"phone","mobile","cell"};
   System.out.print("find me some ");
   String product = sc.next();
   if (Arrays.asList(brand).contains(product)) {
       System.out.println("please enter your brand, Phone brands available : Apple, Samsung");
      }
   }
}

You could also use a for loop:

// Omitted code
   for (String s in brand){
       if (product.equals(s)){
          System.out.println("please enter your brand, Phone brands 
          available : Apple, Samsung");
       }
    }
Carlos
  • 119
  • 10
0

The following line in your code is wrong.

if (product == brand) {
    System.out.println("please enter your brand, Phone brands available : Apple, Samsung");
}

You are trying to compare a string (product) with an Array of strings(brand). This code will not compile, and will error out stating java: incomparable types: java.lang.String and java.lang.String[]

Therefore, you must iterate over your brand array, and see if any of the elements in your brand array matches the given product. So change your main method as:

    public static void main(String[] args) {
        Scanner sc = new Scanner (System.in);
        String[] brand = {"phone","mobile","cell"};
        System.out.print("find me some ");
        String product = sc.next();
        for(String b: brand) {
            if(b.equals(product)) {
                System.out.println("please enter your brand, Phone brands available : Apple, Samsung");
            }
        }
    }

NOTE: When comparing strings use equals method. If you mistakenly use == then it will check if both strings being compared point to the same reference.

Wasim Zarin
  • 166
  • 3