-
Help, please. I don't understand these error messages.
Hi there :)!
I'm working on a project for school. This is pretty urgent because the project is due tomorrow and I can't figure out what the problem is. The task is to write a simple BMI program in Java. I don't understand a whole lot of Java but with the help of the examples we've been shown in class I was able to get it done...well, more or less, because I keep getting two error messages I don't quite understand when trying to compile one of my files. Here you go:
This is the first class which gets compiled without any errors:
Code:
public class BMI {
private double Bmi = 0.0;
private double gewicht = 0.0;
private double größe = 0.0;
public double neuerBmi = 0.0;
private void setGewicht (double neuesGewicht) {
if (neuesGewicht >= 0.01) {
this.gewicht = neuesGewicht;
}
}
public double getGewicht() {
return this.gewicht;
}
private void setGröße (double neueGröße) {
if (neueGröße >= 0.01) {
this.größe = neueGröße;}
}
public double getGröße() {
return this.größe;
}
public void berechnen () {
if (größe*größe >=0.01) {
neuerBmi = this.gewicht/(this.größe*this.größe);
}
}
private void setBmi (double neuerBmi) {
this.Bmi = this.neuerBmi;
}
public double getBmi() {
return this.Bmi;
}
}
This is the second class:
Code:
import java.awt.*;
import java.awt.event.*;
import java.text.*;
public class BmiFrame extends Frame
implements ActionListener, WindowListener {
private BMI meinBMI = null;
private Label gewichtText = new Label("Gewicht in kg eingeben");
private TextField gewichtEingabe = new TextField("00");
private Label größeText = new Label("Größe in m eingeben");
private TextField größeEingabe = new TextField("00");
private Button berechnenButton = new Button("BMI berechnen");
private Label bmiText = new Label(b);
private GridLayout fensterLayout = new GridLayout (3,2);
private DecimalFormat wertFormat = new DecimalFormat("#,##");
private DecimalFormat bmiFormat = new DecimalFormat("#");
public void setBMI (BMI b) {
this.meinBMI = b;
}
public BMI getBMI() {
return meinBMI;
}
public void init () {
double gewicht = meinBMI.getGewicht();
String gewString = wertFormat.format(gewicht);
gewichtEingabe.setText(gewString);
double größe = meinBMI.getGröße();
String gröString = wertFormat.format(größe);
größeEingabe.setText(gröString);
this.setSize(300,150);
this.setTitle("BMI-Rechner");
this.setLayout(fensterLayout);
this.add(gewichtText);
this.add(gewichtEingabe);
this.add(größeText);
this.add(größeEingabe);
this.add(bmiText);
this.add(berechnenButton);
berechnenButton.addActionListener(this);
this.addWindowListener(this);
}
public void actionPerformed (ActionEvent e) {
String gewichtString = gewichtEingabe.getText().trim();
double gewicht = Double.parseDouble(gewichtString);
String größeString = größeEingabe.getText().trim();
double größe = Double.parseDouble(größeString);
Object angeklickt = e.getSource();
if (angeklickt == berechnenButton) {
meinBMI.berechnen();
double Bmi = meinBMI.getBmi();
String bmiString = bmiFormat.format(Bmi);
bmiText.setText(bmiString);
}
}
public void windowClosing (WindowEvent e) {
this.setVisible(false);
System.exit(0);
}
public void windowClosed (WindowEvent e) { }
public void windowOpened (WindowEvent e) { }
public void windowIconified (WindowEvent e) { }
public void windowDeiconofied (WindowEvent e) { }
public void windowActivated (WindowEvent e) { }
public void windowDeactivated (WindowEvent e) { }
}
And I get these errors:
Code:
Compiliere G:\...\BMI\BmiFrame.java mit Java-Compiler
BmiFrame.java:4:8: error: BmiFrame is not abstract and does not override abstract method windowDeiconified(WindowEvent) in WindowListener
public class BmiFrame extends Frame
^
BmiFrame.java:12:37: error: cannot find symbol
private Label bmiText = new Label(b);
^
symbol: variable b
location: class BmiFrame
2 errors
I'm aware of the fact that I kind of haven't actually told the program what variable b is but this is pretty much the way it's done in the examples we've had in class. So I'm not sure what's wrong about this. There's also the possibility that these are not the only mistakes in my code, so if you see anything else that seems wrong, please do tell me :). I'd be extremely thankful for all the help I could get. Detailed explanations would be nice, as I don't know a whole lot about Java :(blush):.
-
Re: Help, please. I don't understand these error messages.
I read your post but not completely but:
is not abstract and does not override abstract method windowDeiconified(WindowEvent)
compare that with the line 62 of your code:
public void windowDeiconofied (WindowEvent e) { }
:P:
-
Re: Help, please. I don't understand these error messages.
Like I've told you, I don't know a whole lot about Java so...I don't quite get what's wrong about that...
-
Re: Help, please. I don't understand these error messages.
windowDeiconofied (WindowEvent e)
windowDeiconified(WindowEvent)
-
Re: Help, please. I don't understand these error messages.
Quote:
Originally Posted by
DarkExperience
Like I've told you, I don't know a whole lot about Java so...I don't quite get what's wrong about that...
Look at the spelling of what Java is expecting versus how you spelled it.
Also, please don't bother mentioning your deadlines. It comes off as rude and disrespectful of the time of other people here.
-
Re: Help, please. I don't understand these error messages.
Oh, sorry! After looking at this for hours on end, it kinda all looks the same to me :p. Thanks so much :)-:.
What about the second error? :(blush):
-
Re: Help, please. I don't understand these error messages.
Okay, I've looked at the second error again. I think I might have screwed up a bit :P. In line 12 I want to tell the program to show the actual result of the BMI calculation in the window. However, I think it needs a String to do that and the result of the calculation is a double. I've tried turning it into a String using toString in the BMI class before returning it to BmiFrame, however, I keep getting the error message "error: double cannot be dereferenced". I don't quite understand what that means. Could anyone explain how I can turn the result of the calculation into a String so I can use it in BmiFrame?
EDIT: Ah, no. I just had to change the double into a Double. However, I still get the same error message described in the first post whenever I try to compile BmiFrame.
-
Re: Help, please. I don't understand these error messages.
Quote:
Originally Posted by
DarkExperience
"error: double cannot be dereferenced".
That means you are trying to call a method on a primitive but they do not have methods. Only classes do.
Code:
double val = 4.23;
System.out.println(val.length());
You are doing something similar to that. If you need to convert a primitive to a String you can either use a static method in the String class or a static method in the wrapper class for the primitive you are using.
-
Re: Help, please. I don't understand these error messages.
Thanks for your help so far :)! We've actually been given two additional days to finish our programs. I've changed a few things and gotten the thing to compile. However, now all I ever get as the result of my "berechnen"-method is 0, even though the program actually recognises the values I type into the text field. Now, I thought I could solve this by adding the code you'll find in lines 64 and 65 of BmiFrame, however, I get error messages again and I don't know what to do about them. Sorry for the commentary being in German, BTW (and for everyone who can actually read it: I know it's wrong :P. I've changed the code after writing the commentary).
BMI
Code:
import java.awt.*;
import java.awt.event.*;
import java.text.*;
//Die für ein Fenster nötigen Datenpakete werden importiert
public class BmiFrame extends Frame
implements ActionListener, WindowListener {
//Die Klasse BmiFrame wird als eine Erweiterung der Klasse Frame mit einem ActionListener und einem WindowListener erstellt
private BMI meinBMI = null;
public static float gewicht = 0.0f;
public static float größe = 0.0f;
public float FBmi = 0.0f;
//Den benötigten Variablen werden ersstellt. Ihnen werden ihre Startwerte zugewiesen
private Label gewichtText = new Label("Gewicht in kg eingeben");
private TextField gewichtEingabe = new TextField("00");
private Label größeText = new Label("Größe in m eingeben");
private TextField größeEingabe = new TextField("00");
private Button berechnenButton = new Button("BMI berechnen");
private Label bmiText = new Label("Ihr BMI");
//Die Labels, Buttons und Textfelder des Fensters werden erstellt. Ihnen werden ihre Startwerte zugewiesen
private GridLayout fensterLayout = new GridLayout (3,2);
//Die Aufteilung/das Layout des Fensters wird bestimmt (3 Zeilen, 2 Spalten)
private DecimalFormat wertFormat = new DecimalFormat("0.#");
private DecimalFormat bmiFormat = new DecimalFormat("0.#");
//Die Formatierungen der Zahlenwerte wird angegebem
public void setBMI (BMI b) {
this.meinBMI = b;
}
public BMI getBMI() {
return meinBMI;
}
//Das Object meinBMI der Klasse BMI wird zurückgegeben
public void init () {
float gewicht = meinBMI.getGewicht();
String gewString = wertFormat.format(gewicht);
gewichtEingabe.setText(gewString);
float größe = meinBMI.getGröße();
String gröString = wertFormat.format(größe);
größeEingabe.setText(gröString);
//Den Textfeldern GewichtEingabe und GrößeEingabe wird der Wert der Floats Gewicht und Größe zugewiesen (welche zuvor aus meinBMI importiert und in Strings umgewandelt werden)
this.setSize(300,150);
this.setTitle("BMI-Rechner");
//Größe und Titel des Fensters werden festgelegt
this.setLayout(fensterLayout);
this.add(gewichtText);
this.add(gewichtEingabe);
this.add(größeText);
this.add(größeEingabe);
this.add(bmiText);
this.add(berechnenButton);
//Die einzelnen Module werden in das zuvor bestimmte Fensterlayout eingeordnet
berechnenButton.addActionListener(this);
this.addWindowListener(this);
//Dem BerechnenButton wird ein ActionListener angehängt
}
public void actionPerformed (ActionEvent e) {
String gewichtString = gewichtEingabe.getText().trim();
float gewicht = Float.parseFloat(gewichtString);
String größeString = größeEingabe.getText().trim();
float größe = Float.parseFloat(größeString);
//Die vom User eingegebenen Werte werden zunächst Strings zugewiesen, welche dann in Floats umgewandelt werden
Object angeklickt = e.getSource();
if (angeklickt == berechnenButton) {
meinBMI.setGewicht(float meinBMI.gewicht);
meinBMI.setGröße(float meinBMI.größe);
meinBMI.berechnen();
System.out.println(größe);
}
//Bei Klicken des berechnenButtons wird die Methode meinBMI.berechnen() ausgelöst
float FBmi = meinBMI.getBmis();
String bmiString = bmiFormat.format(FBmi);
bmiText.setText(bmiString);
System.out.println(FBmi);
}
//Das Ergebnis von meinBMI.berechnen() wird dem Float FBmi als Wert zugewiesen. Dieses wird anschließend in den String bmiText umgewandelt
public void windowClosing (WindowEvent e) {
this.setVisible(false);
System.exit(0);
}
public void windowClosed (WindowEvent e) { }
public void windowOpened (WindowEvent e) { }
public void windowIconified (WindowEvent e) { }
public void windowDeiconified (WindowEvent e) { }
public void windowActivated (WindowEvent e) { }
public void windowDeactivated (WindowEvent e) { }
}
//Dem Fenster selbst werden seine Zustände/Funktionen zugewiesen
BmiFrame
Code:
public class BMI {
//Klasse BMI wird erstellt
private float Bmis = 0.0f;
public float gewicht = 5.0f;
public float größe = 8.0f;
//Floats Bmis, gewicht und größe, neuesGewicht und neueGröße werden erstellt und ihnen werden Startwerte zugewiesen
public void setGewicht (float gewicht) {
if (BmiFrame.gewicht >= 0.01f) {
this.gewicht = BmiFrame.gewicht;
}
}
//Dem Float Gewicht wird der Wert des Floats neuesGewicht zugewiesen (nur wenn dieser größer als 0.01)
public float getGewicht() {
return this.gewicht;
}
//Das Float Gewicht wird zurückgegeben
public void setGröße (float größe) {
if (BmiFrame.größe >= 0.01f) {
this.größe = BmiFrame.größe;}
}
//Dem Float Größe wird der Wert des Floats neueGröße zugewiesen (nur wenn dieser größer als 0.01 ist)
public float getGröße() {
return this.größe;
}
//Das Float Größe wird zurückgegeben
public void berechnen () {
if (größe*größe >=0.01f) {
Bmis = this.gewicht/(größe*größe);
}
}
//Die BMI-Rechnung (Gewicht durch Größe-Hoch-2) wird durchgeführt, wenn größe*größe größer als 0.01 ist. Das Ergebnis der Rechnung wird dem Float Bmis ls Wert zugewiesen
public float getBmis() {
return this.Bmis;
}
//Der Float Bmis wird zurückgegeben
}
Error Message
Code:
Compiliere G:\...\BmiFrame.java mit Java-Compiler
BmiFrame.java:64:32: error: '.class' expected
meinBMI.setGewicht(float meinBMI.gewicht);
^
BmiFrame.java:64:39: error: not a statement
meinBMI.setGewicht(float meinBMI.gewicht);
^
BmiFrame.java:64:47: error: ';' expected
meinBMI.setGewicht(float meinBMI.gewicht);
^
BmiFrame.java:65:30: error: '.class' expected
meinBMI.setGröße(float meinBMI.größe);
^
BmiFrame.java:65:37: error: not a statement
meinBMI.setGröße(float meinBMI.größe);
^
BmiFrame.java:65:43: error: ';' expected
meinBMI.setGröße(float meinBMI.größe);
^
6 errors
Could you please tell me what's wrong? I don't get it.
-
Re: Help, please. I don't understand these error messages.
If you want help, you should provide an SSCCE that demonstrates the problem. That's way too much code.
But those errors are showing improper syntax. That's not how you pass values into a method. Recommended reading: Passing Information to a Method or a Constructor (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
-
Re: Help, please. I don't understand these error messages.
I still keep getting 0 as the result of my BMI calculation. I now know why this is: it's because the program doesn't seem to recognise the weight figure which is typed into the TextField by the user. However, this doesn't make any sense to me, as I treat the weight and the height figures exactly the same and the program DOES recognise the height figure. These are the parts of the program which have got to do with the weight figure:
Code:
public class BmiFrame {
private BMI meinBMI = null;
public static float gewicht = 0.0f;
private TextField gewichtEingabe = new TextField("00");
private DecimalFormat wertFormat = new DecimalFormat("0.##");
public void init () {
float gewicht = BMI.getGewicht();
String gewString = wertFormat.format(gewicht);
gewichtEingabe.setText(gewString);
this.add(gewichtText);
this.add(gewichtEingabe);
}
public void actionPerformed (ActionEvent e) {
String gewichtString = gewichtEingabe.getText().trim();
float gewicht = Float.parseFloat(gewichtString);
Object angeklickt = e.getSource();
}
public static float getGewicht() {
return BmiFrame.gewicht;
}
}
Code:
public class BMI {
public static float gewicht = BmiFrame.gewicht;
public static void setGewicht (float gewicht) {
if (BmiFrame.gewicht >= 0.01f) {
BMI.gewicht = BmiFrame.gewicht;
}
}
public static float getGewicht() {
return BMI.gewicht;
}
}
Could you please help me out with this :)-:?
-
Re: Help, please. I don't understand these error messages.
Code:
public void actionPerformed (ActionEvent e) {
String gewichtString = gewichtEingabe.getText().trim();
float gewicht = Float.parseFloat(gewichtString);
Object angeklickt = e.getSource();
}
Look at what you are doing with gewicht
-
Re: Help, please. I don't understand these error messages.
Ehm...isn't that correct? I'm not exactly a pro at this and I've pretty much copied the program together out of the examples we've been given in class.
-
Re: Help, please. I don't understand these error messages.
Okay ask yourself this. BmiIFrames static variable gewicht is reassigned where?
-
Re: Help, please. I don't understand these error messages.
Ehm...it's assigned to a Float which has been assigned to the value in the TextField? Sorry if that answer seems dumb. We've just not gotten very far in Java at school.
-
Re: Help, please. I don't understand these error messages.
Where in the code is it assigned to the TextField?
-
Re: Help, please. I don't understand these error messages.
In the line above that one I've assigned the value from the TextField (which is called gewichtEingabe) to the variable gewichtString.
-
Re: Help, please. I don't understand these error messages.
can you please type that out for me
-
Re: Help, please. I don't understand these error messages.
Code:
public void actionPerformed (ActionEvent e) {
String gewichtString = gewichtEingabe.getText().trim();
//In the above line, the content of the TextField gewichtEingabe gets assigned to the String gewichtString
float gewicht = Float.parseFloat(gewichtString);
//In the above line, the String gewichtString gets converted into a Float which is then assigned to the float gewicht
Object angeklickt = e.getSource();
}
-
Re: Help, please. I don't understand these error messages.
That float gewicht is local to the actionPerformed() method. It is not the BmiFrames static float gewicht variable.