在Java中按字母顺序对字符串进行排序而没有数组 [英] Sorting strings alphabetically without arrays in java
问题描述
因此,对于此程序,我需要从用户处获取三个字符串,然后按字母顺序对这些字符串进行排序.需要注意的是,我不能使用数组.同样,如果字符串不是以字母开头的,则不接受.到目前为止,我已经设法从用户那里获得了三个字符串,并且能够确定是否应该接受它们.我不知道如何按字母顺序对它们进行排序.我遇到的主要问题是忽略排序时不接受的字符串.这是从用户那里获取字符串的代码.
So for this program I need to obtain three strings from a user and sort those strings alphabetically. The caveat is that I cannot use arrays. Also if the string does not start with an alphabetic letter than it is not accepted. So far I have managed to get the three strings from the user and am able to determine if they should be accepted on or not. What I can't figure out is how to sort them alphabetically. The main issue I am having is ignoring the strings that weren't accepted when sorting. Here is the code to get the strings from the user.
Scanner scan = new Scanner(System.in);
System.out.print("Please input the first name: ");
String name1 = scan.nextLine();
name1 = name1.substring(0, 1).toUpperCase() + name1.substring(1,name1.length()).toLowerCase();
if (name1.charAt(0) >= 'A' && name1.charAt(0) <= 'Z' ){
System.out.println("The first name is: " + name1);
}else{
System.out.println("Error: The name was not accepted");
name1 = null;
}
System.out.print("Please input the second name: ");
String name2 = scan.nextLine();
name2 = name2.substring(0, 1).toUpperCase() + name2.substring(1,name2.length()).toLowerCase();
if (name2.charAt(0) >= 'A' && name2.charAt(0) <= 'Z'){
System.out.println("The second name is: " + name2);
}else{
System.out.println("Error: The name was not accepted");
name2 = null;
}
System.out.print("Please input the third name: ");
String name3 = scan.nextLine();
name3 = name3.substring(0, 1).toUpperCase() + name3.substring(1,name3.length()).toLowerCase();
if (name3.charAt(0) >= 'A' && name3.charAt(0) <= 'Z'){
System.out.println("The third name is: " + name3);
}else{
System.out.println("Error: The name was not accepted");
name3 = null;
}
推荐答案
我想出了这个解决方案.在不使用数组的情况下编写此代码是可读性的噩梦.
I came up with this solution. It is a nightmare in readability to write this code without an array.
import java.util.Scanner;
public class NameSort {
private static final Scanner scan = new Scanner(System.in);
public static void nameStort() {
String name1 = readName("first");
String name2 = readName("second");
String name3 = readName("third");
String potentialName1 = null;
String potentialName2 = null;
String potentialName3 = null;
String potentialName4 = null;
String potentialName5 = null;
String potentialName6 = null;
String potentialName7 = null;
if (name1 == null) {
if (name2 == null) {
potentialName4 = name3;
} else {
potentialName4 = name2;
if (name3 != null) {
if (name2.compareTo(name3) > 0) {
potentialName2 = name3;
} else {
potentialName6 = name3;
}
}
}
} else {
potentialName4 = name1;
if (name2 == null) {
if (name3 != null) {
if (name1.compareTo(name3) > 0) {
potentialName2 = name3;
} else {
potentialName6 = name3;
}
}
} else {
if (name1.compareTo(name2) > 0) {
potentialName2 = name2;
} else {
potentialName6 = name2;
}
if (name3 != null) {
if (name1.compareTo(name3) > 0) {
if (name2.compareTo(name3) > 0) {
potentialName1 = name3;
} else {
potentialName3 = name3;
}
} else {
if (name2.compareTo(name3) > 0) {
potentialName5 = name3;
} else {
potentialName7 = name3;
}
}
}
}
}
System.out.println("The sorted names are: ");
printIfNotNull(potentialName1);
printIfNotNull(potentialName2);
printIfNotNull(potentialName3);
printIfNotNull(potentialName4);
printIfNotNull(potentialName5);
printIfNotNull(potentialName6);
printIfNotNull(potentialName7);
}
private static void printIfNotNull(String potentialName) {
if (potentialName != null) {
System.out.println(potentialName);
}
}
public static String readName(String position) {
System.out.print("Please input the " + position + " name: ");
String name = toTitleCase(scan.nextLine().trim());
if (!name.isEmpty() && Character.isLetter(name.charAt(0))) {
System.out.println("The " + position + " name is: " + name);
return name;
} else {
System.out.println("Error: The name was not accepted");
}
return null;
}
public static String toTitleCase(String word) {
if (word.isEmpty()) {
return word;
}
// one way to do it
return word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase();
}
public static void main(String[] args) {
nameStort();
}
}
但是在赋值中没有愚蠢的数组或集合限制的情况下,代码更具可读性:
But without the silly array or collection restriction in the assignment the code is much more readable:
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class NameSort {
private static final Scanner scan = new Scanner(System.in);
public static void nameStort() {
List<String> names = Arrays.asList(readName("first"), readName("second"), readName("third"));
Collections.sort(names);
for (String name : names) {
printIfNotEmpty(name);
}
}
private static void printIfNotEmpty(String name) {
if (!name.isEmpty()) {
System.out.println(name);
}
}
public static String readName(String position) {
System.out.print("Please input the " + position + " name: ");
String name = toTitleCase(scan.nextLine().trim());
if (!name.isEmpty() && Character.isLetter(name.charAt(0))) {
System.out.println("The " + position + " name is: " + name);
return name;
} else {
System.out.println("Error: The name was not accepted");
}
return "";
}
public static String toTitleCase(String word) {
if (word.isEmpty()) {
return word;
}
// one way to do it
return word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase();
}
public static void main(String[] args) {
nameStort();
}
}
这篇关于在Java中按字母顺序对字符串进行排序而没有数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!