Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning ASP.NET 2.0 And Databases

Authors: John Kauffman, Bradley Millington

1st Edition

0471781347, 978-0471781349

More Books

Students also viewed these Databases questions