如何在信用卡中解决这个问题,我是java的初学者 [英] How can solve this problem in credit card ,i'm beginner in java

查看:69
本文介绍了如何在信用卡中解决这个问题,我是java的初学者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hieveryone,这段代码给我这个错误

hieveryone, this code give me this error

Exception in thread "main" java.lang.NumberFormatException: For input string: "2315778 "
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.lang.Integer.parseInt(Integer.java:492)
	at java.lang.Integer.parseInt(Integer.java:527)
	at javaapplication1.JavaApplication1.checkDigits(JavaApplication1.java:54)
	at javaapplication1.JavaApplication1.main(JavaApplication1.java:18)
Java Result: 1



我的代码:


my code :

public class JavaApplication1
{
   /**
    * @param args the command line arguments
    */
   public static void main(String[] args)
   {
      int num = 2315778;
      System.out.println("Credit card number: " + num + " is " + checkDigits(num));
      num = 1234567;
      System.out.println("Credit card number: " + num + " is " + checkDigits(num));
      num = 7654321;
      System.out.println("Credit card number: " + num + " is " + checkDigits(num));
      num = 1111111;
      System.out.println("Credit card number: " + num + " is " + checkDigits(num));
   }

   public static int decode(int digit, boolean even)
   {
      int a = 0;
      if (even )
      {
          a = digit * 2;
          if ( a >9)
          {
             a = (a+1) % 10;
          }
          return a;
      }
      else
      {
         return digit;
      }
   }

   public static String checkDigits(int cardNo)
   {
      String a = new String ();
      a = cardNo + " ";
      char [ ] array = a.toCharArray ( );
      int [ ] array2 = new int [7];
      for ( int i = 0; i < array.length; i++)
      {
         array2 [i] = Integer.parseInt (a);
      }
      int [ ] array3 = new int [7];
      boolean even = true;
      for ( int i = 0 ; i <array3.length ; i++)
      {
         even = !even;
         array3 [i] = decode ( array2 [i] , even);
      }

      int sum = 0;
      for ( int i = 0 ; i <array3.length ; i++)
      {
         sum = sum + array3 [i];
      }
      if ( sum % 10 == 0)
         return "The Card number is likely to be valid.";
      else
         return "The card number is not valid.";
   }
}

推荐答案

为什么,我要问,你还在使用parseInt吗?

Why, I have to ask, are you using parseInt anyway?
public static String checkDigits(int cardNo)
{
      String a = new String ();
      a = cardNo + " ";
      char [ ] array = a.toCharArray ( );
      int [ ] array2 = new int [7];
      for ( int i = 0; i < array.length; i++)
      {
          array2 [i] = Integer.parseInt (a);
         }

cardNo是一个int。

array2是一个整数数组。

a是一个包含输入数字的字符串和一个space ...所以你要做的就是尝试将你已经拥有的数字作为int解析为int,并失败,因为你首先在它上面添加了一个空格!

我' m假设你要做的是将输入整数的每个数字都放到一个单独的int中?如果是这样,那么从最低有效数字开始工作,并使用模数运算符'%'一次提取一个数字,然后除以10。你根本不需要使用字符串和字符,你绝对不需要解析任何东西!

cardNo is an int.
array2 is an array of ints.
a is a string that contains the input number and a space...so all you are doing is trying to parse a number that you already have as an int to an int, and failing because you added a space onto it in the first place!
I'm assuming that what you are trying to do is get each digit of the input integer into a separate int? If so, then work from the least significant digit, and use the modulus operator '%' to extract each digit one at a time, then divide by ten. You don't need to faff about with strings and characters at all, and you definitely don't need to parse anything!


你只需要删除空格,但仍然保留引号mark

You just need to remove the space, but still keep the quotation mark
public static String checkDigits(int cardNo)
   {
      String a = new String ();
      a = cardNo + "";


这篇关于如何在信用卡中解决这个问题,我是java的初学者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆