2017-11-16 2 views
1

Dies ist der Code für einen Schwein-Latein-Übersetzer in JAVA, es funktioniert mit einem Wort, aber nie mit einem Satz. Es scheint, dass der Code in Zeile 30 alles durcheinander bringt, und ich bin nicht sicher, wie es das macht oder wie ich es beheben kann. IndexOutOfBoundError in Zeile 8 und Zeile 30. Ich bin mir nicht sicher, wie ich das beheben kann, Hilfe.IndexOutOfBound Fehler mit String-Manipulation

public class Practice 
    { 
     public static void main(String[] args) 
     { 
      String a = "hello Stranger"; 

     System.out.println(translate(a)); //8 
    } 

    private static String translate(String a) 
    { 
     String XD = a; 
     boolean repeat = true; 
     int first = 1; 
     int second = 0; 

     do 
     { 
      second = XD.indexOf(" "); 

      if (second == -1) 
      { 
       repeat = false; 
       XD = vowelFinder(a); 
       break; 
      } 
      else 
      { 
       XD = XD + vowelFinder(a.substring(first, second)); //30 
      } 


      first = second +1; 
     }while(repeat == true); 

     return XD; 
    } 

    private static boolean isVowel (char c) 
    { 
     if (c == 'a'|| c== 'e'|| c== 'i' || c == 'o' || c== 'u') 
     { 
      return true; 
     } 

     return false; 
    } 

    private static String vowelFinder(String s) 
    { 
     String nope = s; 

     for(int i = 0; i <= s.length(); i++) 
     { 
      if(isVowel(s.charAt(i)) == true) 
      { 
       nope = nope.substring(i) + "-"+nope.substring(0, i);` 
       return nope; 
      } 
     } 

     return nope; 
    } 

} 
+1

er schrieb sie als Kommentare – sfat

+0

i <= s.length() sollte nicht i

Antwort

1

Versuchen Sie dies;

import java.util.Scanner; 

public class PigLatin { 
    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 
     String yourSentence=""; 
     do { 
      String[] words; 
      System.out.print("Enter your words here: "); 
      yourSentence = input.nextLine(); 
      words = yourSentence.split(" "); 
      for (String word : words) { 
       if (word.startsWith("a") || word.startsWith("e") || word.startsWith("i") || word.startsWith("o") || word.startsWith("u")) 
        System.out.print(word + "way "); 
       else if (word.startsWith("sh") || word.startsWith("ch") || word.startsWith("th")) 
        System.out.print(word.substring(2)+word.substring(0,2)+"ay "); 
       else 
        System.out.print(word.substring(1)+word.substring(0,1)+"ay "); 
     } 
     System.out.println(); 
     } while(!yourSentence.equals("quit")); 
    } 
} 
Verwandte Themen