简体   繁体   English

我如何比较包含整数的变量

[英]How do i compare variables containing integers

package Tutorial;
import java.util.Scanner;
public class Tutorial {

  public static void main(String[] args) {
  Scanner sc=new Scanner(System.in);
   String name="revolt";
   int password=0123;
   String Name;
   int Password;
   System.out.println("enter name;");
   Name=sc.nextLine();
   if (Name.equals(name)){
   System.out.println("enter password");
   }
   else{
     System.out.println("wrong Name");
   }
   Password=sc.nextInt();
   if (password==Password){
     System.out.println("access granted...");
   }
   else{
     System.out.println("wrong 
Password!");
   }
  }
}

The code doesn't show any error but when I enter password it tells me wrong password even though the password is correct.代码没有显示任何错误,但是当我输入密码时,即使密码正确,它也会告诉我密码错误。

You now know what the issue is but, if you are determined to force a User to provide a numerical password (regardless of the order of digits) then make password a String variable and utilize the Scanner#nextLine() method to get User input.您现在知道问题是什么,但是,如果您决定强制用户提供数字密码(无论数字顺序如何),则将密码 设为String 变量并利用Scanner#nextLine()方法获取用户输入。 Once you have that input, check it using the String#matches() method with a small Regular Expression (regex) to ensure that only numerical digits were supplied, for example:获得该输入后,使用String#matches()方法和一个小的正则表达式(regex) 检查它以确保只提供数字,例如:

/* User must enter an all numerical password that is a 
   minimum of 4 digits to a maximum of 18 digits.  */  
String password = "";
while(password.isEmpty()) {
    System.out.println();
    System.out.println("Enter your numerical Password: --> ");
    System.out.print  ("(enter 'q' to quit): --> ");
    password = sc.nextLine().trim();
    if (password.equalsIgnoreCase("Q")) {
        System.out.println("Quitting ... Bye-Bye");
        System.exit(0);
    }
    // Password must be all numerical, 
    // be a minimum of 4 digits in length, 
    // and be a maximum of 18 digits in length.
    if (!password.matches("\\d+") || password.length() < 4 || password.length() > 18) {
        System.err.println("Invalid Password Supplied (" + password + ")!\n"
                + "A password must contain a 'minimum' of at least four (4)\n"
                + "all numerical digits to a 'maximum' of 18 digits! No alpha\n"
                + "characters or whitespaces are allowed!");
        password = "";
    }
}

The Regular Expression \\\\d+ within the String#matches() method checks to make sure that what is now within the password string variable is indeed a string representation of one or more numerical digits from 0 to 9. A password of 0123 would be considered valid however if you were to parse this to Integer or Long for example, the 0 is omitted. String#matches()方法中的正则表达式\\\\d+检查以确保现在密码字符串变量中的内容确实是从 0 到 9 的一个或多个数字的字符串表示。将考虑密码0123有效但是,如果您要将其解析为 Integer 或 Long,则省略0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何比较整数的字符串表示? - How do I compare strings representation of integers? 对包含字符串和整数的 2D 列表进行排序,按整数 asc 排序我该怎么做? (JAVA) - Sorting a 2D List containing both Strings and Integers, sort by integers asc how do i do? (JAVA) java - 如何在java编程中从包含整数和字符串的文本文件中读取整数? - How do I read integers from a text file containing integers and strings in java programming? 我该如何做一个IF语句来比较分配给1类中2个字符串变量的2个整数,然后在Java中的另一个类中执行代码? - How do I make an IF statement that compares 2 integers assigned to 2 string variables in 1 class then execute the code in another class in Java? 我将如何产生 2 个随机整数,然后比较它们? - how would I produce 2 random integers, and then compare them? 如何正确比较 Java 中的两个整数? - How can I properly compare two Integers in Java? 如何在Java中比较一个数组中的两个整数? - How can I compare two integers in one array in Java? Hibernate-validator:如何使用包含“ $ {…}”格式的变量对消息进行插值? - Hibernate-validator: How do I interpolate messages with variables containing format “${…}”? 如何将整数文本行转换为整数数组? - How do I turn a text line of integers into an array of integers? 如何编写通用Java方法并比较方法中泛型类型的两个变量? - How do I write generic Java method and compare two variables of the generic type inside the method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM