Java:三个字符串,字典顺序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21465258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:51:04  来源:igfitidea点击:

Java: Three strings, lexicographic order

javacomparetolexicographic

提问by TGautier

beginner Java programmer here. I am trying to compare three strings to each other, and have the system spit out the second/middle word in lexicographic order.

初学者Java程序员在这里。我试图将三个字符串相互比较,并让系统按字典顺序吐出第二个/中间词。

import java.util.*;

public class Ordered2
{
public static void main(String[] args)
{
    String firstString, secondString, thirdString;

    Scanner keyboard = new Scanner(System.in);

    System.out.println("Enter three different strings.");
    System.out.println("The string in the middle order lexicographically will be displayed.");
    firstString = keyboard.nextLine();
    secondString = keyboard.nextLine();
    thirdString = keyboard.nextLine();

    String topString, middleString, bottomString;

    if (firstString.compareTo(secondString) > 0 && (firstString.compareTo(thirdString) > 0)) 
    { topString = firstString; }
    else if (firstString.compareTo(secondString) < 0 && (firstString.compareTo(thirdString) > 0)) {
    middleString = firstString; }
    else { bottomString = firstString; }

    if (secondString.compareTo(firstString) > 0 && (secondString.compareTo(thirdString) > 0)) {
    topString = secondString; }
    else if (secondString.compareTo(firstString) < 0 && (secondString.compareTo(thirdString) > 0)) {
    middleString = secondString; }
    else { bottomString = secondString; }

    if (thirdString.compareTo(secondString) > 0 && (thirdString.compareTo(firstString) > 0)) {
    topString = thirdString; }
    else if (thirdString.compareTo(secondString) < 0 && (thirdString.compareTo(firstString) > 0)) {
    middleString = thirdString; }
    else { bottomString = thirdString; }

    System.out.println("The second string in lexicographic order: " + middleString);
    }
}

This does not compile, and tells me that middleString has not been initialized. Any help would be appreciated.

这不会编译,并告诉我 middleString 尚未初始化。任何帮助,将不胜感激。

采纳答案by Mad Physicist

The Java compiler does not know which branch of an ifstatement will be executed. That means that if you initialize a variable in one branch but not the other, the variable is not guaranteed to have a value assigned to it. In your code, all of the variables will of course be initialized, but the compiler has no way of knowing this, hence your error. You can just initialize the three to nullor an empty string. Replace String topString, middleString, bottomString;with

Java 编译器不知道if将执行语句的哪个分支。这意味着如果您在一个分支中而不是在另一个分支中初始化一个变量,则不能保证该变量具有分配给它的值。在您的代码中,所有变量当然都会被初始化,但编译器无法知道这一点,因此您会出错。您可以将三个初始化为null或一个空字符串。替换String topString, middleString, bottomString;

String topString = null;
String middleString = null;
String bottomString = null;

Additionally, you may want to use some of Java's built-in sorting functionality to do the sorting for you:

此外,您可能希望使用一些 Java 的内置排序功能来为您进行排序:

import java.util.*;

public class Ordered2
{
public static void main(String[] args)
{
    String firstString, secondString, thirdString;

    Scanner keyboard = new Scanner(System.in);

    System.out.println("Enter three different strings.");
    System.out.println("The string in the middle order lexicographically will be displayed.");
    firstString = keyboard.nextLine();
    secondString = keyboard.nextLine();
    thirdString = keyboard.nextLine();

    String[] array = new String[] {firstString, secondString, thirdString};

    Arrays.sort(array);

    System.out.println("The second string in lexicographic order: " + array[1]);
}
}

Arrays.sort()sorts the strings for you. Taking the second (index 1) string out of the sorted array gives you the middle string. If you want to sort using case-insensitive ordering, you can use Arrays.sort(array, String.CASE_INSENSITIVE_ORDER).

Arrays.sort()为您排序字符串。从排序数组中取出第二个(索引 1)字符串为您提供中间字符串。如果要使用不区分大小写的排序进行排序,可以使用Arrays.sort(array, String.CASE_INSENSITIVE_ORDER).

回答by Kick

You have not initialised the variable middleStringand using it in the end of the pgrm as

您尚未初始化变量middleString并在 pgrm 的末尾使用它作为

System.out.println("The second string in lexicographic order: " + middleString);

Simple initialise the variable as below and it will compile.

如下简单初始化变量,它将编译。

String  middleString ="";

回答by Thrash Bean

Just initialize your 3 string topString, middleString, bottomString to empty like this:

只需将您的 3 个字符串 topString、middleString、bottomString 初始化为空,如下所示:

String topString = "";
String middleString = "";
String bottomString = "";

Must compile.

必须编译。

回答by ajb

The logic here is wrong (I've reformatted a bit):

这里的逻辑是错误的(我重新格式化了一点):

if (firstString.compareTo(secondString) > 0 && (firstString.compareTo(thirdString) > 0)) 
    { topString = firstString; }
else if (firstString.compareTo(secondString) < 0 && (firstString.compareTo(thirdString) > 0)) 
    { middleString = firstString; }
else 
    { bottomString = firstString; }

(I'm going along with your approach, which I think can be made to work with some tweaking.) I'm going to call the strings S1, S2, S3. Assuming none of the strings are equal, there are four cases you need to consider. I've listed those, along with what the above code is doing:

(我同意你的方法,我认为可以通过一些调整来工作。)我将调用字符串 S1、S2、S3。假设所有字符串都不相等,则需要考虑四种情况。我已经列出了这些,以及上面的代码在做什么:

S1 > S2 and S1 > S3    S1 is the top string   
S1 > S2 and S1 < S3    S1 is the bottom string
S1 < S2 and S1 > S3    S1 is the middle string
S1 < S2 and S1 < S3    S1 is the bottom string

One of those is wrong. See it?

其中之一是错误的。看见?

(I haven't checked the other two if's. You should do the same thing, looking at four cases for each one.)

(我没有检查其他两个if。你应该做同样的事情,每一个查看四个案例。)

回答by J Steven Perry

If I understand your scenario, you are only interested in Strings, you can take advantage of the Natural Order of Strings and use JDK classes to help you out:

如果我理解你的场景,你只对字符串感兴趣,你可以利用字符串的自然顺序并使用 JDK 类来帮助你:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class StringSorter {

  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    System.out.println("Enter the FIRST string:");
    String first = scanner.nextLine();
    System.out.println("Enter the SECOND string:");
    String second = scanner.nextLine();
    System.out.println("Enter the THIRD string:");
    String third = scanner.nextLine();

    List<String> strings = new ArrayList<String>();

    strings.add(first);
    strings.add(second);
    strings.add(third);

    System.out.println("Before sort:");
    for (String s : strings) {
      System.out.println(s);
    }

    Collections.sort(strings);

    System.out.println("After sort:");
    for (String s : strings) {
      System.out.println(s);
    }

    System.out.println("The Middle String is '" + strings.get(1) + "'");

    scanner.close();

  }

}

When I ran this in Eclipse (go ahead, paste it in and try it!) using John, JOHN and Kevin as the names, I got this result:

当我在 Eclipse 中运行它时(继续,粘贴它并尝试它!)使用 John、JOHN 和 Kevin 作为名字,我得到了这个结果:

Enter the FIRST string:
John
Enter the SECOND string:
JOHN
Enter the THIRD string:
Kevin
Before sort:
John
JOHN
Kevin
After sort:
JOHN
John
Kevin
The Middle String is 'John'