简体   繁体   中英

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. 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. 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.

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. 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 (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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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