At this point I'm fairly certain the code will run properly once I clear up all syntax errors. However I'm not really sure how to do this as I am not entirely familiar with Java. Any help is appreciated.
Thanks for your help everyone I'm now using NetBeans. I'm seeing lots of errors but I'm not sure how to fix them. The most common is "class, interface, or enum expected" ?
import javax.swing.*;
public class Main {
public static void main(String[] args) {
String cCNum;
int count= 0;
do {
cCNum= JOptionPane.showInputDialog(null,"Please enter a 13 or 16 digit" +
" credit card number");
count++;
if ( cCNum.length() < 13 )
cCNum= JOptionPane.showMessageDialog(null, "The credit card number is" +
" too small please look at the number and re-enter it");
else if ( cCNum.length() >16 )
cCNum= JOptionPane.showMessageDialog(null, "The credit card number is" +
"too large please look at the number and re-enter it");
}
while ( (cCNum.length() < 13) || (cCNum.length() > 16) && (count < 3) && validInt (cCNum) ));
if (count > 3){
JOptionPane.showInputDialog(null,"The numbers you entered were not " +
"valid.\nGoodbye.");
System.exit(0);
}
else {
JOptionPane.showMessageDialog(null,"Your credit card number is:"+ cCNum);
JOptionPane.showMessageDialog(null,"Your card is a" + cCType (cCNum) );
JOptionPane.showMessageDialog(null,"Your card is" + isValid(long.parselong(cCNum)) );
}
public static String cCType (String cC){
if (cC.startsWith("4")){
return "Visa";
}
else if (cC.startsWith("5")){
return "MasterCard";
}
else if (cC.startsWith("6")){
return "Discover";
}
else if (cC.startsWith("37")){
return "American Express";
}
else
return "Unknown card type";
}
public static boolean validInt(String num ){
try {
long.parselong (num);
return num.length()== 13 || num.length()== 16;
}catch(NumberFormatException e){
//If it is not a valid number return false.
return false;
}
}
public static boolean isValid (long num){
boolean error = false;
if ((sumOfEvenPlaces(num)+ sumOfOddPlaces (num))% 10) == 0)
return true;
else
return error;
}
public static int sumOfEvenPlaces (long num) {
int total= 0;
int add= 0;
number = (num)/10;
while (num !=0){
add= getDigits((num %10))*2;
total+=add ;
number = (num)/100;
}
return total;
}
public static int getDigits(int num) {
if ( num >= 10 ) {
return (num % 10);
}
else
return num;
}
public static int sumOfOddPlace(long num) {
int total = 0;
while (num > 0) {
sum += ( number % 10 );
num = (num)/100;
}
return total;
}
}
}
-
You should give more details about what is wrong. But as far as I can see...
a) you're not closing cCType method. You forgot to put "}" before the declaration of the validInt method.
b) also, you're not closing quotes in the "unknow card type".
public static String cCType (String cCNum) if (cCNum.charAt(0)=="4") return "Visa"; else if (cCNum.charAt(0)=="5") return "MasterCard"; else if (cCNum.charAt(0)=="6") return "Discover"; else if (cCNum.charAt(0)=="37") return "American Express"; else return "Unknown card type;" // You forgot this. } // and thisEddie : There are way more errors than just this. For example: cCNum.charAt(0)=="4"Paulo Guedes : @Eddie good point, Eddie. Didn't notice. :-) -
There are many syntax errors in the code you have provided us, so it's difficult to really find your root problem:
long.parselong(cCNum);instead ofLong.parseLong(cCNum);JOptionPane.showMessageDialogtakes two arguments, not just one.cCNum.charAt(0) == "4"doesn't work because you're comparing a char to a 1 byte stringcCNum.charAt(0) == "37"doesn't work because you're comparing a char to a two-byte string- You have a
do { ... }that is missingwhile ....; - Parenthesis not balanced:
while (cCNum.length() < 13) || (cCNum.length() > 16) && (num < 3) && validInt (cCNum) )should bewhile ( (cCNum.length() < 13) || (cCNum.length() > 16) && (num < 3) && validInt (cCNum) ) - JOptionPane.showMessageDialog(null,"Your card is a" + cCType ); uses an undefined variable
cCType - You didn't close the method before
public static String cCType (String cCNum) public static String cCType (String cCNum)does not have its opening and closing curly bracesreturn "Unknown card type;"should bereturn "Unknown card type";cCNum.charAt(0)=="4"should be eithercCNum.charAt(0)=='4'orcCNum.startsWith("4")(and so on for the next few cases)cCNum.charAt(0)=="37"should becCNum.startsWith("37")- In
public static boolean validInt(String num)you then defineint num;where you probably wantlong number; public static boolean validInt(String num )is missing two closing curly braces -- one for the try/catch and one for the method close brace.- There are two too many closing braces before
sumOfEvenPlaces, causing it to be declared outside the class. - In
sumOfEvenPlaces()you usenumberwithout declaring it. - In
sumOfEvenPlaces()you are missing the semi-colon on the return statement as well as ontotal+=add - In
sumOfEvenPlaces()you usegetDigitalthough your method isgetDigits. - In
sumOfOddPlace()you usenumberandsumwithout defining or initializing them. - You define
sumOfOddPlace()but usesumOfOddPlaces(). - in
isValidyour parenthesis don't balance. - There are general problems with int and longs being interchanged where they shouldn't be.
I recommend that you use an IDE for Java (or C# or any other sufficiently advanced language) development. It will help a LOT with these sorts of errors, making them jump out and be more obvious. For Java, there are two free high quality IDEs: NetBeans (from Sun) and Eclipse.
Bombe : +1 for total pwnage. :)Ray Hidayat : Wow that's amazing!Andy White : Yowsa, that earns a +1 -
Don't roll your own code.
Use an existing one from Apache Commons Validator
http://commons.apache.org/validator/api-1.3.1/org/apache/commons/validator/CreditCardValidator.html
Learning : Looking at the quality of the code , this is indeed an appropriate answer! +1
0 comments:
Post a Comment