【问题标题】:how to find a letter in a string and how to return two strings in alphabetical order? java [duplicate]如何在字符串中查找一个字母以及如何按字母顺序返回两个字符串? java [重复]
【发布时间】:2014-10-31 01:50:32
【问题描述】:

当我提示用户写两个单词时,如何打印哪个单词在前(按字母顺序)?以及如何检查扫描的单词中是否存在某个字符?

例如:如果用户写了“Word”和“Apple”,我如何按字母顺序打印这两个词。另外,我编写了一个程序来检查 char '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

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

【问题讨论】:

    标签: java string substring alphabetical


    【解决方案1】:

    要按字母顺序对字符串列表进行排序,您可以执行以下操作:

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

    【讨论】:

      猜你喜欢
      • 2014-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-22
      • 1970-01-01
      • 2012-02-29
      • 2010-11-04
      相关资源
      最近更新 更多