/** * @(#)Validointi.java * * Validointi Applet application * * @author * @version 1.00 2009/10/29 */ import java.applet.*; import java.awt.*; import java.awt.event.*; public class Validointi extends Applet implements ActionListener { char[] merkit = { '1','2','3','4','5','6','7','8','9','0','A','B','C','D','E','F','H','J','K','L','M','N','P','R','S','T','U','V','W','X','Y'}; TextField tx; Label lx; Button ok; String x,y; int a; char merkki, merkki2; public void init () { add(tx = new TextField("123654-123A")); add(lx = new Label("")); add(ok = new Button("Ok")); ok.addActionListener(this); } public void paint(Graphics g) { tx.reshape(40, 20, 100, 20); lx.reshape(40, 40, 180, 20); ok.reshape(40,60,40,20); g.drawString(""+a+" "+ (a % 31 )+" "+merkki2,150,20); } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Ok")){ try { x = tx.getText() ; if ( x.matches ( "[0-9]{6}[-+A][0-9]{3}[0-9A-FHJ-NPR-Y]" ) ) { a=Integer.valueOf( x.substring(0,6) + x.substring(7,10)); merkki = x.charAt(10); merkki2 = merkit[ a % 31]; lx.setText( x ); tx.setText(""); tx.requestFocus(); } else throw new Exception(); } catch (Exception f ) { lx.setText("Anna oikea tunnus"); tx.setText(""); tx.requestFocus(); } } repaint(); } }