[英]How to find the names starting with the letter entered by user?
I have created an ArrayList to store names of customers. 我创建了一个ArrayList来存储客户的名字。 I then have filled the list with some names. 然后我在列表中填写了一些名字。
Now I want to ask the user to enter a letter and then find all names starting with the entered letter and all names containing the entered letter. 现在,我想要求用户输入一个字母,然后查找以输入的字母开头的所有名称以及包含输入字母的所有名称。
That's where I am upto now: 这就是我现在所处的位置:
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.
}
}
Simple iterate over your ArrayList and search for the right names. 简单地遍历您的ArrayList并搜索正确的名称。 You could do it like this: 你可以这样做:
//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);
}
Hope this helps :) 希望这可以帮助 :)
The methods you are searching for are startsWith(String) and contains(CharSequence) of class String. 您要搜索的方法是startsWith(String)并包含String类的(CharSequence)。 In addition, that way it will also work with more than one letter. 此外,这样它也可以使用多个字母。
Just loop through you list of customers and check the names. 只需遍历您的客户列表并检查名称。 As soon as you find a matching one, you add it to your list of customers you want to print to the user afterwards. 只要找到匹配的一个,就可以将其添加到之后要打印给用户的客户列表中。
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);
}
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();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.