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:
I will paste here the given list and positions
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:
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:
Integer.parseInt(...)
over it. If it throws NumberFormatException
, ask for input again. 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.