2017-03-20 72 views
0

我试图创建一个程序,它接受用户的输入,然后用for循环扫描用户输入到数组中。这样我可以遍历数组来查找该字符串是否是一个单词回文。单词回文不同于回文,因为它是整个单词的反向,而不是每个单独的字母。当我编写的程序打印时,它只是打印出空白,我相信这意味着它不存储扫描仪扫描的内容。 下面是我写的东西:如何获取用户输入,将其添加到新扫描仪,然后将输入扫描到数组中?

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 
+0

你的描述很模糊。所以你想要做的是,一个字符串是否是一个回文。 –

+0

'stringScan'在第一次循环的第一次迭代结束时用完。目前还不清楚为什么你甚至有嵌套循环。 – shmosel

回答

1

你已经有了一些时髦的逻辑怎么回事。有几件事情:

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

userInputLengthuserInput字符串,它是一个字符的字符串中的数量,而不是字的数量的长度。

它看起来像while循环仅用于计算所需的数组大小,但外部for循环不是必需的。你有效地说,对于输入字符串中的每个字符,当扫描器有另一个单词时,计数单词,这没有多大意义。

你在你的第二个for循环中做了类似的事情,这也没有多大意义。

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

它会更容易使用List和保存自己,带有固定大小的数组的麻烦。您可以初始化列表并添加内容,而不必关心它有多大。

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

下面是一些代码来简化你的问题有点:

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 
+0

谢谢!发布后我注意到了一些奇怪的逻辑,我应该编辑它,但是我很感谢帮助。 – katie

0

我做了一些改动(删除for循环和额外的迭代),请参考代码中的注释:

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

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

// *** CHANGE- we can directly store the array of words separated by space 
String stringArray[] = userInput.split(" "); 

System.out.println(Arrays.toString(stringArray)); // how I've checked if 
                 // it's storing 
keyboard.close(); // Closing scanner 
+0

使用已生成数组的'split'似乎效率不高,以不同的方式生成数组。 –

+0

替代它,也许我们可以使用'StringTokenizer'来计算单词,即'StringTokenizer st = new StringTokenizer(userInput); \t \t int wordCount = st.countTokens(); //找到字数' – 0p3n5ourcE

+1

另一种选择是使用'userInput.split(“”)'。要么这将工作得很好,要么使用这个长度是一个坏主意。 –