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?
import javax.swing.JOptionPane; public class W7Q11 { public static void main (String [] agrs) { String word = JOptionPane.showInputDialog(null, "Please enter a word"); String wordCopy = word; word = word.toLowerCase(); String a = "a"; String e = "e"; String i = "i"; String o = "o"; String u = "u"; int s1 = 1; int s2 = 2; int s3 = 3; int s4 = 4; int s5 = 5; int countersa=0; int counterse=0; int countersi=0; int counterso=0; int countersu=0; int length = word.length(); for(int index = 0;index<length;index++) { if ((word.indexOf(a)!=-1)) countersa++; else if((word.indexOf(e)!=-1)) counterse++; else if((word.indexOf(i)!=-1)) countersi++; else if((word.indexOf(o)!=-1)) counterso++; else if((word.indexOf(u)!=-1)) countersu++; else JOptionPane.showMessageDialog(null, "You have not entered a valid word"); } JOptionPane.showMessageDialog(null, "The word points for the word you enter are: " + ((s1*countersa)+(s2*counterse)+(s3*countersi)+(s4*counterso)+(s5*countersu))); } }