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 6 of 6

Thread: Basic Java Problem

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

    Default Basic Java Problem

    Hello all,

    Currently studying java in college for first time and cannot see where my error is in the following code, could some one with a lot more experience help me out?

    import javax.swing.JOptionPane;
    public class Week7Q1
    {
    public static void main(String [] agrs)
    {
    String word = JOptionPane.showInputDialog(null, "Please enter a word");
    String vowels = "a,e,i,o,u";
    String consonant = "b,c,d,f,g,h,j,k,l,m,n,p,q,r,s,t,v,w,x,y,z";
    String aChar;
    int length = word.length();
    int i = 0;
    int vc = 0;
    int cc = 0;
    String wordCopy = word;
    word = word.toLowerCase();
    for (i=0;i<length;i=i+1)
    {
    aChar = word.substring(i,i=i+1);
    if (vowels.indexOf(aChar)!=-1)
    vc++;
    else
    if(consonant.indexOf(aChar) !=-1)
    cc++;
    }
    JOptionPane.showMessageDialog(null, "There are " + vc + " vowel(s) and " + cc + " consonant(s) in " + wordCopy);
    }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,827
    My Mood
    Mellow
    Thanks
    177
    Thanked 628 Times in 615 Posts

    Default Re: Basic Java Problem

    Please read the Announcement topic at the top of the sub-forum to learn how to post code correctly and other useful items for new comers.

    What error? Please describe what is happening or not happening that you need help with.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    digitalsystems (October 29th, 2013)

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

    Default Re: Basic Java Problem

    Thanks, I will go through the announcement topic now.

    On this code, I am not getting the correct output for this program, for example, if I enter the word "Today", I get a response telling me there are 0 vowel(s) and 3 consonant(s) in Today. It looks like the program is not counting the vowels in the word entered.

    --- Update ---

    figured out myself where my error was, code below seems to be working for me. thanks

    [highlight=Java]

    import javax.swing.JOptionPane;
    public class Week7Q1
    {
    public static void main(String [] agrs)
    {
    String word = JOptionPane.showInputDialog(null, "Please enter a word");
    String vowels = "a,e,i,o,u";
    String consonant = "b,c,d,f,g,h,j,k,l,m,n,p,q,r,s,t,v,w,x,y,z";
    String aChar;
    int length = word.length();
    int i = 0;
    int vc = 0;
    int cc = 0;
    String wordCopy = word;
    word = word.toLowerCase();
    for (i=0;i<length;i=i+1)
    {
    aChar = word.substring(i,i+1);
    if (vowels.indexOf(aChar)!=-1)
    vc++;
    else
    if(consonant.indexOf(aChar) !=-1)
    cc++;
    }
    JOptionPane.showMessageDialog(null, "There are " + vc + " vowel(s) and " + cc + " consonant(s) in " + wordCopy);
    }
    }

    [/highlight

  5. #4
    Junior Member
    Join Date
    Oct 2013
    Posts
    18
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Basic Java Problem

    Quote Originally Posted by digitalsystems View Post
    Thanks, I will go through the announcement topic now.

    On this code, I am not getting the correct output for this program, for example, if I enter the word "Today", I get a response telling me there are 0 vowel(s) and 3 consonant(s) in Today. It looks like the program is not counting the vowels in the word entered.
    It does.
    Bt you have chosen to skip some letters by an instruction.
    aChar = word.substring(i,i=i+1);
    i.e. i is incremented twice before it is used.
    So u may replace the above with:
    aChar = word.substring(i,i+1);
    But I suggest u use:
    aChar = word.charAt(i);
    for better convenience.[COLOR="Silver"]
    Last edited by aba muhammad; October 29th, 2013 at 12:45 PM. Reason: mistaken

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

    Default Re: Basic Java Problem

    Thanks for your help, realised my error shortly after I posted it.

  7. #6
    Junior Member
    Join Date
    Oct 2013
    Posts
    18
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Basic Java Problem

    ur welcome.

Similar Threads

  1. [SOLVED] Basic Code Problem
    By msinc210 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: June 3rd, 2012, 04:08 PM
  2. Basic Math Expression Java Problem
    By andyluvskrissy in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 15th, 2011, 02:22 AM
  3. Very Basic Java input Problem
    By coke32 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 9th, 2011, 06:54 PM
  4. Basic Math Expression Java Problem
    By andyluvskrissy in forum Object Oriented Programming
    Replies: 3
    Last Post: September 30th, 2010, 02:46 PM
  5. Basic Java File I/O with Scanner Class Problem
    By miss confused in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 26th, 2010, 08:04 AM