2017-03-20 67 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(「」)'。要麼這將工作得很好,要麼使用這個長度是一個壞主意。 –