1

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" />

}
5
  • Your loop seems to be ok. Can you look at y.length result with a log ? Maybe the split didn't work correctly with "" ? Also try with "a b c" and split(" ") if the length actually equals 2 or the split is not right. Put a log on sum too in your loop and show us the result :) Commented Jun 29, 2016 at 12:06
  • try array.length() instead of y.length() Commented Jun 29, 2016 at 12:09
  • I have tried array.length didnt work same results Commented Jun 29, 2016 at 12:14
  • I tried also to input "a" then show the result from first if but it shows 0 Commented Jun 29, 2016 at 12:15
  • if i put space after the input it will work fine but it is not what i want Commented Jun 29, 2016 at 12:16

2 Answers 2

2

If you would like to stick to a String array, to properly split your String to characters, instead of the following:

String[] array = y.split("");

You should do something like this:

String[] array = y.split("(?!^)");

Splitting with "" as the regex will result in an empty string in your array at index 0.

Another option is to use a char array instead:

int sum = 0

EditText name = (EditText)findViewById(R.id.editText);
TextView result  = (TextView)findViewById(R.id.textView);

String y = name.getText().toString();

char[] array = y.toCharArray();
int[] x = new int[y.length()];

for (int i = 0; i < y.length(); i++) {
    char letter = array[i];
    if (letter == 'a') {
        x[i] = 1;
    }
    if (letter == 'b') {
        x[i] = 2;
    }
    if (letter == 'c') {
        x[i] = 3;
    }

    sum = sum + x[i];
}

result.setText(Integer.toString(sum));
Sign up to request clarification or add additional context in comments.

Comments

0

One more solution to the above problem

int charTotal =0;
String input=editText.getText.toString().trim();
final static String alphabet="abcdefghijklmnopqrstuvwxyz";
for(int i=0;i<input.length();i++){
 charTotal = charTotal + alphabet.indexOf(input.charAt(i))+1;}
result.setText(String.valueOf(charTotal))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.