Home > OS >  JAVA search the email ID entered by a user
JAVA search the email ID entered by a user

Time:02-18

I´m new in programming and have to do some homework but I´m stuck. I need to do this:

As a developer, write a program to search a string entered by a user from the array of strings.

Background of the problem statement: You have an array of email IDs of employees. As a programmer, write a program to search the email ID entered by a user.

I have my ArrayList but now I want to be able to enter an email address in the console & the program should check if the entered e-mail address is in my Array List. Can somebody please help me, I couldn't find anything like that (maybe also I don't know how to search for my answer) I´m happy for any help!

instead of searching for the element in the syntax like I did here, I want to have the system asking the user for the email ID and search my ArrayList for it. if the email ID is in the Array List the output should be: "email ID" searcElement "found" otherwise "email ID" searcElement "not found"

package searchElement;

import java.util.ArrayList;

public class searchElement {

    public static void main(String[] args) {
        
        ArrayList<String> emailID = new ArrayList<String>();
        
        emailID.add("[email protected]");
        emailID.add("[email protected]");
        emailID.add("[email protected]");
        emailID.add("[email protected]");
        emailID.add("[email protected]");
        emailID.add("[email protected]");
        emailID.add("[email protected]");
        
   
        String searcElement = "[email protected]";
        
                for(int i=0; i<emailID.size(); i  ) {
                    
                    System.out.println(emailID.get(i));
                    
                    if(searcElement==emailID.get(i)) {
                        
                        System.out.println("\n");
                        
                        System.out.println("email ID"   searcElement   "found");
                        
                        break;
                        
                    }
                }

    }

}

CodePudding user response:

Well, first of all you need to ask for the user to enter the string in the console. For that you need to use Scanner:

// Using Scanner for Getting Input from User
Scanner sc = new Scanner(System.sc);

// Get the input back for a string 
String stringInput = sc.nextLine();
System.out.println("You entered string "   stringInput);


// Get the input back if your emailId is an int 
int intInput = sc.nextInt();
System.out.println("You entered integer "   intInput);

Then, to scan your arrayList and output the result itself (not a boolean wheareas it found it or not) simply loop through your list and use the contains method:

for (String element : emailID){
   if (element.contains(stringInput)){
      System.out.println(element);
   }
}

If you're new in programming I'd advise you to check for those Scanner and contains stuff so you understand how it works and not just copy paste it without any understanding.

When you'll be a little more comfortable with programming, another method to reach that result would be to use Java Stream Filter with Lambda.

I hope that helped.

CodePudding user response:

java.util.Scanner is used to get Console input

 package searchElement;

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

public class searchElement {

    public static void main(String[] args) {
        
        ArrayList<String> emailID = new ArrayList<String>();
        
        emailID.add("[email protected]");
        emailID.add("[email protected]");
        emailID.add("[email protected]");
        emailID.add("[email protected]");
        emailID.add("[email protected]");
        emailID.add("[email protected]");
        emailID.add("[email protected]");
        
         // new code
         String searcElement = null;
         System.out.println("Enter the email to search");
         Scanner sc = new Scanner(System.in);
         searchElement = sc.NextLine();
           
           if (searchElement == null) {
                System.out.println("You haven't entered an email");
                return;
           }
                 for(int i=0; i<emailID.size(); i  ) {
                    
                    System.out.println(emailID.get(i));
                    
                    if(searcElement==emailID.get(i)) {
                        
                        System.out.println("\n");
                        
                        System.out.println("email ID"   searcElement   "found");
                        
                        break;
                        
                    }
                }

    }

}

CodePudding user response:

Use ArrayList.contains and a Scanner.

Scanner input = new Scanner(System.in);
System.out.println("Enter email: ");
String searcElement = input.nextLine(); //Should probably be "searchElement" right?"

if(emailID.contains(searcElement)){
    return "email ID"   searcElement   "found";
}
else{
    return "email ID"   searcElement   "not found";
}

CodePudding user response:

You have to use Scanner class to take input from user and remember you compare String data so, use equals() method for compare String because == operator sometime does not give right answer when compare String

String findOrNot = "";
Scanner sc = new Scanner(System.in);
String searcElement = sc.nextLine();
        
for(int i = 0; i < emailID.size(); i  ) {
    findOrNot = (searcElement.equals(emailID.get(i))) ? "email id "   searcElement   " found" : "email not found";
    break;
}
System.out.println(findOrNot);

You can compare data easily using arraylist because it has inbuilt contains() method for check value are inside list or not.

String findOrNot = "";
Scanner sc = new Scanner(System.in);
String searcElement = sc.nextLine();

findOrNot = (emailID.contains(searcElement)) ? "email id "   searcElement   " found" : "email not found";
System.out.println(findOrNot);
  • Related