2014-11-15 131 views
3

我想寫一個程序,它接受一個小寫字母,將其轉換成大寫,並將單詞中的元音更改爲下一個字母表。到目前爲止,我已經這樣做了:元音檢查 - 數組越界錯誤

import java.util.*; 
class prg11 
{ 
    public static void main(String args[]) 
    { 
     Scanner sc = new Scanner(System.in); 
     System.out.println("Enter a word in lowercase."); 
     String word = sc.next(); 
     word = word.toUpperCase(); 
     int length = word.length(); 
     char ch[] = new char[length+1]; 
     for (int i = 0; i<=length; i++) 
     { 
      ch[i] = word.charAt(i); 
      if("aeiou".indexOf(ch[i]) == 0) 
      { 
       ch[i]+=1; 
      } 
     } 
     String str = new String(ch); 
     System.out.println(str); 
    } 
} 

代碼編譯得很好。但是,當我運行程序並輸入一個單詞時,請說'嘿',這個單詞只用大寫字母打印。其中的元音(在這種情況下,'e')不會變爲下一個字母表。 我該如何解決這個問題? TIA。

回答

2

我想這應該這樣做,讓我知道,如果它不

public class prg11 { 
public static void main(String[] args) { 
    Scanner sc = new Scanner(System.in); 
    System.out.println("Enter a word."); 
    String word = sc.next(); 
    sc.close(); 
    word = word.toUpperCase(); 
    int length = word.length(); 
    char ch[] = new char[length+1]; 
    for (int i = 0; i<length; i++) { 
     ch[i] = word.charAt(i); 
     if("AEIOU".indexOf(ch[i]) > -1) { 
      ch[i]+=1; 
     } 
    } 
    String str = new String(ch); 
    System.out.println(str); 
} 
} 

讓我知道,如果它的工作原理。
快樂編碼;)-Charlie

1

使用:

for (int i = 0; i<length; i++) 

代替作爲最後索引是長度-1。使用代替

+0

是的,我這樣做,並超出了界限的錯誤得到解決。但是現在,我正面臨着另一個運行時錯誤,我在編輯中解釋了這個錯誤。感謝您的幫助! –

+0

你應該調試你的代碼。 – BobTheBuilder

1

for (int i = 0; i<=length-1; i++)for (int i = 0; i<=length; i++)
if("AEIOU".indexOf(ch[i]) != -1)代替if("aeiou".indexOf(ch[i]) == 0)

原因
1.array指數爲0,這就是爲什麼length-1
2.當你已經取得你的大寫字符串,以便檢查條件「開始AEIOU」
3.每個非元音字符將返回-1所以使用if("AEIOU".indexOf(ch[i]) != -1)

1

"aeiou".indexOf(ch[i]) == 0將只匹配‘A’字符(正弦e就是索引0處的字符)。您應該查找大於-1的任何索引。此外,由於您已將字符串轉換爲大寫,因此您應該檢查「AEIOU」而不是「aeiou」。

3

根據問題中的代碼,需要更改三個地方。

word = word.toUpperCase(); 
int length = word.length(); 

// yours: char ch[] = new char[length + 1]; 
// resulting array needs to be as same length as the original word 
// if not, there will be array index out of bound issues 
char ch[] = new char[length]; 

// yours: for (int i = 0; i<=length; i++) 
// need to go through valid indexes of the array - 0 to length-1 
for (int i = 0; i < length; i++) { 
    ch[i] = word.charAt(i); 
    // yours: if ("aeiou".indexOf(ch[i]) == 0) { 
    // two problems when used like that 
    // 1. indexOf() methods are all case-sensitive 
    // since you've uppercased your word, need to use AEIOU 
    // 2. indexOf() returns the index of the given character 
    // which would be >= 0 when that character exist inside the string 
    // or -1 if it does not exist 
    // so need to see if the returned value represents any valid index, not just 0 
    if ("AEIOU".indexOf(ch[i]) >= 0) { 
     ch[i] += 1; 
    } 
} 

這裏有一個簡潔的版本。請注意我所做的更改。

String word = sc.next().toUpperCase(); 
char ch[] = word.toCharArray(); 
for (int i = 0; i < ch.length; i++) { 
    if ("AEIOU".indexOf(ch[i]) >= 0) { 
     ch[i] += 1; 
    } 
} 

Java doc的indexOf()

public int indexOf(int ch) 

Returns the index within this string of the first occurrence of the specified character. 
If a character with value ch occurs in the character sequence represented by this String object, 
then the index (in Unicode code units) of the first such occurrence is returned. 
For values of ch in the range from 0 to 0xFFFF (inclusive), this is the smallest value k such that: 

    this.charAt(k) == ch 

is true. For other values of ch, it is the smallest value k such that: 

    this.codePointAt(k) == ch 

is true. In either case, if no such character occurs in this string, then -1 is returned. 

Parameters: 
    ch - a character (Unicode code point). 
Returns: 
    the index of the first occurrence of the character in the character sequence represented by this object, 
    or -1 if the character does not occur.