Hello all, is that possible to help me out with Error Checking for user input? I am developing a peg solitaire program and now need a error checking for user input or for input file.

Either use try and catch.. or making another method.. can anyone give a me a hint or help me out here please.

Regards

Recommended Answers

All 4 Replies

what kind of error checking? like when the user tries to put a king on a 7? you can use a method to check that and if the user is being a moron, then you can display a joptionpane or you can throw one of your own exceptions which will have to be dealt with. something like InvalidMoveException.

For something like that I would say some if else statements would work. Like:

if (!card slot is already taken)
{
      //do something
}
else
{
    //do something else
}

Of course it would be cool to experiment making your own exception as EB said.

Thanks for all replies. I just got it working with try and catch exception, my problem was I use try {}catch (IOException e) {} instead of just use catch (Exception e) {}

The input only reads: From <row><coloum> To <row1><coloum1>
Exception prevent any invalid input such as "jslkfjsdlkfjlkd".

import javax.swing.*;

public class aaa{   
    public static void main(String[]args){

    String username;
        int i;
    String password;
        int j;

    String[]uname={"geerthan"};
    String[]pwd={"19881108"};
    password.setEchoChar('*');


                    boolean loop=true;
                        while(loop){
            username=JOptionPane.showInputDialog("Enter the Username");
                if(username.equals("")){
            String message="Please Enter the Username";
            JOptionPane.showMessageDialog(new JFrame(),message,"Warning",JOptionPane.WARNING_MESSAGE);
                }
            else
            {
            password=JOptionPane.showInputDialog("Enter the Password");
                if(password.equals("")){
            String message="Please Enter the Password";
            JOptionPane.showMessageDialog(new JFrame(),message,"WARNING",JOptionPane.WARNING_MESSAGE);
            }
            else
            {

            for(i=0;i<uname.length;i++){
                if(uname[i].equals(username)){
            for(j=0;j<pwd.length;j++){
                if(pwd[j].equals(password)){
                        loop=false;
                            break;
                            }
                                }
                                    }
                                        }
        if(loop){
        String message="Username or Password is Incorrect";
        JOptionPane.showMessageDialog(new JFrame(),message,"Error",JOptionPane.ERROR_MESSAGE);
        }
        else
        {
        JOptionPane.showMessageDialog(null,"You can access");
        }

        }
            }
                }
                    }
                        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.