0

I am trying to convert String to array then to an Integer,i made code working successfully in java ,but in android studio the array the i converted don't take the last value of array covered from string

   String y="abc";
    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];
    }

sum variable always dont count the last value of conversation ,it should be 6 but i get 3

Janki Gadhiya
  • 4,492
  • 2
  • 29
  • 59

4 Answers4

0

You should define "sum" outside your loop. To keep the value.

String y="abc";
String[] array = y.split("");
int[] x = new int[y.length()];
int sum = 0; 

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 += x[i];
}
Rene M.
  • 2,660
  • 15
  • 24
  • Thanks for answer but i already did this after declaring method, always cannot read the last value i array ,but same code is working fine in eclipce –  Jun 29 '16 at 09:12
0

Change your code to:

int sum = 0;
String str = "abc";
char letter = ' ';
char[] charArray= str.toCharArray();  //or use str.split (to a string array)

for (int i=0; i < str.length(); i++)
{
    letter = charArray[i];
    if(letter == 'a'){ sum += 1; }
    else if(letter == 'b'){ sum += 2; }
    else if(letter == 'c'){ sum += 3; }
}

This will split the string into an charArray and will than compare the character with the corresponded letter, and will perform the calculation based on the check.

Check this for the result

Good luck!

Strider
  • 4,452
  • 3
  • 24
  • 35
-1

Update

The below answer is based on Android Studio debugger output. Seems a weird issue.

array will be {"","a","b","c"} with size 4 & hence for loop skips the last position since y.length()=3. Try

int[] x = new int[array.length];

See the answer to this question Split string into array of character strings & Why in Java 8 split sometimes removes empty strings at start of result array? & for more reference

Community
  • 1
  • 1
Sujay
  • 3,417
  • 1
  • 23
  • 25
-1

Solved the Android Studio issue by :

string.split("(?!^)")

For complete reference see below code :

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String y="abc";
    int sum=0;
    String[] array = y.split("(?!^)");
    int[] x = new int[array.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];
    }
    System.out.println("value of sum : "+sum+"");
}

And this time definitely sum is having value 6.

Janki Gadhiya
  • 4,492
  • 2
  • 29
  • 59
  • I told you it is working fine in java but in android studio app not working ,anyways i found solution it is woking fine now –  Jun 29 '16 at 09:25
  • It's in android studio you can see the user interface.!! By the way what was the problem ?? – Janki Gadhiya Jun 29 '16 at 09:26
  • Layth - Note: Java is a programming Language, while Android Studio is an IDE (Integrated Development Environment). – Enzokie Jun 29 '16 at 09:27
  • public void start(View v) { int sum = 0;int sum1= 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 –  Jun 29 '16 at 09:30
  • check full code in andoid making app and you wil see the result –  Jun 29 '16 at 09:34
  • just put it aftre oncreate{}methode ,then create edittext and textview in layout –  Jun 29 '16 at 09:36