Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Name: Omar Natour
- * Date: 10/26/15
- * Course Number: CSC-111
- * Course Name: Introduction to Java Programming
- * Problem Number: 5.47
- * Email: ojnatour0001@student.stcc.edu
- * Problem Description: Use the first 12 digits of the isbn number to find the 13th.
- */
- import java.util.Scanner;
- public class HomeWork6 {
- public static void main(String args[]) {
- final String TITLE = "The ISBN-13 calculator";
- final String CONTINUE_PROMPT = "Do this again? [y/N] ";
- System.out.println("Welcome to " + TITLE);
- Scanner sc = new Scanner(System.in);
- do {
- System.out.print("Enter the first 12 digits of an ISBN-13 as a string:");
- System.out.println(calc(sc.nextLine()));
- } while (doThisAgain(sc, CONTINUE_PROMPT));
- sc.close();
- System.out.println();
- System.out.println("Thank you for using " + TITLE);
- }
- // ************************************************************************
- private static boolean doThisAgain(Scanner sc, String prompt) {
- System.out.print(prompt);
- String doOver = sc.nextLine();
- return doOver.equalsIgnoreCase("Y");
- }
- // ************************************************************************
- private static String calc(String twelve) {
- int sum = 0;
- String out = "";
- if (twelve.length() != 12)
- return twelve + " is an invalid input.";
- else {
- for (int i = 0; i < twelve.length(); i++) {
- int asci = twelve.charAt(i);
- if (!(asci >= 48 && asci <= 57))
- return twelve + " is an invalid input.";
- else {
- int dig = -48 + asci;
- out += dig;
- if (i % 2 == 0)
- sum += dig;
- else
- sum += (3 * dig);
- }
- }
- }
- int back = 10 - sum % 10;
- if (back == 10)
- return "the ISBN-13 number is " + out + 0 + ".";
- else
- return "the ISBN-13 number is " + out + back + ".";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement