mardi 23 novembre 2021

Try Catch and if else are working unexpectedly in JAVA

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.Border;
import java.io.*;
import java.util.*;
class everything extends JFrame implements ActionListener{
    private JButton addnew,print,Continue;
    private JPanel details,table;
    private JLabel entfirstname,entlastname,entemail,entphone,enrgender,entid;
    private JTextField firstname,lastname,phone,id;
    private JComboBox entgender;
    private Random random;
    private String gender[]={"MALE","FEMALE"};
    private JTextArea result;
    private  Font font;
    private int a;
    everything(String title){
        super(title);
        setLayout(null);
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setBackground(new Color(20,45,120));
        random=new Random();
        details=new JPanel();
        details.setLayout(null);
        details.setBounds(20,70,350,500);
        details.setOpaque(true);
        details.setBackground(Color.GRAY);
        add(details);

        entfirstname=new JLabel("First Name:");
        entfirstname.setBounds(5,10,70,30);
        details.add(entfirstname);
        firstname=new JTextField();
        firstname.setBounds(90,10,200,30);
        details.add(firstname);
        firstname.setEnabled(false);

        entlastname=new JLabel("Last Name:");
        entlastname.setBounds(5,60,70,30);
        details.add(entlastname);
        lastname=new JTextField();
        lastname.setBounds(90,60,200,30);
        details.add(lastname);
        lastname.setEnabled(false);

        entid=new JLabel("Student Id:");
        entid.setBounds(5,110,70,30);
        details.add(entid);
        id=new JTextField();
        id.setBounds(90,110,200,30);
        details.add(id);
        id.setEnabled(false);

        /*entemail=new JLabel("Email:");
        entemail.setBounds(5,160,70,30);
        details.add(entemail);
        email=new JTextField();
        email.setBounds(90,160,200,30);
        details.add(email);
        email.setEnabled(false);*/

        entphone=new JLabel("Phone:");
        entphone.setBounds(5,160,70,30);
        details.add(entphone);
        phone=new JTextField();
        phone.setBounds(90,160,200,30);
        details.add(phone);
        phone.setEnabled(false);

        enrgender=new JLabel("Gender:");
        enrgender.setBounds(5,210,70,30);
        details.add(enrgender);
        entgender=new JComboBox(gender);
        entgender.setBounds(90,210,200,30);
        details.add(entgender);
        entgender.setEnabled(false);

        addnew=new JButton("Add new");
        addnew.setBounds(90,350,200-50,35);
        addnew.setForeground(new Color(20,45,120));
        details.add(addnew);
        addnew.addActionListener(this);

        font=new Font("Arial",Font.BOLD,20);

        Continue=new JButton("Continue");
        Continue.setForeground(new Color(20,45,120));
        details.add(Continue);
       Continue.addActionListener(this);

       result=new JTextArea();
       result.setOpaque(true);
       result.setBounds(550,50,600,400);
       result.setEditable(false);
       result.setOpaque(false);
       result.setFont(font);
       result.setForeground(Color.WHITE);
       result.setBorder(BorderFactory.createMatteBorder(
               1, 5, 1, 1, Color.red));
       print=new JButton("Download the Receipt");
       add(print);


       add(result);

        setVisible(true);

    }
    public void actionPerformed(ActionEvent event){
            if(event.getSource()==addnew){
                firstname.setEnabled(true);
                lastname.setEnabled(true);
                entgender.setEnabled(true);
                id.setEnabled(true);
                id.setEditable(false);
                phone.setEnabled(true);
                int x=random.nextInt(50000);
                id.setText("Al"+x+"");
                Continue.setBounds(90,350,200-50,35);
                addnew.setBounds(0,0,0,0);

            }
            if(event.getSource()==Continue){

                String num=phone.getText();
                String fname=firstname.getText();
                String lname=lastname.getText();
                String resulttext="Student's name:\t"+fname.toUpperCase(Locale.ROOT)+" "+lname.toUpperCase(Locale.ROOT)+"\nRecorded phone number:\t"+a+"\nStudent id:\t\t"+id.getText().toUpperCase(Locale.ROOT)+"\nStudent's gender:\t"+entgender.getSelectedItem();

                if(num.length()!=10){
                    JOptionPane.showMessageDialog(null,"Please enter a correct phone number","Alert",JOptionPane.ERROR_MESSAGE);
                }else {
                    result.setText(resulttext);
                    print.setBounds(700,600,250,30);

                try{
                    a=Integer.parseInt(num);
                }catch(NumberFormatException e){
                    JOptionPane.showMessageDialog(null,"Please enter a valid Phone Number","Error",JOptionPane.ERROR_MESSAGE);
                    result.setText("");
                    phone.setForeground(Color.RED);

                    return;
                }



                }


            }


        }

    }


class College{
    public static void main(String[] args) {
        new everything("School registration");
    }
}

When I enter my phone number weird things are happening. If I enter : 1234567890 it is showing not correct phone number which is the condition for character exceed limit. If I enter my original phone number (which is of course a 10 digit number) it is showing invalid phone number which is the catch block.

What I want is to check whether the number entered has 10 digits or not and if anyone enters character values, an error message will be showed. Any help will be greatly appreciated

Aucun commentaire:

Enregistrer un commentaire