-
-
Save bytecodeman/d98389bb704d08fadd2feddcda0414c9 to your computer and use it in GitHub Desktop.
CSC-111 HW6 ISBN Number Generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* A.C. Silvestri | |
* CSC 111 | |
* 11/04/2018 | |
* Generate Proper ISBN Numbers | |
* silvestri@stcc.edu | |
* Check using: http://www.hahnlibrary.net/libraries/isbncalc.html | |
*/ | |
import java.util.Scanner; | |
public class ISBNGenerator { | |
public static void main(String[] args) { | |
Scanner input = new Scanner(System.in); | |
String isbn; | |
boolean isValid; | |
do { | |
System.out.print("Enter 12 digits: "); | |
isbn = input.nextLine().trim(); | |
if (isbn.length() == 12) { | |
isValid = true; | |
for (int i = 0; isValid && i < 12; i++) | |
if (!Character.isDigit(isbn.charAt(i))) | |
isValid = false; | |
} | |
else | |
isValid = false; | |
if (!isValid) | |
System.out.println("Sorry, bad input."); | |
} while (!isValid); | |
// If execution comes here, it's guaranteed to be a | |
// 12 digit string | |
int sum = 0; | |
for (int i = 0; i < isbn.length(); i++) { | |
int digitValue = isbn.charAt(i) - '0'; | |
if (i % 2 == 0) | |
sum += digitValue; | |
else | |
sum += 3 * digitValue; | |
} | |
int checkSum = 10 - sum % 10; | |
isbn += checkSum == 10 ? '0' : (char)(checkSum + '0'); | |
System.out.printf("ISBN = %s\n", isbn); | |
input.close(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* A.C. Silvestri | |
* CSC 111 | |
* 11/04/2018 | |
* Generate Proper ISBN Numbers | |
* silvestri@stcc.edu | |
* Check using: http://www.hahnlibrary.net/libraries/isbncalc.html | |
*/ | |
import java.util.Scanner; | |
public class ISBNGenerator2 { | |
public static void main(String[] args) { | |
Scanner input = new Scanner(System.in); | |
String isbn; | |
do { | |
System.out.print("Enter 12 digits: "); | |
isbn = input.nextLine().trim(); | |
if (isbn.length() == 12) { | |
boolean isValid = true; | |
for (int i = 0; isValid && i < 12; i++) | |
if (!Character.isDigit(isbn.charAt(i))) | |
isValid = false; | |
if (isValid) | |
break; | |
} | |
System.out.println("Sorry, bad input."); | |
} while (true); | |
// If execution comes here, it's guaranteed to be a | |
// 12 digit string | |
int sum = 0; | |
for (int i = 0; i < isbn.length(); i++) { | |
int digitValue = isbn.charAt(i) - '0'; | |
if (i % 2 == 0) | |
sum += digitValue; | |
else | |
sum += 3 * digitValue; | |
} | |
int checkSum = 10 - sum % 10; | |
isbn += checkSum == 10 ? '0' : (char)(checkSum + '0'); | |
System.out.printf("ISBN = %s\n", isbn); | |
input.close(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* A.C. Silvestri | |
* CSC 111 | |
* 11/04/2018 | |
* Generate Proper ISBN Numbers | |
* silvestri@stcc.edu | |
* Check using: http://www.hahnlibrary.net/libraries/isbncalc.html | |
*/ | |
import java.util.Scanner; | |
public class ISBNGenerator3 { | |
private static boolean isISBNStringValid(String isbn) { | |
boolean isValid; | |
if (isbn.length() == 12) { | |
isValid = true; | |
for (int i = 0; isValid && i < 12; i++) | |
if (!Character.isDigit(isbn.charAt(i))) | |
isValid = false; | |
} | |
else | |
isValid = false; | |
return isValid; | |
} | |
private static int calcCheckSum(String isbn) { | |
int sum = 0; | |
for (int i = 0; i < isbn.length(); i++) { | |
int digitValue = isbn.charAt(i) - '0'; | |
if (i % 2 == 0) | |
sum += digitValue; | |
else | |
sum += 3 * digitValue; | |
} | |
int checkSum = 10 - sum % 10; | |
return checkSum; | |
} | |
public static void main(String[] args) { | |
Scanner input = new Scanner(System.in); | |
String isbn; | |
boolean isValid; | |
do { | |
System.out.print("Enter 12 digits: "); | |
isbn = input.nextLine().trim(); | |
isValid = isISBNStringValid(isbn); | |
if (!isValid) | |
System.out.println("Sorry, bad input."); | |
} while (!isValid); | |
// If execution comes here, it's guaranteed to be a | |
// 12 digit string | |
int checkSum = calcCheckSum(isbn); | |
isbn += checkSum == 10 ? '0' : (char) (checkSum + '0'); | |
System.out.printf("ISBN = %s\n", isbn); | |
input.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment