简体   繁体   中英

How to add the input from a textfield

Here is my code below, I will highlight what is throwing the error, I am completely lost on how to fix this...

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.awt.event.ActionListener.*;
import java.awt.event.TextListener.*;
public abstract class GradeAverage extends Applet
implements ActionListener, TextListener{
Button Ok=new Button("Ok");
Button Cancel=new Button("cancel");
TextField Math=new TextField("Math Grade",2);
TextField Science=new TextField("Scien Grade",2);
TextField SocialStudies=new TextField("Social Studies Grade",2);
TextField English=new TextField("English Grade",2);
TextField Elective=new TextField("Elective Grade",2);
TextField Elective2=new TextField("2nd Elective Grade",2);
TextArea message=new TextArea();
public void init(){
GridBagLayout gbl=new GridBagLayout();
setLayout(gbl);
GridBagConstraints c=new GridBagConstraints();
c.anchor=GridBagConstraints.WEST;
c.weightx=2.0;
c.weighty=2.0;
c.fill=GridBagConstraints.HORIZONTAL;
c.insets=new Insets(10,10,10,10);
gbl.setConstraints(Math,c); add(Math);
c.fill=GridBagConstraints.NONE;
gbl.setConstraints(Science,c); add(Science);
c.fill=GridBagConstraints.HORIZONTAL;
gbl.setConstraints(SocialStudies,c); add(SocialStudies);
c.gridy=1;
gbl.setConstraints(English,c); add(English);
c.gridy=2;
gbl.setConstraints(Elective,c); add(Elective);
c.fill=GridBagConstraints.NONE;
gbl.setConstraints(Elective2,c); add(Elective2);
c.gridy=3;
c.anchor=GridBagConstraints.CENTER;
gbl.setConstraints(Ok,c); add(Ok);
gbl.setConstraints(Cancel,c); add(Cancel);
c.gridy=4;
c.gridwidth=3;
c.fill=GridBagConstraints.BOTH;
gbl.setConstraints(message,c); add(message);
Math.addTextListener(this);
Science.addTextListener(this);
SocialStudies.addTextListener(this);
English.addTextListener(this);
Elective.addTextListener(this);
Elective2.addTextListener(this);
Ok.addActionListener(this);
Cancel.addActionListener(this);
}
public void actionPerformed(ActionEvent event){
Object Source=event.getSource();
if(Source==Ok){
Math.getText();
Science.getText();
SocialStudies.getText();
English.getText();
Elective.getText();
Elective2.getText();
do{ 
**message=(Math+Science+SocialStudies+English+Elective+Elective2)/6;**
system.println("Your grade average is"+message);
}while (message<0);
if(Source==Cancel){
system.ext(0);
}
}}}

The code throwing the error is marked by ** The error given is: bad operand types for binary operator '+'

Please help!

Thanks

You cannot add TextFields together, and Java does not support overloading. To get the value, you must first call getText() on each TextField , then pass it to Double.parseDouble to convert it into a number that Java can add.

You should have something like this:

double gpa = (Double.parseDouble(Math.getText()) +
              ...
              Double.parseDouble(Elective2.getText())
             ) / 6;

You'll need to catch NumberFormatException if any TextField contains text that can't be converted into a number.

Then you'll need to convert the GPA into text for the TextArea you've called message .

message.setText(String.valueOf(gpa));

It looks like you are trying to add string variables together - try converting them to integer values first. something like

 message=(Integer.parseInt(Math)+Integer.parseInt(Science)+Integer.parseInt(English)...

Here is a link to another thread on this very subject: How to convert a String to an int in Java?

Hope this helps you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM