Welcome to the Java Programming Forums


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.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Java error

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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?

    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)));
     
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,966
    Thanks
    56
    Thanked 2,357 Times in 2,328 Posts

    Default Re: Java error

    My code is giving me widly different values to what I was expecting.
    Can you copy and paste here some examples of the program's execution that shows what you are talking about.


    BTW With if/else if statements, the execution of any following statements is skipped when the first true condition is found.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java error

    Thanks for the response.

    For example if I input "Hello", I return a value of 10 when I would expect to return a value of 6. Or if I input "Tommy" I get a value of 20 instead of 4. I can't see a consistency in the outputs.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,966
    Thanks
    56
    Thanked 2,357 Times in 2,328 Posts

    Default Re: Java error

    Please add a println statement to the code that prints out the results. The JOptionPane doesn't print on the console so its display can not be copied to the forum.

    Try debugging the code by Printing out the values of all the counters so you can see what the code is doing.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: Java error

    for(int index = 0;index<length;index++) {
        if ((word.indexOf(a)!=-1)) {
    Examine your code very carefully. If user enters the word "hat" how many times will that if statement be true?
    Improving the world one idiot at a time!

  6. #6
    Junior Member
    Join Date
    Nov 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    This is the documentation for : String.indexOf(String).

    Look for other methods that return individual letters in Strings.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,966
    Thanks
    56
    Thanked 2,357 Times in 2,328 Posts

    Default Re: Java error

    This is not the right way to check through the string for a letter.
    @Rend Please explain what is wrong with code you posted. Can you make a small test program to show the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Nov 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java error

     Scanner scan = new Scanner(System.in);
            System.out.println("Enter letter: ");
            a = scan.next();
            int count = 0;
     
            for (int i = 0; i < a.length(); i++) {            
                if (a.substring(i, i + 1).equals("a")) {
                    count++;
                }
            }
            System.out.println(count);
        }

    Here is a sample program I wrote up to count the letter 'a' every time it appears in the word saved as 'a'.

    Documentation for substring(int, int) :

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,966
    Thanks
    56
    Thanked 2,357 Times in 2,328 Posts

    Default 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

    if the position isn't at -1.
    That would mean that the letter was found in word.

    I would probably code it:
    if (word.indexOf(a) >= 0) // does word have the letter a?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: January 17th, 2013, 07:04 AM
  2. Replies: 0
    Last Post: July 2nd, 2012, 10:11 PM
  3. Java Error cannot be applied to (java.lang.String), phone book entry program.
    By iceyferrara in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 23rd, 2011, 07:32 AM
  4. [SOLVED] Java run time error: 'java.lang.NullPointerException"
    By ChandanSharma in forum What's Wrong With My Code?
    Replies: 9
    Last Post: June 20th, 2011, 12:53 PM
  5. Java error
    By d7o0om in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 12th, 2010, 09:14 AM