Java 处理多个前导零

Java 处理多个前导零,java,Java,我正在编写一个计算校验和的程序,但需要删除前面的所有前导零。我知道如何只删除一个,但如何将它们全部删除 以下是我目前掌握的情况: Scanner scan = new Scanner(System.in); System.out.print("Enter the first 9 digits of an ISBN as an integer: "); ISBN = scan.nextInt(); /****************************************

我正在编写一个计算校验和的程序,但需要删除前面的所有前导零。我知道如何只删除一个,但如何将它们全部删除

以下是我目前掌握的情况:

 Scanner scan = new Scanner(System.in);
  System.out.print("Enter the first 9 digits of an ISBN as an integer: ");
  ISBN = scan.nextInt();


  /******************************************************************************
  *                           Processing Section                              *
  ******************************************************************************/
  processingISBN = ISBN;     
  sum = 0;
  for (int i = 2; i <= 10; i++) 
  {
     digit = processingISBN % 10;  // digit at the end
     sum = sum + i * digit;
     processingISBN = processingISBN / 10;
  }
  firstDigit = ISBN / 100000000; // grab first digit (in case of zero)

  /******************************************************************************
  *                              Outputs Section                                *
  ******************************************************************************/

   // print out check sum number, use X for 10



  if (firstDigit == 0)
  {
     System.out.print("The ISBN-10 number is 0" + ISBN);
  }
  if (firstDigit != 0)
  {
     System.out.print("The ISBN-10 number is " + ISBN);
  }

  if(sum % 11 == 1) //checks for checksum=10 
  {
     System.out.print("X");
  }
  else if (sum % 11 == 0) 
  {
     System.out.print("0");
  }
  else                    
  {
     System.out.print(11 - (sum % 11)); 
  }
Scanner scan=新的扫描仪(System.in);
System.out.print(“将ISBN的前9位输入为整数:”;
ISBN=scan.nextInt();
/******************************************************************************
*加工科*
******************************************************************************/
处理ISBN=ISBN;
总和=0;

对于(int i=2;i尝试此操作,将ISBN构建为字符串,然后运行此操作:

String yourInputAsString = "0000002514";
int yourInputAsInt;
Pattern p = Pattern.compile("^\\d+$");
Matcher m = p.matcher(yourInputAsString);

if(m.matches()){
    yourInputAsInt = Integer.valueOf(yourInputAsString.replaceAll("^0+", ""));
    System.out.println("As String: " + yourInputAsString.replaceAll("^0+", ""));
    System.out.println("As Int: " + yourInputAsInt);

        //do check

} else {
    System.out.println(yourInputAsString);
}
输出:

As字符串:2514

As Int:2514


如果在某人输入
“00001278126”
时检查大小写,您将遇到两个问题:首先,前导零表示八进制(基数8),因此您将希望捕获字符串。其次,整数不关心前导零,因此00001278126无论如何都会变成1278126。我的Java很臭,但使用正则表达式提前处理ISBN,它们会摇摆。删除前导0的
ISBN=ISBN.replaceAll(^0),“”)
。我知道这样做不对,但还是玩玩吧。