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

/**
 * Provides a TextField to type in an arbritrary String.
 * Tells in a separated TextArea whether the String typed
 * is a decimal integer literal.
 */
public class IsDecimalIntegerLiteral extends Applet {
    TextField input;
    TextArea  output;

    /**
     * As always...
     */
    public void init() {
        setLayout(new BorderLayout());
        input=new TextField(10);
        input.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                isDecimalIntegerLiteral();
            }
        });
        add(input, BorderLayout.NORTH);
        output=new TextArea(10, 56);
        output.setEditable(false);
        add(output, BorderLayout.CENTER);
    }

    /**
     * @return  true    iff parameter c equals '0'
     */
    private boolean isZero(char c) {
        if (c=='0') {
            return true;
        } else {
            return false;
        }
    }
    
    /**
     * @return  true    iff parameter c is between '1' and '9'
     */
    private boolean isNonZeroDigit(char c) {
        if (c>='1' && c<='9') {
            return true;
        } else {
            return false;
        }
    }
    
    /**
     * @return  true    iff isDigit(c) or isNonZeroDigit(c)
     */
    private boolean isDigit(char c) {
        return isZero(c)||isNonZeroDigit(c);
    }
    
    /**
     * Reads the contents of this Applet's TextField and
     * outputs in the TextArea whether the String found
     * was a decimal integer literal.
     */
    private void isDecimalIntegerLiteral() {

        // ignore leading and trailing white spaces
        String s=input.getText().trim();

        // assume by default to get a decimal integer literal
        boolean isDecimalIntegerLiteral=true;
        
        // firstly, check if s equals "0"
        if (s.equals("0")==false) {
            
            // secondly, check if we have a zero at the first position
            if (isNonZeroDigit(s.charAt(0))) {
                
               // lastly, for all remaining items check if they are digits
               for (int i=1; i<s.length(); i++) {
                  if (isDigit(s.charAt(i))==false) {
                      
                      // second or later position isn't a digit
                      // ==> s is NOT a decimal integer literal
                      isDecimalIntegerLiteral=false;
                  }
               }
            } else {
                
               // either leading 0 but following digits, or
               // no digit at all at first position
               // ==> s is NOT a decimal integer literal
               isDecimalIntegerLiteral=false;
            }
        }
        output.append("Die Zeichenkette "+s+" ist ");
        if (isDecimalIntegerLiteral==false) {
            // Was ist das Wichtigste am Schweißen? -- Das 'w'...
            output.append("k");
        }
        output.append("ein Dezimalliteral.\n");
        
        // reset input TextField to the empty string
        input.setText("");
    }
}

