我需要java中的验证方法
我需要为学校的项目制作自动取款机。 我完成了所有工作,一切正常,我对pin进行了验证,因为它是字符串。因此,我的问题是如何对所有其他方法进行验证,以检查是否输入了除数字以外的任何内容,从而表明用户是错误的,并在该方法开始时返回给他。所有变量都作为整数存储到数组中 所以这是我的代码,请帮助我,我尝试了很多东西,但我不能让它工作我需要java中的验证方法,java,arrays,validation,methods,Java,Arrays,Validation,Methods,我需要为学校的项目制作自动取款机。 我完成了所有工作,一切正常,我对pin进行了验证,因为它是字符串。因此,我的问题是如何对所有其他方法进行验证,以检查是否输入了除数字以外的任何内容,从而表明用户是错误的,并在该方法开始时返回给他。所有变量都作为整数存储到数组中 所以这是我的代码,请帮助我,我尝试了很多东西,但我不能让它工作 public class Banka { static Scanner skener = new Scanner(System.in); static S
public class Banka {
static Scanner skener = new Scanner(System.in);
static String pin[] = {"1234","2345","3456","4567","5678"};
static String username[] = {"Mate","Nathan","John","Michelle","Angelina"};
static int balance[] = {200,100,250,150,300};
static boolean overdraft[] = {true,true,false,true,false};
static int index;
public static void main(String[] args) {
login();
}
public static void login() {
Date datum = new Date();
System.out.println("" +datum);
System.out.println("------------------------------");
System.out.println("Welcome to the Illuminati Bank \n Please log in with your PIN");
String login = skener.next();
checkpin(login);
for (int i = 0; i< pin.length; i++) {
if (login.matches(pin[i])) {
index = i;
System.out.println("\nWelcome " + username[index]);
Menu();
}
}
System.out.println("Wrong PIN entered, please login again \n");
login();
}
public static void Menu() {
System.out.println("Please select an option");
System.out.println("\n 1.View Bank Statement \n 2.Deposit \n 3.Withdraw \n 4.Change Pin \n 5.Exit \n");
int choice = skener.nextInt();
switch (choice) {
case 1: statement();
break;
case 2: deposit();
break;
case 3: withdraw();
break;
case 4: change();
break;
case 5: exit();
break;
default: System.out.println("Incorrect Choice ");
Menu();
}
}
public static void statement() {
switch(index) {
case 0: case 1: case 2: case 3: case 4:
System.out.println("" +username[index]+ ", your balance is: " +balance[index]+ "€");
if (overdraft[index] == true) {
System.out.println("You are entitled to overdraft");
}
else {
System.out.println("You are NOT entitled to overdraft");
}
Menu();
}
}
public static void deposit() {
System.out.println("" +username[index]+ ", how much you wish to deposit?");
int deposit = skener.nextInt();
balance[index] = balance[index] + deposit;
System.out.println("Thank you, you deposited " +deposit+ "€, now you have " +balance[index]+ "€ total");
depositm();
}
public static void depositm(){
System.out.println("\n 1.Deposit more \n 2.Exit to menu");
int more = skener.nextInt();
switch (more) {
case 1: deposit();
break;
case 2: Menu();
default: System.out.println("Wrong choice, please choose again");
depositm();
}
}
public static void withdraw() {
System.out.println("" +username[index]+ ", how much you wish to withdraw?");
int withdraw = skener.nextInt();
if (overdraft[index] == true) {
balance[index] = balance[index] - withdraw;
System.out.println("Thank you, you withdrawed the money, now you have " +balance[index]+ "€");
Menu();
}
if(overdraft[index] == false && balance[index] >= withdraw)
{balance[index] = balance[index] - withdraw;
System.out.println("Thank you, you withdrawed the money, now you have " +balance[index]+ "€");
Menu();
}
else {
System.out.println("You have insufficient funds \nPlease try again");
withdraw();
}
}
public static void change() {
System.out.println("" +username[index]+ ", do you want to change your pin?");
System.out.println("Press 1 to change or 2 to exit to menu");
int change = skener.nextInt();
switch (change) {
case 1: System.out.println("Please enter new PIN");
pin[index] = skener.next();
System.out.println("You successfully changed your PIN");
Menu();
case 2: System.out.println("Your PIN remains unchanged");
Menu();
default: System.out.println("Wrong choice, please choose again");
change();
}
}
public static void exit(){
System.out.println("Goodbye " +username[index]+ ", Illuminati Bank wish you all the best \n");
login();
}
public static int checkpin(String x){
while(!x.matches("\\d{4}")){
System.err.println("\n Error.\n Please enter 4 digit pin.");
login();
}
return 0;
}
}
公共类Banka{
静态扫描仪skener=新扫描仪(System.in);
静态字符串引脚[]={“1234”、“2345”、“3456”、“4567”、“5678”};
静态字符串用户名[]={“Mate”、“Nathan”、“John”、“Michelle”、“Angelina”};
静态整数余额[]={200100250150300};
静态布尔透支[]={true,true,false,true,false};
静态整数指数;
公共静态void main(字符串[]args){
登录();
}
公共静态void登录(){
日期基准=新日期();
系统输出打印项次(“+基准);
System.out.println(“-----------------------------------”);
System.out.println(“欢迎来到光明会银行\n请使用您的PIN登录”);
String login=skener.next();
checkpin(登录);
对于(int i=0;i=取款)
{余额[索引]=余额[索引]-收回;
System.out.println(“谢谢你,你取款了,现在你有了“+余额[指数]+”€”);
菜单();
}
否则{
System.out.println(“您的资金不足\n请重试”);
撤回();
}
}
公共静态无效更改(){
System.out.println(“+username[index]+”,是否要更改pin?”);
System.out.println(“按1更改或按2退出菜单”);
int change=skener.nextInt();
开关(更换){
案例1:System.out.println(“请输入新PIN”);
pin[index]=skener.next();
System.out.println(“您成功更改了PIN”);
菜单();
案例2:System.out.println(“您的PIN保持不变”);
菜单();
默认值:System.out.println(“选择错误,请重新选择”);
改变();
}
}
公共静态无效退出(){
System.out.println(“再见”+用户名[索引]+”,光明会银行祝您一切顺利\n);
登录();
}
公共静态整数校验码(字符串x){
而(!x.matches(\\d{4}”)){
System.err.println(“\n错误。\n请输入4位pin”);
登录();
}
返回0;
}
}
所以,如果有人能帮助我,如何用用户输入验证所有其他方法,其中int是最好的 试试这个
String input = "xxxx";
int pin;
try{
pin = Integer.parseInt(input);
}catch(NumberFormatException e){
// input contains letters or symbols.
}
或者这是另一个使用该类的
编辑:回复评论 您可以这样修改您的方法
public static void checkpin(String x) {
if (x.length() == 4) {
try {
Integer.parseInt(x);
login();
} catch (NumberFormatException e) {
System.err.println("\n Error.\n Invalid pin.");
}
} else {
System.err.println("\n Error.\n Please enter 4 digit pin.");
}
}
这样,仅当pin有4位数字且所有数字都是数字时才会调用方法
login()
。因此,您得到的是字符串输入,并且希望验证字符串仅包含数字?检查Integer.parseInt(您的字符串)
是否引发NumberFormatException
。如果输入字符串不是有效的整数,则抛出该异常,您可以捕获该异常并根据lyint deposit=skener.nextInt()采取操作;如何验证这一点
public static void checkpin(String x) {
if (x.length() == 4) {
try {
Integer.parseInt(x);
login();
} catch (NumberFormatException e) {
System.err.println("\n Error.\n Invalid pin.");
}
} else {
System.err.println("\n Error.\n Please enter 4 digit pin.");
}
}