0
import java.awt.*;
import java.applet.*;

public class userin extends Applet
{
TextField text1, text2;
public void init( )
{
    text1 = new TextField(8);
    text2 = new TextField(8);
    add (text1);
    add (text2);
    text1.setText ("0");
    text2.setText ("0");
}
public void paint (Graphics g)
{
    int x = 0, y = 0, z = 0;
    String s1, s2, s;
    g.drawString("input a number in each box", 10, 50);
    try
    {
        s1 = text1.getText( );
        x = Integer.parseInt(s1);
        s2 = text2.getText( );
        y = Integer.parseInt(s2);
    }
    catch (Exception ex) { }
    z = x + y;
    s = String.valueOf (z);
    g.drawString ("THE SUM IS:", 10, 75);
    g.drawString (s, 100, 75);
}
public boolean action (Event event, Object object)
{
    repaint ( );
    return true;
}

}

The following code works well on windows and gives the sum of 2 numbers, but when i use it on mac in Netbeans or Eclipse, the code runs but sum is always 0. I don't know what is causing this issue, It would be kind if someone could help out.

Anton
  • 3,587
  • 2
  • 12
  • 27
Kamesh
  • 1,122
  • 9
  • 12
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) See [Java Plugin support deprecated](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) and [Moving to a Plugin-Free Web](https://blogs.oracle.com/java-platform-group/entry/moving_to_a_plugin_free). .. – Andrew Thompson Mar 06 '16 at 11:35
  • .. 3) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Mar 06 '16 at 11:35

1 Answers1

4

The only reason that can cause this is exception thrown in your try block that you catch an ignore:

try {
   .......
} catch (Exception e) {}

It is extremely bad practice to write empty catch block because you are actually hiding problems from yourself.

First, just remove your try/catch at all or print exception:

try {
      ...........
} catch (Exception e) {
     e.printStackTrace();
}

Now you will see your problem. I guess it is NumberFormatException thrown from Integer.parseInt() and it is caused by wrong User's input.

I believe it is not a problem of different behavior on different platform but different input that you typed on each one of the tests.

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • But i am just entering integers in both the text boxes, then why is it misbehaving. Same code runs without any trouble on Windows – Kamesh Mar 04 '16 at 16:39
  • Did you try to do what I suggested you to see the exception? – AlexR Mar 04 '16 at 16:41
  • Just remove your try/catch, get exception and post it here. I guess that the reason will be obvious. I can suppose what is it but I do not want to do this before I see the exception. – AlexR Mar 04 '16 at 16:45
  • 1
    `} catch (Exception e) { e.printStackTrace(); }` That isn't the answer to (just) this question, it's the answer (or at least good direction towards it) for **1000s** of questions asked around these parts. *Don't ignore stack traces!* – Andrew Thompson Mar 06 '16 at 11:34
  • StackTrace is a king! :) – AlexR Mar 07 '16 at 15:25