首先按字母顺序打印哪个单词并检查字符串中是否存在某个字符?

时间:2014-10-29 08:59:17

标签: java if-statement char alphabetical

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

例如:如果用户写了“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).. *** WORNG ***

    char ch;
    int compare = Word1.compareTO(Word2);

    if (compare < 0)  
       System.out.print(compare)
    else   
      if (compare > 0)
    a is larger 
   }else  
   {  

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

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

  }//main
}//Pr7

最后两个运算符是错误的,我需要正确的方法来修复它。

2 个答案:

答案 0 :(得分:1)

要对两个单词进行排序,请使用String.compareTo()。您可能会发现将它们添加到列表中更容易,然后使用Collections.sort()对该列表进行排序。字符串使用字母顺序自然排序。

要查看字符串是否包含特定字符,请使用String.indexOf('z')并测试返回值是否为-1,这表示不匹配。或者,正如Tom建议的那样,您可以使用String.contains("z"),这需要您传递字符串"z"而不是字符'z'

答案 1 :(得分:0)

这是我修复它的方式..

import java.util.*;
public class Pr6{
  public static void main(String[] args){
    Scanner scan = new Scanner (System.in);    
    String word1;
    String word2;
    String sentence1;
    String sentence2;
    
    System.out.print("* Please enter a word: ");
    word1 = scan.nextLine();
    System.out.print("* Please enetr a word: ");
    word2 = scan.nextLine();
    
    if (word1.length() > word2.length())
      System.out.print("The first word has more characters than the second word");
    else if (word1.length() < word2.length())
      System.out.print("The second word has more characters than the first word");
    else
      System.out.print("The characters in both words are equal");
    
    System.out.println();
    
    if (word1.toLowerCase().charAt(0) < word2.toLowerCase().charAt(0))
      System.out.println(word1 + " (comes first alphabetically)");
    else if (word1.toLowerCase().charAt(0) > word2.toLowerCase().charAt(0))
      System.out.println(word2 + " (comes first alphabetically)");
    
    if (word1.toLowerCase().indexOf('z') >= 0)
      System.out.println("Letter 'z' appears in the first word.");
    else
      System.out.println("Letter 'z' doesn't appear in the first word.");
    if (word2.toLowerCase().indexOf('z') >= 0)
      System.out.println("Letter 'z' appears in the second word.");
    else
      System.out.println("Letter 'z' doesn't appear in the second word.");
    
    System.out.print("* Please enter a sentence: ");
    sentence1 = scan.nextLine();
    System.out.print("* Please enter a sentence: ");
    sentence2 = scan.nextLine();
    
    System.out.println("The first sentence has " + sentence1.length() + " characters");
    System.out.println("The second sentence has " + sentence2.length() + " characters");
    
    if (sentence1.toLowerCase().indexOf(" the ") >= 0)
      System.out.println("Substring 'the' appears in the first sentence.");
    else
      System.out.println("Substring 'the' doesn't appear in the first sentence.");
    if (sentence2.toLowerCase().indexOf(" the ") >= 0)
      System.out.println("Substring 'the' appears in the second sentence.");
    else
      System.out.println("Substring 'the' doesn't appear in the second sentence.");
    
    if (sentence1.length() >= 11)
      System.out.println(sentence1.replace(sentence1.substring(8, 10), "the"));
    if (sentence2.length() >= 11)
      System.out.println(sentence2.replace(sentence2.substring(8, 10), "the"));
      
  }//main
}//Pr6