Question

In: Computer Science

Can you please modify where I'm going wrong (regarding Problem #2 I posted earlier; i.e., credit...

Can you please modify where I'm going wrong (regarding Problem #2 I posted earlier; i.e., credit card validation)? Please fix my code using the general format that reflects my current level of coding/where I am in the learning process:

import java.util.Scanner;

public class AmExValidator{ public static void main(String[] args) {

Scanner amex = new Scanner(System.in);

System.out.print("Enter credit card number for validation: ");

String ccNum = amex.nextString();

//cc = credit card number as a string (not #)

//checking for cc length (15) and whether first 2 digits = 34 or 37

if (ccNum.length() != 15){

System.out.println("Invalid.");

}

else if (ccNum.substring(0, 1) == "34") || (ccNum.substring(0, 1) == "37"); //I'm receiving syntax errors here (enum, class or interface expected; as well as illegal start of expression highlighting || )

for (int i = 0; i < ccNum.length(); i++) {

char thisChar = ccNum.charAt(i);    //don't want char 1, you want the value/#

int thisCharasInt = Integer.parseInt("" + thisChar);

System.out.println(thisCharAsInt);

System.out.println("Valid.");

}

}

}

else { System.out.println("Invalid.");

} //receiving error messages here too

}

}

Solutions

Expert Solution

Correct Code:

import java.util.Scanner;

public class AmExValidator{
public static void main(String[] args) {

Scanner amex = new Scanner(System.in);

System.out.print("Enter credit card number for validation: ");

String ccNum = amex.nextLine();

//cc = credit card number as a string (not #)

//checking for cc length (15) and whether first 2 digits = 34 or 37
if (ccNum.length() != 15){

System.out.println("Invalid.");

}

else if( (ccNum.substring(0, 2).equals("34")) || (ccNum.substring(0, 2).equals("37")) ){ // checking if first two characters are 37 or 34

for (int i = 0; i < ccNum.length(); i++) {

char thisChar = ccNum.charAt(i);

int thisCharasInt = Integer.parseInt("" + thisChar);
// printing each character
System.out.println(thisCharasInt);

System.out.println("Valid.");

}

}


else {

System.out.println("Invalid.");

}

}

}

Explanation: First error you are getting in else if statement is because you have not placed both conditions in one bracket because of which, if is taking first bracket condition as it condition and is not able to whatever is given after that. the second error you are getting is because you have used else statement without if statement, as you have finished the else, if ladder by putting a semicolon at the end of else if statement and placed for loop between else if and else statement.  

Sample output:

Note: Let me know if you have any other doubt.


Related Solutions

C++ program please, can you show me where I did wrong. I'm trying to print the...
C++ program please, can you show me where I did wrong. I'm trying to print the counter-clockwise spiral form using int *p, but my output turned out weird, thank you void makeSpiral(int *p, int rows, int cols) { int left = 0, value = 1, top = 0; while(left < cols && top < rows) { for(int i = top;i < rows;++i) { *(p+i*cols+left) = value++; } left++; for(int i = left;i < cols;++i) { *(p+(rows-1)*cols+i) = value++; } rows--;...
This is for an accounting assignment and I'm not sure where I'm going wrong. I'll copy...
This is for an accounting assignment and I'm not sure where I'm going wrong. I'll copy and paste what I have and the directions as best as possible. PLEEEASE HELP: June 22: Received a bill for $1,190 from Computer Parts and Repair Co. for repairs to the computer equipment. It's telling me my rep and maintenance expense is wrong. I entered: Repairs & Maint. Expense 1190 Accounts payable 1190 It's for Byte of Accouting. What else would this transaction be...
I posted this question before too but answer was wrong can you please make sure the...
I posted this question before too but answer was wrong can you please make sure the answer is right Required information [The following information applies to the questions displayed below.] In 2018, the Westgate Construction Company entered into a contract to construct a road for Santa Clara County for $10,000,000. The road was completed in 2020. Information related to the contract is as follows: 2018 2019 2020 Cost incurred during the year $ 2,204,000 $ 3,192,000 $ 2,424,400 Estimated costs...
For this assignment, I'm going to give a series of problem statements and you need to...
For this assignment, I'm going to give a series of problem statements and you need to state whether the problem would be more appropriately solved with a linear or logistic regression. You also have to give a short defense of why this is the correct regression for each one (only need a sentence or two for each). I have a dataset of soil sample attributes (acidity, density, clay content, etc.) and whether Kentucky bluegrass successfully grew in the soil. I...
I posted this question before and the person who answered it answered wrong.........please have someone else...
I posted this question before and the person who answered it answered wrong.........please have someone else try again The following information applies to the questions displayed below.] O’Brien Company manufactures and sells one product. The following information pertains to each of the company’s first three years of operations: Variable costs per unit: Manufacturing: Direct materials $28 Direct labor $15 Variable manufacturing overhead $5 Variable selling and administrative $3 Fixed costs per year: Fixed manufacturing overhead $580,000 Fixed selling and administrative...
Suppose I'm going to do a study to see if I can use the high school...
Suppose I'm going to do a study to see if I can use the high school GPA of college students to predict their college GPA. Please let me know What are the cases? What would be an appropriate label? Give four examples of variables I could use and state whether they are categorical or quantitative variables and also state possible values each variable can take:
please I want it to step by step and in word posted so I can read...
please I want it to step by step and in word posted so I can read them. Q: If the average realized return of a portfolio is 27.5% per year, the standard deviation of returns is 50%, the portfolio beta is 1.25, the average return of Treasury bills over the same period is 2.5% per year, and the average return on the market is 12.5% per year Calculate i) the Sharpe; ii) Treynor and iii) Jensen    
Can you please see what I have done wrong with my program code and explain, This...
Can you please see what I have done wrong with my program code and explain, This python program is a guess my number program. I can not figure out what I have done wrong. When you enter a letter into the program, its supposed to say "Numbers Only" as a response. I can not seem to figure it out.. instead of an error message. import random def menu(): print("\n\n1. You guess the number\n2. You type a number and see if...
I'm Getting an "unindented error" Please fix the bolded codes. Because I don't know whats going...
I'm Getting an "unindented error" Please fix the bolded codes. Because I don't know whats going on. Thank You. # This program exercises lists. # The following files must be in the same folder: # abstractcollection.py # abstractlist.py # arraylist.py # arrays.py # linkedlist.py # node.py # input.txt - the input text file. # Input: input.txt # This file must be in the same folder. # To keep things simple: # This file contains no punctuation. # This file contains...
Case 2 (Ms. Brown continued, I need answers typed please I posted case 1 in the...
Case 2 (Ms. Brown continued, I need answers typed please I posted case 1 in the end if you need it). Patient: Brown, Francine DOB: 04/25/44 Medical Record Number: 09052016 Allergies: NKA Precautions: Fall Height: 5’ 2” Weight: 60.4 kg Ms. Brown condition improves, and she is discharged home after her admission to the hospital for her CVA. She has residual right-sided weakness in her arm and leg and a right-sided visual field cut. Ms. Brown is prescribed the following...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT