简体   繁体   English

如何接收用户输入,将其添加到新的扫描仪中,然后将输入扫描到阵列中?

[英]How do I take a users input, add that to a new scanner, and then scan the input into an array?

I'm trying to create a program that takes the user's input and then scans the user's input into an array with a for loop. 我正在尝试创建一个程序,该程序需要用户输入,然后将用户输入扫描到带有for循环的数组中。 That way I can loop through the array to find if the string is a word palindrome or not. 这样,我就可以遍历数组以查找字符串是否是回文词。 A word palindrome differs from a palindrome in that it is the whole word in reverse rather than each individual letter in reverse. 回文词与回文词的不同之处在于,它是整个单词的反义词,而不是每个字母的反义词。 When the program that I wrote prints it just prints null, which I believe means that it's not storing what the scanner scanned. 当我编写的程序打印时,它只打印null,这意味着它没有存储扫描仪扫描的内容。 Below is what I've written: 以下是我写的内容:

String userInput, scannedWord;
Scanner keyboard = new Scanner(System.in); //scanner for user input

System.out.print("Please enter a sentence: ");
userInput = keyboard.nextLine(); //stores user input

Scanner stringScan = new Scanner(userInput); //scanner to scan through user input

int userInputLength = userInput.length(); //to find word count
int wordCount = 0; //for array size


for (int i = 0; i < userInputLength; i++) //finds word count
{
    while(stringScan.hasNext())
    {
      scannedWord = stringScan.next();
      wordCount = wordCount + 1;
    }
}

String stringArray[] = new String[wordCount]; 

  for (int i = 0; i < userInputLength; i++) //should store scanned words into the array
  {
    while (stringScan.hasNext())
    {
      scannedWord = stringScan.next();
      stringArray[i] = scannedWord;
    }
  }

System.out.println(Arrays.toString(stringArray)); //how I've checked if it's storing

You've got some funky logic going on here. 您在这里进行了一些时髦的逻辑。 A few things: 一些东西:

userInput = keyboard.nextLine(); //stores user input
int userInputLength = userInput.length(); //to find word count

userInputLength is the length of the userInput string, which is the number of characters in the string, not the number of words. userInputLengthuserInput字符串的长度,它是字符串中的字符数,而不是单词数。

It looks like the while loop is used simply to calculate the required size of the array, but the outer for loop is not required. 看起来, while循环仅用于计算所需的数组大小,但不需要外部for循环。 You're effectively saying, for every character in the input string, while the scanner has another word, count the word, which doesn't make much sense. 您实际上是在说,对于输入字符串中的每个字符,当扫描程序有另一个单词时,请对该单词计数,这没有什么意义。

You do something similar in your second for loop, which also doesn't make much sense. 您在第二个for循环中执行类似的操作,这也没有多大意义。

for (int i = 0; i < userInputLength; i++) //finds word count
{
    while(stringScan.hasNext())
    {
      scannedWord = stringScan.next();
      wordCount = wordCount + 1;
    }
}

It would be easier to use a List and save yourself the trouble that comes with fixed size arrays. 使用List会更容易,并且省去了固定大小的数组带来的麻烦。 You can just initialize the list and add things to it without caring about how big it is. 您可以只初始化列表并向其中添加内容,而无需关心列表的大小。

List<String> words = new ArrayList<String>();
words.add(word1);
words.add(word2);

Here's some code to simplify your problem a little: 以下是一些代码,可以简化您的问题:

Scanner keyboard = new Scanner(System.in); //scanner for user input

System.out.print("Please enter a sentence: ");

String userInput = keyboard.nextLine(); //stores user input

Scanner stringScan = new Scanner(userInput); //scanner to scan through user input

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

while (stringScan.hasNext())
{
    String scannedWord = stringScan.next();
    words.add(scannedWord);
}

System.out.print(Arrays.toString(words.toArray())); // nasty! but you can see what's in the array for debugging

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM