2017-09-30 7 views
0

本質的に私はPig Latinコンバータを作成しようとしています。ただし、この割り当てでは、単語の入力をやめるためにユーザーが 'Q'を入力できるようにする必要があります。私は、コードをコンパイルするために取得することができますが、ユーザーがQに入った時はいつでも、それがクラッシュし、スロー:ユーザ入力= qのときにループを終了しようとしています

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1 
at java.lang.String.charAt(String.java:658) 
at mission4aa.Mission4AA.main(Mission4AA.java:38) 

私はちょうどcompletleyわからないどこでもこれを固定して行くことになっています。私は努力してきました。ない新しいもの -

import java.util.Scanner; 


public class Mission4AA { 

    public static void main(String[] args) { 

     Scanner scanIn = new Scanner(System.in); 
     String userInput; 
     int firstVowel = 0; 

     System.out.println("Welcome to the pig latin translator!"); 
     System.out.println("Please enter a word (Q to exit): "); 

     do { 
      userInput = scanIn.next(); 
      userInput = userInput.trim(); 
      userInput = userInput.toLowerCase(); 
      int end = userInput.length(); 

      char a = userInput.charAt(0); 
      if (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u') 
       System.out.println(userInput + "way"); 

      else { //Check for next vowel if the first letter is consonant 
       for (int i = 1; i < userInput.length(); i++) { 
        char b = userInput.toLowerCase().charAt(i); 
        if (b == 'a' || b == 'e' || b == 'i' || b == 'o' || b == 'u') { 
         firstVowel = i; //Stores the index of the first vowel 
         break;   
        } 
       } 
       if(userInput.charAt(1) != firstVowel) { 
        String startString = userInput.substring(firstVowel, end); 
        String endString = userInput.substring(0, firstVowel) + "ay"; 
        String result = startString + endString; 
        System.out.println("Translation: " + result); 

       }    

      } 
      System.out.println("Enter another word(Q to exit): "); 
     } while (!userInput.equalsIgnoreCase("q")); 
     System.out.println("Thank you"); 
    } 

} 
+1

これは、単に「Q」または「q」と入力すると、if(userInput.charAt(1)!= firstVowel){'が爆発するからです。インデックス '1'に' char'はありません。 –

+0

なぜ入力を小文字に2回変換していますか?入力が単一の子音であれば、何が起こると思いますか? –

答えて

0

問題は、あなたは、このようにあなたのdo-whileループ内の条件は、以前のユーザー入力をチェックし、ループの最初にユーザー入力を読むことのように見えます。

さらに、if-statementのelseブランチは、入力が少なくとも2文字の長さであることを前提としています。if(userInput.charAt(1) != firstVowel) {...}

これは、入力"q"として例外が発生し、他の分岐に達したが、唯一の長さのあるものであるあなたがあなたのコードには2つの変更を加える必要がある1.

  1. あなたが必要としますloop-conditionをチェックする前にユーザ入力を読んでください。
  2. elseブランチでは、入力が2文字以上であることを確認してから、2文字目が母音かどうかを確認する必要があります。
以下

修正コード:

public static void main(String[] args) { 

    Scanner scanIn = new Scanner(System.in); 
    String userInput; 
    int firstVowel = 0; 

    System.out.println("Welcome to the pig latin translator!"); 
    System.out.println("Please enter a word (Q to exit): "); 
    userInput = scanIn.next().trim().toLowerCase(); 

    do { 
     int end = userInput.length(); 

     char a = userInput.charAt(0); 
     if (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u') 
      System.out.println(userInput + "way"); 

     else { //Check for next vowel if the first letter is consonant 
      for (int i = 1; i < userInput.length(); i++) { 
       char b = userInput.toLowerCase().charAt(i); 
        if (b == 'a' || b == 'e' || b == 'i' || b == 'o' || b == 'u') { 
         firstVowel = i; //Stores the index of the first vowel 
         break;   
        } 
      } 
      if(end > 1 && userInput.charAt(1) != firstVowel) { 
       String startString = userInput.substring(firstVowel, end); 
       String endString = userInput.substring(0, firstVowel) + "ay"; 
       String result = startString + endString; 
       System.out.println("Translation: " + result); 
      } else { /* Handle handle input of length 1 */} 

     } 
     System.out.println("Enter another word(Q to exit): "); 

     userInput = scanIn.next().trim().toLowerCase(); 

    } while (!userInput.equalsIgnoreCase("q")); 
     System.out.println("Thank you"); 
    } 
1

このチェックユーザーは 'Q' を入力している場合

if(userInput.charAt(1) != firstVowel) { 

を行っている際に、ユーザ入力にのみ0項(長さを持つことになりますので1)。ユーザー入力の2番目の文字を実際に取得しようとしています。あなたの問題を解決するために、私は 'q'のチェックをdoセクションの開始時に行います(または単にdo-whileの概念を廃止し、while(true)ループを使用します)。将来的にあなたは長さ1である入力を処理する必要があります。しかし、ユーザーがちょうどQまたはqを入力した場合、あなたの問題のために、このようなものが

do { 
    userInput = scanIn.next(); 
    userInput = userInput.trim(); 
    userInput = userInput.toLowerCase(); 
    int end = userInput.length(); 

    char a = userInput.charAt(0); 
    //here 
    if(userInput.equals("q") || userInput.equals("Q")){ 
     System.out.println("Thank you"); 
     return; 
    } 
    //else continue 
1

を働くことに注意してください - なぜあるindex1で何charは、ありませんコードでjava.lang.StringIndexOutOfBoundsException例外がスローされます。

これを修正する方法はたくさんあります。私の場合、do-whilewhile(true)に変換しました。入力がちょうどQまたはqの場合はbreakを使用します。

// get first input 
userInput = scanIn.next(); 
while(true){ 

    userInput = userInput.trim(); 
    userInput = userInput.toLowerCase(); 

    int end = userInput.length(); 

    char a = userInput.charAt(0); 
    if (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u') 
     System.out.println(userInput + "way"); 

    else { //Check for next vowel if the first letter is consonant 
     for (int i = 1; i < userInput.length(); i++) { 
      char b = userInput.toLowerCase().charAt(i); 
       if (b == 'a' || b == 'e' || b == 'i' || b == 'o' || b == 'u') { 
        firstVowel = i; //Stores the index of the first vowel 
        break;   
       } 
     } 
     if(userInput.charAt(1) != firstVowel) { 
      String startString = userInput.substring(firstVowel, end); 
      String endString = userInput.substring(0, firstVowel) + "ay"; 
      String result = startString + endString; 
      System.out.println("Translation: " + result); 

     }    

    } 

    // check next word here - if Q or q, break out and finish 
    userInput = scanIn.next(); 
    if(userInput.equalsIgnoreCase("q")) { 
     break; 
    } 
    System.out.println("Enter another word(Q to exit): "); 
} 

注 - 印刷文を適切に並べ替える必要があります。

関連する問題