Question

Please fix my code so I can get this output: Enter the first 12-digit of an...

Please fix my code so I can get this output: Enter the first 12-digit of an ISBN number as a string: 978013213080 The ISBN number is 9780132130806
This was my output: 

import java.util.Scanner;

public class Isbn {

    private static int getChecksum(String s) {
        // Calculate checksum
        int sum = 0;
        for (int i = 0; i < s.length(); i++)
            if (i % 2 == 0)
                sum +=  (s.charAt(i) - '0') * 3;
            else
                sum  += s.charAt(i) - '0';

        return 10 - sum;
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the first 12-digit of an ISBN number as a string: ");
        // get a string input
        String s = input.nextLine();

        if (s.length()!= 12) {
            System.out.println(s + " is an invalid input");
            System.exit(1);
        }

        int checksum = getChecksum(s);
        System.out.println("The ISBN number is " + s +
                (checksum == 10 ? "0" : checksum));
    }
}
/*Enter the first 12-digit of an ISBN number as a string: 97801320
97801320 is an invalid input*/
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

public class Exercise {
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter the first 12-digit of an ISBN number as a string: ");
    String s = input.nextLine();

    if (s.length() != 12) {
      System.out.println(s + " is an invalid input");
      System.exit(1);
    }

    // Calculate checksum
    int sum = 0;
    for (int i = 0; i < s.length(); i++)
      if (i % 2 == 0)
        sum += (s.charAt(i) - '0');
      else
        sum += (s.charAt(i) - '0') * 3;

    int checksum = 10 - sum % 10;

    System.out.println("The ISBN number is " + s +
        (checksum == 10 ? "0" : checksum));
}
}


Add a comment
Know the answer?
Add Answer to:
Please fix my code so I can get this output: Enter the first 12-digit of an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • How can I make this program sort strings that are in a text file and not...

    How can I make this program sort strings that are in a text file and not have the user type them in? I need this done by 5:00 AM EST tommorow please. import java.util.*; public class MergeDemo { public static void main(String args[]) { Scanner input=new Scanner(System.in); System.out.print("How many lines to be sorted:"); int size=input.nextInt(); String[] lines=new String[size]; lines[0]=input.nextLine(); System.out.println("please enter lines..."); for(int i=0;i { lines[i]=input.nextLine(); } System.out.println(); System.out.println("Lines Before Sorting:"); System.out.println(Arrays.toString(lines)); mergeSort(lines); System.out.println(); System.out.println("Lines after Sorting:"); System.out.println(Arrays.toString(lines)); } public...

  • Consider the encryption code  Encryption Code true false - stringContains is an encryption algorithm that can be...

    Consider the encryption code  Encryption Code true false - stringContains is an encryption algorithm that can be decrpyted true false - Running reverseString twice will have no effect true false - Running incLetters(s,4) then incLetters(s,-4) will have no effect true false - using maxDigit as an encryption algorighm, it can be decrypted USE CODE BELOW: package encryption; import java.util.Scanner; public class Encryption { public static void main(String[] args) { Scanner scanner = new Scanner (System.in); String password, encryptedPassword, salt; int increment;...

  • draw a flew chart for this code // written by Alfuzan Mohammed package matreix; import java.util.Scanner;...

    draw a flew chart for this code // written by Alfuzan Mohammed package matreix; import java.util.Scanner; public class Matrix {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);    System.out.print("Enter number of rows: first matrix ");    int rows = scanner.nextInt();    System.out.print("Enter number of columns first matrix: ");    int columns = scanner.nextInt();    System.out.print("Enter number of rows: seconed matrix ");    int rowss = scanner.nextInt();    System.out.print("Enter number of columns seconed matrix:...

  • What is wrong with my code? Trying to fix the Boolean situation and it keeps skipping...

    What is wrong with my code? Trying to fix the Boolean situation and it keeps skipping my boolean question and repeating. import java.util.Scanner; public class MyTest{       public static void main (String [] args){               Scanner input = new Scanner(System.in);               System.out.println("JAVA QUIZ");        System.out.println("This quiz includes three questions about the Java Programming Language.");        System.out.println("Each question has 4 possible answers. (numbered 1,2,3,4)");        System.out.println("Enter 0 to exit the test.");...

  • Java. Java is a new programming language I am learning, and so far I am a...

    Java. Java is a new programming language I am learning, and so far I am a bit troubled about it. Hopefully, I can explain it right. For my assignment, we have to create a class called Student with three private attributes of Name (String), Grade (int), and CName(String). In the driver class, I am suppose to have a total of 3 objects of type Student. Also, the user have to input the data. My problem is that I can get...

  • Java Help Please: I'm doing something wrong with my while loop. I want my code to...

    Java Help Please: I'm doing something wrong with my while loop. I want my code to check the phone number that is input to make sure it matches. If it doesn't I want it to print out Error! and I want my While loop to ask the user to try again by inputting another phone number. When the user puts in a phone number that matches I want the program to continue to output my tokens. When I run this...

  • Debug following methods(longestRun(finds how many times a character got repeated), findLastP(finds last p in a string),...

    Debug following methods(longestRun(finds how many times a character got repeated), findLastP(finds last p in a string), findFirstP(finds first p in a string)) in java language. public class simpleLoops { /** * @param args */ public static void main(String[] args) {    System.out.println(longestRun("aabbbccd"));    System.out.println("Expected 3");    System.out.println(longestRun("aaa"));    System.out.println("Expected 3");    System.out.println(longestRun("aabbbb"));    System.out.println("Expected 4");          int count = countP("Mississippi"); System.out.println(count);    int result = findLastP("Mississippi"); System.out.println(result); result = findFirstP("stop"); System.out.println(result); result = findFirstP("xxxyyyzzz"); System.out.println(result); } /** *...

  • Could you help me pleas , this is my code I want change it to insert...

    Could you help me pleas , this is my code I want change it to insert student by user , and i have problem when i want append name it just one time i can't append or present more one. >>>>>>>>>>>>>>>>>>>. import java.util.Iterator; import java.util.Scanner; public class studentDLLTest { static int getChoice() { Scanner in = new Scanner(System.in); int choice; do { System.out.print("\nYour choice? : "); choice = in.nextInt(); } while (choice < 1 || choice > 9); return choice;...

  • This is my current output for my program. I am trying to get the output to...

    This is my current output for my program. I am trying to get the output to look like This is my program Student.java import java.awt.GridLayout; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import javax.swing.JFileChooser; public class Student extends javax.swing.JFrame {     BufferedWriter outWriter;     StudentA s[];     public Student() {         StudentGUI();            }     private void StudentGUI() {         jScrollPane3 = new javax.swing.JScrollPane();         inputFileChooser = new javax.swing.JButton();         outputFileChooser = new javax.swing.JButton();         sortFirtsName = new...

  • Can someone help me with my code.. I cant get an output. It says I do...

    Can someone help me with my code.. I cant get an output. It says I do not have a main method. HELP PLEASE. The instruction are in bold on the bottom of the code. package SteppingStones; //Denisse.Carbo import java.util.Scanner; import java.util.ArrayList; import java.util.List; public class SteppingStone5_Recipe { private String recipeName; private int servings; private List<String> recipeIngredients; private double totalRecipeCalories; public String getRecipeName() { return recipeName; } public void setRecipeName (string recipeName){ this.recipeName = recipeName; } public int getServings() { return...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT