Question
Java I have this code import java.io.DataInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; public class CreditCard { public static void main(String[] args) throws
Java I have this code
import java.io.DataInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; public class CreditCard { public static void main(String[] args) throws IOException { Scanner keyboard = new Scanner(System.in); System.out.println("Enter a filename"); String input = keyboard.nextLine(); try { FileInputStream basic = new FileInputStream(input); DataInputStream inputFile = new DataInputStream(basic); while (inputFile.available() > 0) { long data = inputFile.readLong(); String s = String.valueOf(data); int sum = 0; int len = s.length(); int sp = 0; for (int i = len - 2; i >= 0; i -= 2) { int d = (s.charAt(i) - '0') * 2; d = d % 10 + (d / 10) % 10; sum += d; } sp = 0; for (int i = len - 1; i >= 0; i -= 2) { sum += s.charAt(i) - '0'; } System.out.printf("Credit card number: %s ", s); System.out.printf("Checksum: %d ", sum); System.out.print("Card status: "); if (sum % 10 == 0) System.out.println("VALID"); else System.out.println("INVALID"); s = String.valueOf(data); } inputFile.close(); } catch (FileNotFoundException e) { System.out.println("File not found."); } } }
I need the format to match the required output
Enter a filename credit-cards-1.datENTER Credit card number: 3782 8224 6310 005 Checksum: 60 Card status: VALID Credit card number: 3714 4963 5398 431 Checksum: 80 .......
however,
the numbers come out as
378282246310005
I need it formatted
3782 8224 6310 005
can you please tell me where I need the change my coding to match the spacing of the required output.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started