Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an input checking function that takes input from the user. (It's supposed to be lower case letter and digit. It's for the battleship game.) The function needs to remove spaces from input, lowercase it, and check to make sure that the first character is a letter, and the second is a digit. Letters must be from a to g, and digits from 1 to 7. I wrote some code, but for some reason after any mistake, the programs ends. It should say an error message and wait for new input. I tried to check inputLine = null; to return null;, but still not working.. What did I do wrong? Here is my code.
============================================================

C#
inputLine = is.readLine( );

       inputLine = inputLine.replaceAll("\\s","");          // replace all non-characters with "" empty
       inputLine = inputLine.toLowerCase();                 // cast all characters to lower case

       if (inputLine.length( ) == 0 )
       inputLine = null;
            else if (inputLine.length () >2)                //method to check valid input
            {
            System.out.println("Invalid input, please enter column (a-g) and row (1-7)");
            inputLine = null;
            }
            else {
                  if (Character.isLetter(inputLine.charAt(0)))
                    {
                     char i = inputLine.charAt(0);
                     if (i<'a' || i>'g')
                        {
                        System.out.println("Invalid input, please enter a letter a to g");
                        inputLine = null;
                        }
                        int row = i -'a';
                        if (Character.isDigit(inputLine.charAt(1)))
                        {
                        int k = (inputLine.charAt(1)-'0');
                            if (k<1 || k>7)
                            {
                            System.out.println("Invalid input, please enter a digit 1 to 7");
                            inputLine = null;
                            }
                        }
                    }
                 }
         }
Posted
Comments
[no name] 28-Jun-13 19:48pm    
Well for starters, you do not show anywhere where you have any kind of a loop that waits for valid input before continuing.
Michael Gurevich 28-Jun-13 23:10pm    
how should i fix it ?
Shubhashish_Mandal 29-Jun-13 2:38am    
Instead of doing if-else try to write a regx pattern and match the pattern with your input
Michael Gurevich 29-Jun-13 9:51am    
i am not sure how to do that.. i just started learning java, we did not go over regx yet..
any advice? thank you !
Shubhashish_Mandal 2-Jul-13 2:47am    
then start with this >>
http://docs.oracle.com/javase/tutorial/essential/regex/

1 solution

You will need to provide an infinite loop in your code that only terminates when the user inputs the correct information.

...
boolean invalid = true;
while(invalid){
  if(...){
    // Perform your validity checks.
    // if all is well
    invalid = false;
  }
  else{
    ...
    //Error message
    invalid;
  }
}
...


Also you may try to make use of regex to check your code for conformity to your statuses. Makes your code cleaner and shorter.

As an addition to your code for slight modification, you may choose to read the first two characters and ignore any other characters after the second, that way whatever number of characters the user types, you will only use the first two
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900