0

I have a 3 class program, and I need to know the first vowel in a String.

This String has his getter and setter methods. I tried to use by example:

obj.getnameString().charAt(i);

where (i) is a value previously obtained. But this throws a null value. why?

ChiefTwoPencils
  • 13,548
  • 8
  • 49
  • 75
  • Can you attach your code? We won’t be of any help without it. – Logan May 13 '18 at 22:56
  • Please add your code and clarify. `charAt` doesn't throw a null value or exception. If you mean you're getting a `NullPointerException` then it's from something else bu we cannot tell. – ChiefTwoPencils May 13 '18 at 23:26
  • 1
    A program throws **exceptions**, not values. If you mean a `NullPointerException` is thrown from this line it means that either `obj` is `null` or `obj.getnameString()` is `null`. – SHG May 13 '18 at 23:26

1 Answers1

0

It's hard to say if you don't provide any code, especially to say where the null value comes from. But from how I understand it, you can just iterate through the String and check each char if it's a vowel?

Something like this:

String str = obj.getnameString();
char firstVowel;    

for(int i = 0; i < str.length(); i++){
    if(str.charAt(i) == "a" || str.charAt(i) == "o" || ...... || str.charAt(i) == "U"){ //pretty sure theres an easier solution for that
        firstVowel = str.charAt(i);
        break;
    }
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
CrystalRain
  • 72
  • 10