I am trying to convert a string to an array, then to an integer by summing values based on the letters in the array. The problem is every time I make the Android app, the app cannot read the last value of array. The same code is working fine in Eclipse.
For example, if you put "abc", you will get 3, but it should be 6.
public void start(View v) {
int sum = 0
EditText name = (EditText)findViewById(R.id.editText);
TextView result = (TextView)findViewById(R.id.textView);
String y = name.getText().toString();
String[] array = y.split("");
int[] x = new int[y.length()];
for (int i = 0; i < y.length(); i++) {
String letter = array[i];
if (letter.equals("a")) {
x[i] = 1;
}
if (letter.equals("b")) {
x[i] = 2;
}
if (letter.equals("c")) {
x[i] = 3;
}
sum = sum + x[i];
}
result.setText(Integer.toString(sum));
}
Layout:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:inputType="text"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_marginTop="48dp"
android:layout_below="@+id/editText2"
android:layout_centerHorizontal="true"
android:onClick="start" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView"
android:minHeight="50dp"
android:layout_below="@+id/button"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
}