2014-12-06 2 views
1

고객의 이름을 저장하기 위해 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

당신이 일을 루프 방법을 알고 있나요? –

+0

배열을 반복하고 각 값에 대해'name.startsWith (letter)'와'name.contains (letter)'를 수행하십시오. –

답변

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); 
} 

희망이 도움이 :)

+0

'toCharArray()'를 할 때마다 새로운 객체를 생성하게됩니다. 대신 'i.charAt (0)'(문자열의 첫 번째 색인에서 문자를 반환) 또는'i.startsWith (letter)'를 사용하여 문자열의 첫 번째 색인을 검사 할 수 있습니다. 각 이름에 대한 추가 개체가 필요하지 않습니다. –

+0

TrezzJo, 도움이되었고 개념을 얻었습니다. – Simon

+0

감사합니다 :) 또한 빈스로부터의 피드백 – TrezzJo

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); 
} 
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(); 
     } 
    } 
} 
관련 문제