我创建了一个ArrayList来存储客户的名字。然后我在列表中填写了一些名字。
现在我想要求用户输入一个字母,然后找到 以输入的字母开头的所有名称 和 包含输入字母的所有名称。
这就是我现在所处的位置:
package costomersearching;
import java.util.ArrayList;
import java.util.Scanner;
public class CostomerSearching {
public static void main(String[] args) {
ArrayList<String> customerName = new ArrayList();
Scanner input = new Scanner(System.in);
customerName.add("Sara");
customerName.add("John");
customerName.add("Miami");
customerName.add("Mart");
customerName.add("Alex");
System.out.println("Customer List: \n" + customerName);
System.out.println("Search Customer by letter: ");
String letter = input.next();
//show the name containg the letter starting as the first letter
//Show the name containing the letetr.
}
}
答案 0 :(得分:1)
您要搜索的方法是startsWith(String)并包含String类的(CharSequence)。 此外,这样它也可以使用多个字母。
只需遍历您的客户列表并检查名称。 只要找到匹配的产品,就可以将其添加到之后要打印给用户的客户列表中。
String searchterm = "s"; // You read the string from console
// Existing customers
ArrayList<String> customerNames = new ArrayList<String>();
// A list of customer names starting with the search term
ArrayList<String> matchesStarting = new ArrayList<String>();
// A list of customer names containing the search term
ArrayList<String> matchesContaining = new ArrayList<String>();
// Iterate over customers and check for each one if it matches the search term
for(String customer: customerNames) {
// If it starts with the search term, add it to the list of start matches
if(customer.startsWith(searchterm))
matchesStarting.add(customer);
// If it contains the search term, add it to the list of start matches
if(customer.contains(searchterm))
matchesContaining.add(customer);
}
答案 1 :(得分:1)
简单地遍历您的ArrayList并搜索正确的名称。你可以这样做:
//show names containing the letter starting as the first letter
for(String i : costumerName) {
if(i.startsWith(letter)) System.out.println(i);
}
//show names containing the letter
for(String i : costumerName) {
if(i.contains(letter)) System.out.println(i);
}
希望这会有所帮助:)
答案 2 :(得分:0)
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class TestStack {
public static void main(String[] args) {
Scanner input = null ;
try{
ArrayList<String> customerName = new ArrayList<>();
input = new Scanner(System.in);
customerName.add("Sara");
customerName.add("John");
customerName.add("Miami");
customerName.add("Mart");
customerName.add("Alex");
System.out.println("Customer List: \n" + customerName);
System.out.println("Search Customer by letter: ");
String letter = input.next();
List<String> searchResult = new ArrayList<>();
for (String string : customerName) {
if(string.contains(letter))
searchResult.add(string);
}
List<String> searchResultStartsWithSpecifiedLetters = new ArrayList<>();
for (String string : customerName) {
if(string.startsWith(letter))
searchResult.add(string);
}
System.out.println("Displaying result containing entered letters");
// Displays result containing those letters
for (String string : searchResult) {
System.out.println(string);
}
System.out.println("Displaying result starts with entered letters");
// Displays result starts with those letters
for (String string : searchResultStartsWithSpecifiedLetters) {
System.out.println(string);
}
}finally{
input.close();
}
}
}