Click here to Skip to main content
15,889,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 :
C#
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.";
   }
}
Posted
Updated 11-May-14 6:40am
v3
Comments
[no name] 11-May-14 11:19am    
Remove the extra space character at the end of your string. A space is not a number.
mhassan083 11-May-14 13:15pm    
when remove space ...give me error (incompatible type)
phil.o 12-May-14 4:45am    
Why do you parse the string to an integer, since you still have access to the integer variable you used to create it?

Why, I have to ask, are you using parseInt anyway?
C#
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 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!
 
Share this answer
 
Comments
mhassan083 11-May-14 11:44am    
if i remove Integer.parseIn() ....give me error!
because a is a string.
OriginalGriff 11-May-14 11:55am    
Yes, I know - because you don't want to use the string at all.
Read what I said again.
mhassan083 11-May-14 12:47pm    
i try but ,i cant ,i'm beginner
OriginalGriff 12-May-14 4:06am    
Why not? You have an integer value.
You know how to divide by ten.
I assume you know what a modulus does?
mhassan083 12-May-14 6:40am    
thanks
You just need to remove the space, but still keep the quotation mark
C#
public static String checkDigits(int cardNo)
   {
      String a = new String ();
      a = cardNo + "";
 
Share this answer
 
Comments
mhassan083 12-May-14 6:40am    
thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900