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: java awt...if statement not working properly

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation java awt...if statement not working properly

    it is a program to find vowels from tect in awt....but it is not doing so may be some problem in if statement


    import java.awt.*;
    import java.awt.event.*;

    class Test implements ActionListener
    {
    Frame fr;

    TextField tf0,tf1;
    Button b;
    Test()
    {
    fr=new Frame();
    fr.setLayout(null);
    tf0=new TextField();
    tf1=new TextField();
    tf0.setBounds(30,40,100,20);
    tf1.setBounds(30,80,50,20);
    fr.add(tf0);
    fr.add(tf1);


    b=new Button("Calculate");
    b.setBounds(25,240,100,20);
    fr.add(b);

    fr.setSize(300,300);
    fr.setVisible(true);
    b.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e)
    {
    if(e.getActionCommand().equals("Calculate"));
    {
    int v=0,k=0,l=0;
    String st=tf0.getText();
    String str[]=st.split("");
    String vowels[]={"a","e","i","o","u"};
    for(int i=0;i<str.length;i++)
    {
    for(int j=0;j<=4;j++)
    if(str[i]== vowels[j]) //this statement is giveing output 0 even when vowels are available
    v++;
    }
    String s1=Integer.toString(v);
    tf1.setText(s1);
    }
    }
    public static void main(String s[])
    {
    new Test();
    }
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: java awt...if statement not working properly

    We have no idea what the problem is unless you provide more details.
    String str[]=st.split("");
    Are you sure that line is doing what you want? If you want to break a String into an array of characters then perhaps use toCharArray method instead. Having said that I just realised you are making the common mistake of using == to compare Strings, use the equals method instead.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java awt...if statement not working properly

    yes i am sure that split line is working fine.i have already checked that
    thanks for taking interest in solving the problem
    in case you find the solution please let me know
    thanks again

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: java awt...if statement not working properly

    Did you read the second part of my post?
    Improving the world one idiot at a time!

  5. #5
    Junior Member
    Join Date
    Jun 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java awt...if statement not working properly

    thanks a lot i have just tried the second part and its working fine now
    can you please explain me why this happen??
    i mean why its working by using equals but not ==

    --- Update ---

    [/COLOR]if we use if(str[i].equals("a")) or if(str[i]=="a")
    instead of using a string array its still not working can you please explain me the reason
    Last edited by edward005; June 28th, 2013 at 12:13 AM. Reason: not autosaved

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: java awt...if statement not working properly

    Quote Originally Posted by edward005 View Post
    ...if we use if(str[i].equals("a")) or if(str[i]=="a")
    instead of using a string array its still not working can you please explain me the reason
    What does the documentation say about the use of String.equals? What does it say about using ==? Post any questions you have remaining

Similar Threads

  1. Replies: 2
    Last Post: June 25th, 2013, 06:33 AM
  2. IF statement accumulataion not working properly - random numbers
    By StrugglerWithJava in forum Loops & Control Statements
    Replies: 1
    Last Post: April 4th, 2013, 07:48 AM
  3. Scanner not working properly.
    By kookevk in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 25th, 2012, 10:14 PM
  4. Complete but not working properly
    By Noob101 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 12th, 2011, 03:17 PM
  5. if else statement not working properly
    By tina G in forum Algorithms & Recursion
    Replies: 1
    Last Post: March 29th, 2010, 08:04 AM