File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes  java awt if statement not working properly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark " java awt if statement not working properly" Watch " java awt if statement not working properly" New topic
Author

java awt if statement not working properly

Vaibhav Kaushal
Greenhorn

Joined: Jun 25, 2013
Posts: 2
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();
}
}
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39544
    
  27
Strings are not compared using the "==" operator, but using the "equals" method.

Start by rewriting your code to use

char vowels[]={'a','e','i','o','u'}

instead of

String vowels[]={"a","e","i","o","u"}

If you do that, note that char is compared using "==", unlike String.

Once you got all this working, ask yourself whether é is a vowel for your purposes :-)


Ping & DNS - updated with new look and Ping home screen widget
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java awt if statement not working properly
 
Similar Threads
how to add functionality in dialog window
problem regarding loop in buttons
Teacher said This is not a correct code
how to subtract decimal????
Focussing a TextField