简体   繁体   中英

Ask for user input and verify

I am doing a school JAVA project in which I have ask for user to input a 8 digit string followed by a letter at the end, and I have to verify:

  • that I receive those 8 digits and not letters
  • the final letter has to be included in a given char [ ] list.

I will paste here the given list and positions

在此处输入图片说明

  • final and most tricky thing - the letter in order to be valid has to be the numbers entered divided by 23 (number of total characters). For example if user inputs 00000102X the only valid letter has to be X. (102%23=10) In case user enters 24659213Q the letter has to be Q (24659213%23=16).

I'm not so sure where to start..so the only thing I got done till now is:

    import java.util.Scanner;
public class Practice {

    static final char[] LETTERS = {'T', 'R', 'W', 'A', 'G', 'M', 'Y', 'F', 'P', 'D', 'X'
                                 ,'B', 'N',  'J', 'Z', 'S', 'Q', 'V', 'H', 'L', 'C'
                                 , 'K', 'E'};
    String [][] input;

    public static void main (String[] args){
    Scanner scanner = new Scanner (System.in);

    System.out.print("Please input: ");

}
}

Any idea how to even start this? Any help would be kindly accepted. Thank you!

What I need to do is:

  1. ask user to input 8 digits and a letter
  2. verify composition of the input (8 digits+1 letter)
  3. take digits as a number and divide it by 23. The result should point the accepted letter from the char [ ] list
  4. If all OK, save and go back to step 1. If not give error and ask for user to input again.

I will give you some design thoughts instead of code so that you can go ahead and write that yourself:

  • think about how to store the user input (eg would it be a list of char s, a String maybe?) - that depends on what you're more comfortable working with but I recommend a String

  • what methods will you need? if you break the task into a number of different checks you'll probably need to

  • firstly check the length of the String (you can write a method just for this but it'll only be one line of code so I don't recommend this),

  • then a check that all the characters except the last one are digits and the last one is a String (you could get the substring represented by the first 7 characters and try and parse an Integer out of it, then you could check that the last character is a letter (there are built-in methods in the class Character for checking this and in the String class for working with different parts of the String )

  • then perhaps a different method that checks that the number represented by the first 7 characters modulo 23 is the index of the last letter in your given char[] array

  • when you have all check methods in place (I think you should simply assert what each method should check inside the body of it eg assert(input.length == 8) or throw a custom or built-in Exception in case the check fails) you can simply chain them up (ie call them one after the other) in your main method

Good luck!

You will need to do this step by step:

  1. Check whether string has 9 characters or not. If not, ask for input again.
  2. Take first 8 characters and use Integer.parseInt(...) over it. If it throws NumberFormatException , ask for input again.
  3. Now, you have the number to be validated and the array against which it is to be validated. So, you can take modulus of number and check if the character matches with the modulus index of array.

Example:

class Example{
    final char[] LETTERS = {'T', 'R', 'W', 'A', 'G', 'M', 'Y', 'F', 'P', 'D', 'X'
                                 ,'B', 'N',  'J', 'Z', 'S', 'Q', 'V', 'H', 'L', 'C'
                                 , 'K', 'E'};

    boolean isLastCharacterValid(int number, char c){  //c means the 9th character, number is the  numeric value of first 8 characters
        return (c == LETTERS[number % LETTERS.length]);
    }
}

A do-while loop would be better for your case.

You can use :

int i = scanner.nextInt();
String str=""+i;
if (str.length() != 8) {
 // ERROR
}
char c = scanner.nextChar();
...

Some examples there:

https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM