Disclaimer: I'm a beginner straight out of AP Computer Science. I'm trying to make an app that converts text input from the user into pig latin... For practice.
Here's my code: package com.example.ashavolian.piglatin2; import android.content.DialogInterface; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.view.View.OnClickListener; public class MainActivity extends AppCompatActivity { TextView txtOut; EditText txtIn; Button button; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); txtOut = (TextView) findViewById(R.id.txtOut); txtIn = (EditText) findViewById(R.id.txtIn); button = (Button) findViewById(R.id.button); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String txtTxtIn = txtIn.getText().toString(); txtOut.setText(translate(txtTxtIn)); } }); } public String translate(String txtTxtIn) { int length = txtTxtIn.length(); String pigLatin = ""; for (int b = 0; b < length; b++) { int space = txtTxtIn.indexOf(" "); String word = txtTxtIn.substring(0, space); int a = word.indexOf("a"); int e = word.indexOf("e"); int i = word.indexOf("i"); int o = word.indexOf("o"); int u = word.indexOf("u"); int vowels[] = {a, e, i, o, u}; for (int c = 0; c < 4; c++) { if (vowels[c] == -1) vowels[c] += 101; } for (int d = 0; d < 4; d++) { if (vowels[d] > -1 && vowels[d] <= vowels[0] && vowels[d] <= vowels[1] && vowels[d] <= vowels[2] && vowels[d] <= vowels[3] && vowels[d] <= vowels[4]) { word = word.substring(vowels[i] + 1) + word.substring(0,vowels[i]); pigLatin += " " + word; } } /*if (a != -1 && a < e && a < i && a < o && a < u) { word = word.substring(a + 1) + word.substring(0, a); pigLatin += " " + word; } else if (e != -1 && e < a && e < i && e < o && e < u) { word = word.substring(e + 1) + word.substring(0, e); pigLatin += " " + word; } else if (i != -1 && i < e && i < a && i < o && i < u) { word = word.substring(i + 1) + word.substring(0, i); pigLatin += " " + word; } else if (o != -1 && o < e && o < i && o < a && o < u) { word = word.substring(o + 1) + word.substring(0, o); pigLatin += " " + word; } else if (u != -1 && u < e && u < i && u < o && u < a) { word = word.substring(u + 1) + word.substring(0, u); pigLatin += " " + word; }*/ txtTxtIn = txtTxtIn.substring(space + 1); } return pigLatin; } } The error only occurs when you press the button. Making the Program crash. Here's the error: FATAL EXCEPTION: main Process: com.example.ashavolian.piglatin2, PID: 2991 java.lang.StringIndexOutOfBoundsException: length=0; regionStart=0; regionLength=-1 at java.lang.String.startEndAndLength(String.java:298) at java.lang.String.substring(String.java:1087) at com.example.ashavolian.piglatin2.MainActivity.translate(MainActivity.java:39) at com.example.ashavolian.piglatin2.MainActivity$1.onClick(MainActivity.java:27) at android.view.View.performClick(View.java:5198) at android.view.View$PerformClick.run(View.java:21147) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Please take a look if you can, thanks. -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscr...@googlegroups.com. To post to this group, send email to android-developers@googlegroups.com. Visit this group at https://groups.google.com/group/android-developers. To view this discussion on the web visit https://groups.google.com/d/msgid/android-developers/5f83c294-bdb5-4cd6-9d63-7b2ef7022f44%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.