wood burning stoves 2.0*
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
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: 41525
    
  53
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 - my free Android networking tools app
 
 
subject: java awt if statement not working properly