If without else
hi, i am trying to create a drinks machine with 8 checkboxesand 2 textfields to display the drink chosen and the price. My question is when i come to the if statements i am getting an if without else error.I will post my code if some could just see if i am on the right lines i don't want the code changing, I would like to know if i am doing it right and if not where i am going wrong, i am a novice who is wanting to learn.
thankyou.
ps. sorry for not using code tags i tried to edit but was unable.
public void itemStateChanged(ItemEvent ie)
{
if(ie.getSource()==tea)
{
if(tea.getState==true)
{
tf1.setText("TEA");
}
else
{
tf1.setText("");
}
if(ie.getSource()==coffee)
{
if(coffee.getState()==true)
{
tf1.setText("COFFEE");
}
tf1.setText("");
}
if(ie.getSource()==cocoa)
{
if(cocoa.getState()==true)
{
tf1.setText("COCOA");
}
else
tf1.setText("");
{
if(ie.getSource()==chocolate)
{
if(chocolate.getState()==true)
{
tf1.setText("CHOCOLATE");
}
}
Message was edited by:
fowlergod09
Message was edited by:
fowlergod09
Message was edited by:
fowlergod09
Message was edited by:
fowlergod09
> ps. sorry for not using code tags i tried to edit but was unable.
Just highlight the text and click the code button.
I would suggest formatting your code (if it isn't already) and making sure you have opening and closing parenthesis and that your ifs and elses are in the correct places.
plece { after else here
if(cocoa.getState()==true)
{
tf1.setText("COCOA");
}
else
tf1.setText("");
{
I have formatted the code and some of the parenthesis are missing.... review the code and be sure that every if and every else have opening and closing parenthesis...
And one tip: you can reduce else statements by setting tf1.setText(""); at the begginging of the method and than only pick correct drink name....(in else statements you have always the same code)
I have made sure that the braces are correct i am now getting the following error:
addItemListener(java.awt.event.ItemListener) in java.awt.Checkbox cannot be applied to (Drinksgui)
i am correct using item Listeners for checkboxes aren't i? or should i be using ActionListeners?
Is Drinksgui an ItemListener? I assume from your error that it is not.
I think you were missing some brackets. Also when comparing strings in Java you do not use the ?=?
if (ie.getSource().eqauls("tea")) {}
public void itemStateChanged(ItemEvent ie) {
if (ie.getSource() == tea) {
if (tea.getState == true) {
tf1.setText("TEA");
}
else {
tf1.setText("");
}
}
else if (ie.getSource() == coffee) {
if (coffee.getState() == true) {
tf1.setText("COFFEE");
}
tf1.setText("");
}
if (ie.getSource() == cocoa) {
if (cocoa.getState() == true) {
tf1.setText("COCOA");
}
else {
tf1.setText("");
}
}
else if (ie.getSource() == chocolate) {
if (chocolate.getState() == true) {
tf1.setText("CHOCOLATE");
}
}
}
I think you were missing some brackets. Also when comparing strings in Java you do not use the ?=?
I do not understand that statement?
if (ie.getSource().eqauls("tea")) {}
are you suggesting using the above instead of using "=="
Message was edited by:
fowlergod09
I am sorry. I was trying to help 3 people at once and got confused. I did not follow my rule of never posting first thing in the morning when I am not thinking straight.
> I think you were missing some brackets. Also when
> comparing strings in Java you do not use the
> ?=?
>
> I do not understand that statement?
>
> if (ie.getSource().eqauls("tea")) {}
> are you suggesting using the above instead of using
> "=="
To be clear, the following is correct for your application:
if (ie.getSource() == tea)
getSource() would never be compared to a String.
You might be able to do something like:
public void itemStateChanged(ItemEvent ie)
{
Checkbox pressed = (Checkbox)ie.getSource();
if(pressed.getState())
{
tf1.setText(pressed.getLabel().toUpperCase());
}
}
Then you don't need all of those if's and else's.
public void itemStateChanged(ItemEvent ie)
{
Checkbox pressed = (Checkbox)ie.getSource();
if(pressed.getState())
{
tf1.setText(pressed.getLabel().toUpperCase());
}
}
i think i understand except for the UpperCase(); part and also i have 2 textfields i for drinks and one for sugar ect so how can i make the last statement append to the correct textfield
i have amended the code and now i am getting the following error message: cannot find symbol variable getState.
this is my full code.
/**
* @(#)Drinksgui.java
*
* Drinksgui application
*
* @author
* @version 1.00 2007/1/2
*/
iimport java.awt.*;
import java.awt.event.*;
class Drinksgui extends Frame implements ItemListener
{
private Panel tPanel,bPanel,lPanel,rPanel;
private Checkbox tea,coffee,cocoa,chocolate,sugar,cream,ffmilk,semi;
private TextField tf1,tf2;
private Label title;
public Drinksgui()
{
setLayout(new BorderLayout());
setBackground(Color.cyan);
tPanel=new Panel();
bPanel=new Panel();
lPanel=new Panel();
rPanel=new Panel();
tPanel.setLayout(new FlowLayout());
tPanel.setBackground(Color.red);
bPanel.setLayout(new FlowLayout());
bPanel.setBackground(Color.red);
lPanel.setLayout(new GridLayout(4,1,0,0));
lPanel.setBackground(Color.GREEN);
rPanel.setLayout(new GridLayout(4,1,0,0));
rPanel.setBackground(Color.green);
tea=new Checkbox("TEA");
tea.addItemListener(this);
coffee=new Checkbox("COFFEE");
coffee.addItemListener(this);
cocoa=new Checkbox("COCOA");
cocoa.addItemListener(this);
chocolate=new Checkbox("DRINKING CHOCOLATE");
chocolate.addItemListener(this);
sugar=new Checkbox("SUGAR");
sugar.addItemListener(this);
ffmilk=new Checkbox("FULL FAT MILK");
ffmilk.addItemListener(this);
semi=new Checkbox("SEMI-SKIMMED MILK");
semi.addItemListener(this);
cream=new Checkbox("CREAM");
cream.addItemListener(this);
tf1=new TextField(20);
tf2=new TextField(10);
title=new Label("DRINKS MACHINE");
tPanel.add("Center",title);
bPanel.add("East",tf1);
bPanel.add("West",tf2);
rPanel.add(tea);
rPanel.add(coffee);
rPanel.add(cocoa);
rPanel.add(chocolate);
lPanel.add(sugar);
lPanel.add(cream);
lPanel.add(ffmilk);
lPanel.add(semi);
add("North",tPanel);
add("South",bPanel);
add("East",rPanel);
add("West",lPanel);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
});
}
public static void main(String[] args)
{
Drinksgui mainFrame=new Drinksgui();
mainFrame.setSize(600,600);
mainFrame.setTitle("DRINKS GUI");
mainFrame.setVisible(true);
}
public void itemStateChanged(ItemEvent ie) {
if (ie.getSource() == tea) {
if (tea.getState == true) {
tf1.setText("TEA");
}
else {
tf1.setText("");
}
}
else if (ie.getSource() == coffee) {
if (coffee.getState() == true) {
tf1.setText("COFFEE");
}
tf1.setText("");
}
if (ie.getSource() == cocoa) {
if (cocoa.getState() == true) {
tf1.setText("COCOA");
}
else {
tf1.setText("");
}
}
else if (ie.getSource() == chocolate) {
if (chocolate.getState() == true) {
tf1.setText("CHOCOLATE");
}
}
else if (ie.getSource() == sugar) {
if (sugar.getState() == true){
tf2.setText("+SUGAR");
}
else {
tf2.setText("");
}
}
else if (ie.getSource() == cream){
if (cream.getState() == true){
tf2.setText("+CREAM");
}
}
else if (ie.getSource() == ffmilk){
if (ffmilk.getState() == true){
tf2.setText("+FULL FAT MILK");
}
else {
tf2.setText("");
}
}
else if (ie.getSource() == semi){
if (semi.getState()==true){
tf2.setText("+SEMI-SKIMMED MILK");
}
else {
tf2.setText("");
}
}
}
}
Message was edited by:
fowlergod09
public void itemStateChanged(ItemEvent ie) {if (ie.getSource() == tea) {if (tea.getState == true) {tf1.setText("TEA");}else {tf1.setText("");}missing () by getState.....it should be getState() not only getState
> i think i understand except for the
> UpperCase(); part and also i have 2 textfields
> i for drinks and one for sugar ect so how can i make
> the last statement append to the correct textfield
You don't need the upper case if your labels are already upper case. So, just ignore that part.
As for one for drinks and one for additives (sugar, etc.), you should have two ItemListener classes. One for drinks can be as above and set tf1. One for additives can be as above, except set tf2. If they can have more than one additive at a time, you'll need to append to the text field, rather than using setText.
And for your "getState" error, you are missing the parentheses on your method call for tea.getState().
Get rid of all of those "== true". They are redundant. Just use:
if (tea.getState())
thanyou doremeforsolatidoyou have been very helpful.
> thanyou doremeforsolatido
> you have been very helpful.
You're welcome. Is your GUI being displayed correctly? The following code looks wrong:
tPanel.add("Center",title);
bPanel.add("East",tf1);
bPanel.add("West",tf2);
yes it is i used the "Center" as the title was displayed to the left of the panel ,now it is in the middle. It may be more by good luck than good management. I also had to add a couple of else statements as when i unchecked a couple of checkboxes thetextfield still displayed the text.I had forgot to add:
else
{
tf2.setText("");
}
thanks mate
glenn
> yes it is i used the "Center" as the title was
> displayed to the left of the panel ,now it is in the
> middle. It may be more by good luck than good
> management.
Okay. Yes, it is more good luck than good management. The 'add' method you used is:
[url]http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Container.html#add(java.lang.String, java.awt.Component)[/url]
It is obsolete. The link shows you what you should use instead:
[url]http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Container.html#add(java.awt.Component, java.lang.Object)[/url]
Make sure that the second parameter you pass to that method (the "constraints") matches the layout (e.g., BorderLayout) of the Container to which you are adding the component.
Why you don't put name to tea, coffee, cocoa, etc. and use more simple code?:
...
tea.setText("TEA");
coffee.setText("COFFEE");
...
public void itemStateChanged(ItemEvent ie) {
AbstractButton src;
if (src instanceof AbstractButton) {
src=ie.getSource()
tf1.setTexty(src.getState()?src.getText();"");
}
}
This code is simplest and more reusable.
> Why you don't put name to tea, coffee, cocoa, etc.> and use more simple code?:Already suggested (Reply #10). No need to test "instanceof" if the ItemListener is only attached to the appropriate Checkbox(es). A Checkbox isn't an AbstractButton, either.
Why you don't put name to tea, coffee, cocoa, etc. and use more simple code?:
...
tea.setText("TEA");
coffee.setText("COFFEE");
...
public void itemStateChanged(ItemEvent ie) {
Checkbox src=(CheckBox)ie.getSource()
tf1.setTexty(src.getState()?src.getText();"");
}
This code is simplest and more reusable.
For use BorderLayout, you don't should use String, use constants:
p.add(/*Component*/, BorderLayout.CENTER); //Add component to center.
http://java.sun.com/javase/6/docs/api/java/awt/BorderLayout.html
> For use BorderLayout, you don't should use String, use constants:
Also already suggested. In this case, the Strings are treated as names, not as locations. Please read the thread before you repeat the same advice (unless the OP has obviously ignored it--in this case, he hasn't answered anything since the advice was posted).
I appreciate the advice but i do not understand what src instance of means.
With regards to the person who posted the links, thankyou i will study them as i really am finding it difficult to master the if/else statements.
the code now maybe obselete but it is the way that they are teaching it at leeds met university. I am in my first year and i am finding it difficult because we are being taught a certain way, but when it comes to the assignments when i am researching points i do not fully understand when i google on the internet everything is different so i end up becoming more confused than i was before i started.
Message was edited by:
fowlergod09
instanceof explanation:
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/op2.html
In short, the code (which should have been "e.getSource() instanceof AbstractButton") says "Is e.getSource() an AbstractButton? Can I cast e.getSource() to an AbstractButton without getting a ClassCastException?"
instanceof is not really needed for your code right now.
Your university probably shouldn't be teaching obsolete methods. I'll show you the newer way to do it:
Since your Frame has a BorderLayout, the following four lines:
add("North",tPanel);
add("South",bPanel);
add("East",rPanel);
add("West",lPanel);
should be:
add(tPanel, BorderLayout.NORTH);
add(bPanel, BorderLayout.SOUTH);
add(rPanel, BorderLayout.EAST);
add(lPanel, BorderLayout.WEST);
with your original code, you would likely get different results if you rearranged the order of the 'add' calls. If you do it with the non-obsolete 'add' method and specify the BorderLayout constraints correctly, the order won't matter.
tPanel has a FlowLayout. So:
tPanel.add("Center",title);
should be simply (no constraints):
tPanel.add(title);
Same with bPanel--it has a FlowLayout, so you don't need constraints:
bPanel.add("East",tf1);
bPanel.add("West",tf2);
should be:
bPanel.add(tf1);
bPanel.add(tf2);
The order of the 'add' calls for a FlowLayout does matter because the components will be displayed in the order added.
When you use 'if'/'else'/'for'/'while'/etc., always use the { and }, even if you currently only have one statement associated with them. Then, if you add more statements that need to be controlled by them, you won't forget to add { }, and your bug may be harder to find.
thanks a lot for that doremifasollatido i shall be asking why i am paying 4,000 per year to be taught obselete methods. The way you showed me is much simpler and saves a lot of tpying.Message was edited by: fowlergod09
> thanks a lot for that doremifasollatido i shall be
> asking why i am paying 4,000 per year to be taught
> obselete methods. The way you showed me is much
> simpler and saves a lot of tpying.
You're welcome. Definitely a good idea to ask that question about why they are teaching obsolete methods. The new way will definitely help when you try to make more complex GUIs, because you will be able to use LayoutManagers (like BorderLayout) correctly, instead of just hoping that they work by luck.