Reputation: 1845
What is wrong with this Java Applet
, even tho I compile it with no issue it does not run.
import java.applet.*;//Importing java.applet
public class MyApplet extends Applet {
TextField txt1, txt2;
public void init(){//Initializing our applet
txt1 = new TextField(""); //Creates a textfield 'txt1'
txt2 = new TextField(""); //Creates a textfield 'txt2'
setBackground(Color.CYAN);//Setting background color to CYAN
add(txt1); //Adds textfield 'txt1' to your applet
add(txt2); //Adds textfield 'txt2' to your applet
}
public void paint(Graphics obj){//Paint method to display our message
String s1 = txt1.getText(); //Fetching data from text field 1.
String s2 = txt2.getText(); //Fetching data from text field 2.
int num1=0, num2 = 0, num3; //Declaring 3 integer variables
num1 = Integer.parseInt(s1); //Parsing the string value of text field 1 to integer
num2 = Integer.parseInt(s2); //Parsing the string value of text field 2 in integer
num3 = num1 + num2; //Performing addition
String s3 = String.valueOf(num3); //Converting the result from integer to string
obj.drawString("Result:", 40, 50);
obj.drawString(s3, 50, 50);//To display the result
}
}
Upvotes: 0
Views: 725
Reputation: 1
The major problem is the strings couldn't be painted properly. The paint
method calls by the applet several times before you have any input. So, the textfields has empty strings. This because a NumberFormatException
. Check the text for empty value. If you parse numbers add try/catch
block. In addition moving the logic to the method calculate
with repaint
and initiating it when you make input. This could be happen if you add some listeners to the textfields.
import java.applet.*;
import java.awt.*;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;//Importing java.applet
public class MyApplet extends Applet
{
TextField txt1, txt2;
String s3;
public void init()//Initializing our applet
{
txt1 = new TextField(""); //Creates a textfield 'txt1'
txt2 = new TextField(""); //Creates a textfield 'txt2'
txt1.addTextListener(new TextListener(){
public void textValueChanged(TextEvent e) {
calculate();
}
});
txt2.addTextListener(new TextListener(){
public void textValueChanged(TextEvent e) {
calculate();
}
});
setBackground(Color.CYAN);//Setting background color to CYAN
add(txt1); //Adds textfield 'txt1' to your applet
add(txt2); //Adds textfield 'txt2' to your applet
}
void calculate(){
try {
String s1 = txt1.getText(); //Fetching data from text field 1.
String s2 = txt2.getText(); //Fetching data from text field 2.
int num1=0, num2 = 0, num3; //Declaring 3 integer variables
num1 = Integer.parseInt(s1); //Parsing the string value of text field 1 to integer
num2 = Integer.parseInt(s2); //Parsing the string value of text field 2 in integer
num3 = num1 + num2; //Performing addition
s3 = String.valueOf(num3); //Converting the result from integer to string
repaint();
} catch (NumberFormatException nfe){}
}
public void paint(Graphics obj)//Paint method to display our message
{
super.paint(obj);
obj.drawString("Result:", 100, 100);
if (s3 != null)
obj.drawString(s3, 150, 100);//To display the result
}
}
Upvotes: 0
Reputation: 2616
The problem is in :
txt1 = new TextField("");
txt2 = new TextField("");
as
String s1 = txt1.getText();
String s2 = txt2.getText();
num1 = Integer.parseInt(s1);
num2 = Integer.parseInt(s2);
will not be able to parse ""
and will throw exception
as explained by Jon Skeet.
So the fix could be a try catch
block(Exception handling):
try{
num1 = Integer.parseInt(s1);
num2 = Integer.parseInt(s2);
}catch(NumberFormatException ex){
System.out.println(ex);
}
OR have some initial parseable String
value
txt1 = new TextField("0");
txt2 = new TextField("0");
Upvotes: 0
Reputation: 1500165
I strongly suspect a NumberFormatException
is being thrown.
After all, any time the applet tries to paint itself - including immediately after initialization - you'll be running this code:
// Comments removed as they were more distracting than useful. You really
// *don't* need to comment variable declarations to say they're declarations...
String s1 = txt1.getText();
String s2 = txt2.getText();
int num1=0, num2 = 0, num3;
num1 = Integer.parseInt(s1);
num2 = Integer.parseInt(s2);
So when txt1.getText()
returns an empty string, which it will before the user has had a chance to type anything, you're going to be parsing that empty string, which will throw a NumberFormatException
.
It feels to me that the general design of this applet is inappropriate. Why would you want to use drawString
for what are essentially labels?
I would add either one or two Label
controls - either one for the complete text "Result: " and the result, or one for just "Result: " and a separate one for the result. Then you don't need to override paint()
at all - you can instead add handlers for when the textbox contents change - which is, after all, the only time you need to change anything.
You should then put the Integer.parseInt
call into a try/catch block, catching NumberFormatException
. (You could also consider using a NumberFormat
instead of Integer.parseInt
, but you can do that later...)
Upvotes: 1