简体   繁体   English

如何找到以用户输入的字母开头的名称?

[英]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.

相关问题 如何保存和读取用户输入的所有名称? - How to save and read all names user entered? 如何检查用户输入的字符串在Java中是否是某个字母? - How to check if a string entered by a user is a certain letter in Java? 当字母是第一个输入时,如何阻止用户在开关盒中输入字母 - How to stop a user from entering a letter in a switch case when the letter is the first input entered 试图在用户输入的句子中找到所有四个字母的单词并替换它们,但代码不会做任何事情 - Trying to find all four letter words in a sentence entered by user and replace them but code won't do anything 如何找到短语的第 n 个字母,并保留所有起始字母? - How to find the nth letter of the phrase, and keep all the starting letters? 我如何告诉用户他们以前输入了带有indexOf的字母? JAVA - How do I tell user that they've previously entered a letter with indexOf? JAVA 如何在Java中查找用户输入的字符串是否为基本字符串文字? - How to find user entered string is base String literal or not in java? 如何找到用户输入的两个输入的通用后缀? - How can I find common suffix of two inputs entered by the user? JSON没有异常值此外,如果用户输入了列名,该如何检索值 - JSON No Value For Exception Also how to retrieve values if column names are entered by user 如何生成以Java中的字母开头的随机字符串 - How to generate a random string starting with a letter in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM