-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathISBN.java
More file actions
110 lines (102 loc) · 3.2 KB
/
Copy pathISBN.java
File metadata and controls
110 lines (102 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* ISBN.java
* A program for calculating the 13th and 10th digit of ISBN number.
* Part of homework 1, problem 2
*/
import java.util.Scanner;
public class ISBN {
public static void main (String[] args){
Scanner scan = new Scanner(System.in);
boolean corrISBN = false;
while (!corrISBN){
System.out.println("Enter the ISBN");
String isbn = scan.nextLine();
if (ISBNNumisInt(isbn)){
if (isbn.length() == 9){
int isbnInt = Integer.parseInt(isbn);
String tenthDig = isbn10(isbnInt);
System.out.println("Tenth digit is " + tenthDig);
corrISBN = true;
} else if (isbn.length() == 12){
long isbnLong = Long.parseLong(isbn);
String thirteenthDig = isbn13(isbnLong);
System.out.println("Thirteenth digit is " + thirteenthDig);
corrISBN = true;
} else {
System.out.println("Invalid ISBN");
}
} else {
System.out.println("Invalid ISBN");
}
} scan.close();
}
/**
* checks if the user input is a character or integer.
* @param ISBN user input taken from main
* @return true if the input is integer and false for character.
*/
public static boolean ISBNNumisInt(String ISBN){
for (int i = 0; i < ISBN.length(); i++){
if (!Character.isDigit(ISBN.charAt(i))){
return false;
}
}
return true;
}
/**
* sample ISBN 019853453
* 10th digit should be 1
* @param isbn takes in the nine digit ISBN
* @return
*/
public static String isbn10 (int isbn){
int divident = 100000000;
int singleDig = 0;
int calcTenth = 0;
for (int i = 1; i < 10; i ++)
{
singleDig = isbn/ divident;
isbn = isbn - (singleDig * divident);
calcTenth = calcTenth + (i * singleDig);
divident = divident / 10;
}
calcTenth = calcTenth % 11;
if (calcTenth == 10)
{
return ("X");
} else
{
return Integer.toString(calcTenth);
}
}
/**
* Sample ISBN digit 978156619909
* Thirteenth digit should be 4
* @param isbn takes in the twelve ISBN
* @return Thriteenth digit
*/
public static String isbn13 (long isbn){
long divident = 100000000000L;
long singleDig = 0L;
long calcThirteenth = 0L;
for (int i = 1; i < 13; i++)
{
singleDig = isbn/ divident;
isbn = isbn - (singleDig * divident);
if (i%2 == 0 && i != 1){
calcThirteenth = calcThirteenth + (3*singleDig);
} else{
calcThirteenth = calcThirteenth + singleDig;
}
divident = divident / 10;
}
calcThirteenth = 10 - (calcThirteenth % 10);
if (calcThirteenth == 10)
{
return "0";
} else
{
return Long.toString(calcThirteenth);
}
}
}