import java.util.Scanner;
public class Validator
{
//Main method
public static void main(String[] args)
{
//Creates a scanner for user input
Scanner input = new Scanner(System.in);
//Get credit card number from user
System.out.println("Enter a 16 digit credit card # ");
long cCNum = input.nextLong();
long sum = sumOfEvenPlace(cCNum) + sumOfOddPlace(cCNum) + 1;
if (isValid(sum))
{ System.out.println("Number Is Valid ");
} else
{
System.out.println("Number Is Invalid ");
}
}
// Return true if card is valid
public static boolean isValid(long sum)
{
if (sum % 10 == 0)
{ return true;
} else
{
return false;
}
}
// Return sum of even placed numbers
public static int sumOfEvenPlace(long cCNum)
{
int total = 0;
String digits = Long.toString(cCNum);
for (int i = digits.length() - 2; i > -1; i -= 2)
{ total += Integer.parseInt("" + digits.charAt(i));
}
return total;
}
/* Adds single digits from step 1 and if number has 2
digits it adds them to make a single digit */
public static int getDigit(int cCNum)
{
if (cCNum > 9)
{ return (cCNum % 10 + cCNum / 10);
}
else
{
return cCNum;
}
}
// return sum of odd placed digits in cCNum
public static int sumOfOddPlace(long cCNum)
{
int total = 0;
String digits = Long.toString(cCNum);
for (int i = digits.length() - 1; i > 0; i -= 2)
{ total += Integer.parseInt("" + digits.charAt(i));
}
return total;
}
}
6 Replies - 6709 Views - Last Post: 16 December 2012 - 05:09 AM
#1
How to use getDigit method in creditCardValidator
Posted 15 October 2012 - 10:18 PM
I have my program working fine as far as I can tell, however my Professor asked where the getDigit method was called from. Honestly it isn't called from anywhere, although I am trying to incorperate it somehow. Any suggestions would be appreciated, thank you!
Replies To: How to use getDigit method in creditCardValidator
#2
Re: How to use getDigit method in creditCardValidator
Posted 15 October 2012 - 11:42 PM
If your code is working fine as it is, then why are you trying to use an unnecessary method? Why not just remove it entirely? If the method is meant to be there, as a part of the project requirements, then your code is obviously not fine by the mere fact that the method is not used anywhere.
So, if the latter is true, then what is the purpose of the method supposed to be? Why is it meant to be there?
So, if the latter is true, then what is the purpose of the method supposed to be? Why is it meant to be there?
#3
Re: How to use getDigit method in creditCardValidator
Posted 16 October 2012 - 01:19 PM
The method is a requirement and is suppose to take the even place digits and sum them up; if there is two digits aka 10, it adds the two digits to make a single digit aka 1+0=1
/* Adds single digits from step 1 and if number has 2
digits it adds them to make a single digit */
public static int getDigit(int cCNum)
{
if (cCNum > 9)
{ return (cCNum % 10 + cCNum / 10);
}
else
{
return cCNum;
}
}
This post has been edited by Atli: 16 October 2012 - 01:50 PM
#4
Re: How to use getDigit method in creditCardValidator
Posted 16 December 2012 - 03:45 AM
Hey DIC Lover Atli thanks for nothing... Sincerely, DIC Head JavaBeanz...
I fixed my program by the way no thanks to you. When people are trying to learn how to code and seek the wisdom of knowledgeable programmers, why don't you get the stick out and lend a helping hand. Not everyone is as smart as you when it comes to the Java language.
I fixed my program by the way no thanks to you. When people are trying to learn how to code and seek the wisdom of knowledgeable programmers, why don't you get the stick out and lend a helping hand. Not everyone is as smart as you when it comes to the Java language.
#5
Re: How to use getDigit method in creditCardValidator
Posted 16 December 2012 - 04:16 AM
Well, I'm glad you solved it anyway.
As for why I didn't help you. I simple didn't know what you were asking for. Your response to my question about it basically just reiterated what was already written in the comments. I still don't have any clue how the method you described can be utilized in the main code, given that the code is working as it is.
With that said, keep in mind that we are all here to try and help. If we don't respond to your question it's usually because you've not provided us with enough information to actually do that. It's not because we're to busy being superior and unwilling to help. If that was the case, we wouldn't be here in the first place. If you don't get an answer, try clarifying the question.
As for why I didn't help you. I simple didn't know what you were asking for. Your response to my question about it basically just reiterated what was already written in the comments. I still don't have any clue how the method you described can be utilized in the main code, given that the code is working as it is.
With that said, keep in mind that we are all here to try and help. If we don't respond to your question it's usually because you've not provided us with enough information to actually do that. It's not because we're to busy being superior and unwilling to help. If that was the case, we wouldn't be here in the first place. If you don't get an answer, try clarifying the question.
#6
Re: How to use getDigit method in creditCardValidator
Posted 16 December 2012 - 04:29 AM
Java_Beanz, on 16 December 2012 - 05:45 AM, said:
Hey DIC Lover Atli thanks for nothing... Sincerely, DIC Head JavaBeanz...
I fixed my program by the way no thanks to you. When people are trying to learn how to code and seek the wisdom of knowledgeable programmers, why don't you get the stick out and lend a helping hand. Not everyone is as smart as you when it comes to the Java language.
I fixed my program by the way no thanks to you. When people are trying to learn how to code and seek the wisdom of knowledgeable programmers, why don't you get the stick out and lend a helping hand. Not everyone is as smart as you when it comes to the Java language.
That was unnecessary, ungracious, and just plain rude!
The volunteers here provide the best advice they can with the information given. All questions are not as clear as they could be, not all questions or answers are interpreted in the way intended by the poster, and as a result, not all answers are what the OP was hoping for. Be thankful for any effort, but if the effort was not what was needed, answer any questions asked and attempt to improve the responses by improving the question.
#7
Re: How to use getDigit method in creditCardValidator
Posted 16 December 2012 - 05:09 AM
http://technojeeves....hn-algo-in-java could be of interest
Page 1 of 1

New Topic/Question
Reply


MultiQuote



|