问题
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;
}
回答1:
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();
}
}
回答2:
For any of those wondering here is my solution to this problem given the ridiculous constraints. public class Assignment3 {
public static void main(String[] args) {
//read names and return error if the name starts with a non-alphabetic character
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 (Character.isLetter(name1.charAt(0))){
System.out.println("The first name is: " + name1);
}else{
System.out.println("Error: The name was not accepted");
name1 = "";
}
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 (Character.isLetter(name2.charAt(0))){
System.out.println("The second name is: " + name2);
}else{
System.out.println("Error: The name was not accepted");
name2 = "";
}
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 (Character.isLetter(name3.charAt(0))){
System.out.println("The third name is: " + name3);
}else{
System.out.println("Error: The name was not accepted");
name3 = "";
}
//Sort the names alphabetically
String first = "";
if(name1.compareTo(name2) <= 0 && name1.compareTo(name3) <= 0){
first = name1;
}else if (name2.compareTo(name3) <= 0 && name2.compareTo(name1) <= 0){
first = name2;
}else if (name3.compareTo(name1) <= 0 && name3.compareTo(name2) <=0){
first = name3;
}
String middle = "";
if (name1.compareTo(name2)*name1.compareTo(name3) <= 0){
middle = name1;
}else if (name2.compareTo(name1)*name2.compareTo(name3) <= 0){
middle = name2;
}else if (name3.compareTo(name2)*name3.compareTo(name1) <= 0){
middle = name3;
}
String last = "";
if(name1.compareTo(name2) >= 0 && name1.compareTo(name3) >= 0){
last = name1;
}else if (name2.compareTo(name3) >= 0 && name2.compareTo(name1) >= 0){
last = name2;
}else if (name3.compareTo(name1) >= 0 && name3.compareTo(name2) >=0){
last = name3;
}
if(first.isEmpty() && middle.isEmpty()){
System.out.printf("The only name is \"%s\" ", last);
}else if(first.isEmpty()){
System.out.printf("the names are \"%s\"and \"%s\" ",middle,last);
}else{
System.out.printf("the names are \"%s\", \"%s\", and \"%s\" ",first,middle,last);
}
}
}
来源:https://stackoverflow.com/questions/25924445/sorting-strings-alphabetically-without-arrays-in-java