我是編程新手,必須做一些功課,但我被困住了。我需要這樣做:
作為開發人員,撰寫一個程式來從字串陣列中搜索用戶輸入的字串。
問題陳述的背景: 您有一組員工的電子郵件 ID。作為程式員,撰寫一個程式來搜索用戶輸入的電子郵件 ID。
我有我的 ArrayList,但現在我希望能夠在控制臺中輸入電子郵件地址,并且程式應該檢查輸入的電子郵件地址是否在我的 Array List 中。有人可以幫助我嗎,我找不到類似的東西(也許我也不知道如何搜索我的答案)我很高興得到任何幫助!
而不是像我在這里那樣在語法中搜索元素,我想讓系統向用戶詢問電子郵件 ID 并在我的 ArrayList 中搜索它。如果電子郵件 ID 在陣列串列中,則輸出應為:“電子郵件 ID” searcElement “找到”,否則為“電子郵件 ID” searcElement “未找到”
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;
}
}
}
}
uj5u.com熱心網友回復:
好吧,首先您需要要求用戶在控制臺中輸入字串。為此,您需要使用掃描儀:
// 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);
然后,要掃描您的 arrayList 并輸出結果本身(不是布林值,因為它是否找到它)只需遍歷您的串列并使用 contains 方法:
for (String element : emailID){
if (element.contains(stringInput)){
System.out.println(element);
}
}
如果您是編程新手,我建議您檢查那些掃描儀并包含一些東西,以便您了解它是如何作業的,而不是在沒有任何理解的情況下復制粘貼它。
當您對編程更加熟悉時,另一種達到該結果的方法是使用帶有 Lambda 的 Java Stream Filter。
我希望這有幫助。
uj5u.com熱心網友回復:
java.util.Scanner 用于獲取控制臺輸入
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;
}
}
}
}
uj5u.com熱心網友回復:
使用ArrayList.contains和 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";
}
uj5u.com熱心網友回復:
您必須使用Scanner類從用戶那里獲取輸入并記住您比較字串資料,所以使用比較字串equals()的方法,因為操作員有時在比較字串時不會給出正確的答案==
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);
您可以使用 arraylist 輕松比較資料,因為它具有contains()檢查值是否在串列中的內置方法。
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);
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/426018.html
上一篇:如何區分數字和字母或符號?
