roshanpeter
roshanpeter

Reputation: 1364

Android searching for a character in a string

I am developing a programme .. in android where a name is entered in edittext and vowels are searched in it. suppose in name Roshan vowels are present and ie o&a and from which the first vowel has to be displayed I got the code for displaying the first vowel and the programme is working fine.. but the programme is displaying the vowel only when there is more than one vowel ..if there is only one vowel that letter is not displaying.. pls check the code and if found any mistake pls help..

public char gReport(View V) 
            {
                et1 = (EditText) findViewById (R.id.editText1);
                et2 = (EditText) findViewById (R.id.editText2);
                et3 = (EditText) findViewById (R.id.editText3);
                TextView tv1 = (TextView) findViewById (R.id.textView1);


                String str = et1.getText().toString();


                int a=str.indexOf('a');
                int b=str.indexOf('e');
                int c=str.indexOf('i');
                int d=str.indexOf('o');
                int e=str.indexOf('u');

                if(a!=-1 && a<(b&c&d&e)) 
                {
                    tv1.setText("a");
                }
                else if(b!=-1 && b<(a&c&d&e)) 
                    {

                    tv1.setText("e");

                    }
                else if(c!=-1 && c<(a&b&d&e)) 
                {

                tv1.setText("i");

                }
                else if(d!=-1 && d<(a&c&b&e)) 
                {

                tv1.setText("o");

                }
                else if(e!=-1 && e<(a&c&d&b)) 
                {

                tv1.setText("u");

                }
                return 0;



    }

Upvotes: 1

Views: 1808

Answers (4)

CRUSADER
CRUSADER

Reputation: 5472

to find first occurance of vowel

String str = et1.getText().toString();

       outer : for (i = 0; i < str.length(); i++)
        {
        switch(s.charAt(i)) {
    case 'a':
    case 'e':
    case 'i':
    case 'o':
    case 'u':
        tv1.setText(s.charAt(i));
        break outer;
        }

or like this..

for (i = 0; i < str.length(); i++)
            {
if(StringUtils.isVowel(str,i)){
 tv1.setText(s.charAt(i));
            break;
}
}

Upvotes: 2

Scott Helme
Scott Helme

Reputation: 4799

You have a flaw in your logic.

if(a!=-1 && a<(b&c&d&e)) 

You're saying, if a != -1 - this means it found the vowel in the string.

Followed by, && a<(b&c&d&e) - you're trying to find if it's the lowest index.

What if b, c, d, and e are -1. a will never be less than them. This is why it does not work when there is only 1 character found.

NitroNbg beat me to the answer, I've made a comment on there and that should resolve your problem.

Upvotes: 2

Armaan Stranger
Armaan Stranger

Reputation: 3130

You can Try this::

In java you have one method that used for searching particular string in your given string.

YourString.contains("CheckString");

For checking First vowel just find the minimum number from the array and then get substring from the main string.

char[] a = {'3', '5', '1', '4', '2'};

List b = Arrays.asList(ArrayUtils.toObject(a));

System.out.println(Collections.min(b));

Hope it Helps!!

Upvotes: 1

nstosic
nstosic

Reputation: 2614

In your second if condition I assume you want to check if the current vowel index is smaller than all the others. You can't do it the way you wrote. You have to explicitly write for each case:

if((a!=-1) && (a<b) && (a<c) && (a<d) && (a<e))

however, this won't work since your index will be greater than the erronous index (-1). So, you have to include a temporary integer value to check the minimum of the indexes. Try this:

int min = str.length();
if(a!=-1 && a<min) {
    tv1.setText("a");
    min=a;
}
if(b!=-1 && b<min) {
    tv1.setText("e");
    min=b;
}
if(c!=-1 && c<min) {
    tv1.setText("i");
    min=c;
}
if(d!=-1 && d<min) {
    tv1.setText("o");
    min=d;
}
if(e!=-1 && e<min) {
    tv1.setText("u");
    min=e;
}

Upvotes: 1

Related Questions