The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Java error

Hi all,

Have an issue with my code below where it is giving me the wrong output. Essentially the code is to scan a word entered to see if it contains vowels. Each vowel(regardless of location in the word) has a value, a = 1, e = 2, i = 3, o = 4, u = 5.

My code is giving me widly different values to what I was expecting. Can anyone see where I am going wrong with it?

Re: Java error

if ((word.indexOf(a)!=-1))

This is not the right way to check through the string for a letter. What you're actually doing is checking the index of the letter you are looking for (in this case "a") and seeing if the position isn't at -1. Which will always be true in this case.

Re: Java error

You haven't said what was wrong with the code the OP is using.
In general I think using variables is a better way to code than hardcoding literals in the code.
One fault I find is using too short a variable name: a