如何将用户输入拆分为字符数组或字符串数​​组,然后在 java 中检查是否回文

How to split user input into char array or string array and then check if palindrome or not in java

所以我正在从我的 Java 编程 class 中编写一个程序,要求制作一个回文程序。我已经成功地使程序在用户输入一个词的情况下运行干净,但我仍然坚持如何检查用户单独输入的三个词。到目前为止,这是我的代码。实验 objective 是:单独检查每个单词,看它是否是回文——如果找到,将其打印到屏幕上。 我必须遵循以下说明: • 要求用户在一行中提供三个回文

• 分别检查每个单词是否为回文 - 如果找到,打印 它到屏幕上 – 提示:查看 String 的 toCharArray() 方法 • 不断询问用户,直到他们提供了一组包含至少 一个回文。

import java.util.Scanner;
public class Palindrome {

private static Scanner scanUserInput;
private static String wordGuess, reverseWord ;
public static void main(String[] args) {
scanUserInput = new Scanner(System.in);
System.out.println("WELCOME TO THE PALINDROME CHECKER!!!");
while(true){
System.out.print("Please enter at least three words to check: ");
wordGuess = scanUserInput.nextLine();
reverseWord = "";

//String[] wordArray = wordGuess.split(","); I tried to use this way to split the inputs but no luck
char[] wordArray = wordGuess.toCharArray();
for(int x=wordArray.length-1;x>=0;x--){
    reverseWord = reverseWord+wordArray[x];

}
System.out.println(reverseWord);
if(wordGuess.equalsIgnoreCase(reverseWord))
{
    System.out.println("");
    System.out.println("You have found a Palindrome!!!");
    System.out.println("The Palindrome we found was "+reverseWord);
    break;
}
else{
    System.out.println("");
    System.out.println("You have not entered a Palindrome...");
    System.out.println("Please Try again...");

}

}//end of main
}
}

提前感谢您抽出宝贵时间。请将对我完成此实验或任何其他想法有用的任何文档返回给我。 enter image description here

我觉得你的逻辑完全没问题。如果您有额外的要求能够接受要检查的 CSV 单词列表,那么您可以按如下方式稍微修改您的代码:

System.out.print("Please enter at least three words to check: ");
String wordGuess = scanUserInput.nextLine();
String[] words = wordGuess.split(",\s*");
for (String word : words) {
    String reverseWord = "";
    char[] wordArray = word.toCharArray();
    for (int x=wordArray.length-1; x>=0; x--) {
        reverseWord = reverseWord + wordArray[x];
    }
    System.out.println(reverseWord);
   if (wordGuess.equalsIgnoreCase(reverseWord)) {
       System.out.println("");
       System.out.println("You have found a Palindrome!!!");
       System.out.println("The Palindrome we found was " + reverseWord);
    }
    else {
        System.out.println("");
        System.out.println("You have not entered a Palindrome...");
        System.out.println("Please Try again...");
    }
}

我认为您用于拆分的模式有问题。您正在这样做:

String[] wordArray = wordGuess.split(",");

但是,如果您输入带有空格的 CSV 列表,那么它会将空格捕获为单词的一部分,然后可能不会有回文。此外,您需要循环输入单词,而您也没有这样做。

您可以为此目的使用拆分,它会起作用。

String[] wordArray = wordGuess.split(" "); //or .split("<seperator used b/w words>")

现在迭代 wordArray 并为每个单词单独检查回文。 在您的代码中,您没有单独检查每个单词

//错误

for(int x=wordArray.length-1;x>=0;x--){
    reverseWord = reverseWord+wordArray[x];
}

//修改代码

   while(true){
        System.out.print("Please enter at least three words to check: ");
        wordGuess = scanUserInput.nextLine();

        String[] wordArray = wordGuess.split(",");
       // char[] wordArray = wordGuess.toCharArray();
        for (String word : wordArray) {
            reverseWord = "";
            for(int x=word.length()-1;x>=0;x--){
                reverseWord = reverseWord+word.charAt(x);

            }
            System.out.println(reverseWord);
            if(word.equalsIgnoreCase(reverseWord))
            {
                System.out.println("");
                System.out.println("You have found a Palindrome!!!");
                System.out.println("The Palindrome we found was "+reverseWord);
            }
            else{
                System.out.println("");
                System.out.println("You have not entered a Palindrome...");
                System.out.println("Please Try again...");

            }
        }


    }

如果用户只输入一个单词,您的代码将 运行 正常。但是当你输入多个单词时 till 就会报错。 示例:输入:racecar hello test 您的代码会将其反转为:tset olleh racecar

赛车你好测试!= tset olleh 赛车

如果用户输入的分隔符为 ,则使用

String[] wordArray = wordGuess.split(",");

将您的逻辑放在一个单独的函数中:

public checkPalindrome(String word){
    reverseWord="";
    for(int x=word.length-1;x>=0;x--){
        reverseWord = reverseWord+wordArray[x];

    }
    System.out.println(reverseWord);
    if(wordGuess.equalsIgnoreCase(reverseWord))
    {
        System.out.println("");
        System.out.println("You have found a Palindrome!!!");
        System.out.println("The Palindrome we found was "+reverseWord);
        break;
   }
   else{
        System.out.println("");
        System.out.println("You have not entered a Palindrome...");
        System.out.println("Please Try again...");

  }

} 

从您的 main 方法中调用此函数 3 次,并将所有以“,”分隔的字符串一一传递。

checkPalindrome(wordArray[0]);
checkPalindrome(wordArray[1]);
checkPalindrome(wordArray[2]);

输出:

WELCOME TO THE PALINDROME CHECKER!!!
Enter number of Strings with spaces to check palindrome
abba acac adda abaa
Pallindrome Strings
abba
adda

您的问题是:- 实验室 objective 是单独检查每个单词,看它是否是回文 - 如果找到一个,将其打印到屏幕上。

你必须检查每个字符串是否是回文,这是我从你的问题中得到的:-

这是该解决方案的代码:

import java.util.Scanner;
import java.util.StringTokenizer;

/**
 * Created by itt on 10/4/17.
 */
public class Palindrome {
    private static Scanner scanUserInput;
    private static String wordGuess, reverseWord ;
    public static void main(String[] args) {
        scanUserInput = new Scanner(System.in);
        System.out.println("WELCOME TO THE PALINDROME CHECKER!!!");

        System.out.println("Enter Strings with spaces to check palindrome");
        wordGuess = scanUserInput.nextLine();

        StringTokenizer tokens = new StringTokenizer(wordGuess);

        System.out.println("Pallindrome Strings");
        while (tokens.hasMoreTokens()){
            StringBuilder builder = new StringBuilder();
            String token = tokens.nextToken();
            builder.append(token);
            if(builder.reverse().toString().equals(token)) {
                System.out.println(token);
            }
        }

    }
}

输出:

WELCOME TO THE PALINDROME CHECKER!!!
Enter Strings with spaces to check palindrome
abba acac daad jaaj
Pallindrome Strings
abba
daad
jaaj

如果我错了,请指正,以便我改进。