2014-10-31 96 views
0

当我提示用户写两个单词时,如何打印哪个单词首先按字母顺序排列?以及如何检查扫描字中是否存在某个字符?如何在字符串中查找字母以及如何按字母顺序返回两个字符串? java

例如:如果用户写了“Word”和“Apple”,我怎样才能按字母顺序打印这两个单词。另外,我编写了一个程序来检查字符“z”是否出现在字中,但我不知道它有什么问题?这里是我的程序:

import java.util.*; 
public class Pr7{ 
    public static void main(String[] args){ 

    //print two words and read them.. 

    Scanner scan = new Scanner (System.in);  
    String Word1; 
    String Word2; 

    System.out.println(); 

    System.out.print("* Please write one word: "); 
     Word1 = scan.nextLine(); 
    System.out.print("* Please write one word: "); 
     Word2 = scan.nextLine(); 

    //Prints which word has more characters in it.. 

    if (Word1.length() > Word2.length()) 
     System.out.println("- " + "(" + Word1 + ")" + " has more characters."); 

    else if (Word2.length() > Word1.length()) 
     System.out.println("- " + "(" + Word2 + ")" + " has more characters."); 
    else 
     System.out.println("- " + "(" + Word1 + ")" + " has equal characters with " + "(" + Word2 + ")"); 

    //Prints which word comes first (alphabetically).. 
/*  
    char ch; 
    int compare = Word1.compareTO(Word2); 
*/ 

    //Prints whether the letter 'z' appears in either word.. 

    if (Word1.indexOf('z') == true) 
    System.out.print("- Letter 'z' appears in the first word."); 
    else if (Word2.indexOf('z') ==) 
    System.out.print("- Letter 'z' appears in the second word."); 
    else 
    System.out.print("- Letter 'z' doesn't appears in either word."); 

    System.out.println(); 

    //Prompts the user for a sentence and reads it.. 

    String Str1; 
    String Str2; 

    System.out.println(); 

    System.out.print("* Please write a string: "); 
     Str1 = scan.nextLine(); 
    System.out.print("* Please write a string: "); 
     Str2 = scan.nextLine(); 

    //Prints how many characters are in the first sentence and the second sentence.. 

    if (Str1.length() > Str2.length()){ 
     System.out.println("- " + "(" + Str1 + ")" + " has more characters."); 
     System.out.print("- " + "(" + Str1 + ")" + " = " + Str2.length() + " Character(s)" + " && " + "(" + Word2 + ")" + " = " + Word2.length() + " Character(s)"); 
    } 
    else if (Str2.length() > Str1.length()){ 
     System.out.println("- " + "(" + Str2 + ")" + " has more characters."); 
     System.out.print("- " + "(" + Str2 + ")" + " = " + Str2.length() + " Character(s)" + " && " + "(" + Word1 + ")" + " = " + Word1.length() + " Character(s)"); 
    } 
    else{ 
     System.out.println("- " + "(" + Str1 + ")" + " has equal characters with " + "(" + Str2 + ")"); 
     System.out.print("- " + "(" + Str1 + ")" + " = " + Str1.length() + " Character(s)" + " && " + "(" + Word2 + ")" + " = " + Word2.length() + " Character(s)"); 
    }  


    System.out.println(); 

    }//main 
}//Pr7 

我知道我需要调用的方法,但我不知道如何使用它。

回答

0

排序按字母顺序排列的字符串列表,你可以做到以下几点:

List<String> lst = new ArrayList<>(); 
lst.add("asd"); 
lst.add("ffsd"); 

Collections.sort(lst, new Comparator<String>() { 
    @Override 
    public int compare(String foo, String bar) { 
    return String.CASE_INSENSITIVE_ORDER.compare(foo, bar); 
    } 
}); 

// will print the first of the sorted strings 
System.out.println(lst.get(0)); 
相关问题