Josh Herber
Josh Herber

Reputation: 19

Why do I keep getting an error in this Java Code?

I'm writing a program that lets the user select a type of currency, then when they enter in the number and click the 'convert button' the conversion is displayed in a text box. But I keep getting an error on line 36 that says 'class or interface expected public void init ()'

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

public class CurrencyConversionApplet implements ActionListener, ItemListener
{

// Variables

     double  dollars, pounds, euros, ruble, price;

     Image   dollarSign;



    Label lblTitle = new Label ("Enter the dollar amount (do not use commas or dollar signs): ");
    Label lblOutput = new Label (" ");
    TextField txtDollar = new TextField(10);
    Button convButton = new Button("Convert");

    CheckboxGroup chkGroup = new CheckboxGroup();
          Checkbox chkPounds = new Checkbox("British Pounds",false,chkGroup);
          Checkbox chkEuro = new Checkbox("Euros",false,chkGroup);
          Checkbox chkRuble = new Checkbox("Russian Ruble",false,chkGroup);
          Checkbox hiddenBox = new Checkbox("",true,chkGroup);
     Image dollarSign;
}

     public void init()

        {


      add(lblTitle);
      add(txtDollar);
      add(convButton);
      add(chkPounds);
      add(chkEuro);
      add(chkRuble);
          chkPounds.addItemListener(this);
          chkEuro.addItemListener(this);
          chkRuble.addItemListener(this);

      dollarSign=getImage(getDocumentBase(), "dollar.jpg");

      setBackground(Color.blue);
      setForeground(Color.yellow);

        convButton.addActionListener(this);

    }

          public void paint(Graphics g) {
          g.drawImage(dollarSign, 0, 28, this);

    }


 public void itemStateChanged(ItemEvent choice)
      {

             dollars = Double.parseDouble(txtDollar.getText());
             pounds = dollars * .62
             euros =  dollars * .71
             ruble = dollars * .03


     if(chkPounds.getState())
        price = pounds;

     if(chkEuro.getState())
        price = euros;

     if(chkRuble.getState())
        price = ruble;

    }

        public void actionPerformed(ActionEvent e)
                    {



lblOutput.setText(Double.toString(price));

}

Upvotes: 0

Views: 491

Answers (6)

ypercubeᵀᴹ
ypercubeᵀᴹ

Reputation: 115660

You have the init() method defined outside of class CurrencyConversionApplet. Is that what you want?

The error 'class or interface expected public void init ()' says it all: The compiler expects there either a class or an interface. And init() is none of these.

Upvotes: 4

Mahesh
Mahesh

Reputation: 34665

Because the method init() needs to be part of CurrencyConversionApplet . So, do this instead -

 Image dollarSign;
 } // <-  Remove that and place it at the very end of the program.

With that corrected, there are other mistakes too -

 pounds = dollars * .62
 euros =  dollars * .71
 ruble = dollars * .03

All the above statements of itemStateChanged method should be ended by a ;

Upvotes: 2

AlfaTeK
AlfaTeK

Reputation: 7775

Shouldn't you delete that '}' ?

Image dollarSign;
}

Upvotes: 3

Matt
Matt

Reputation: 14551

You're getting that error because the method public void init() is not inside the class CurrencyConversionApplet.

Upvotes: 2

Ken White
Ken White

Reputation: 125757

If I'm reading it right, you have an extra } just before your declaration line for Init, closing the class declaration.

     Image dollarSign;
} /* <-- */

     public void init()

Upvotes: 2

allthenutsandbolts
allthenutsandbolts

Reputation: 1523

I don't see that you extend the Applet Class. If this is an applet then you should extend the Applet class

Upvotes: 0

Related Questions