0

In this below program, I'm trying to check whether the number is ISBN or not. I'm giving input with spaces (eg: 0 3 0 6 4 0 6 1 5 2) because array only accepts it like this. I don't know how to give input without space to read. Can anyone help me how to read the number eg: 0306406152 and also it will read 10 numbers only like if(i==10) else it says it's not ISBN number to give output.

public class ISBN {
 int digits[];
 int dig = 11;
 int sum;
 int isbn1;
 public void CheckISBN() {
 for (int digit : digits) {
 // System.out.println(digit);
 if (dig >= 1) {
 dig--;
 digit = digit * dig;
 // System.out.println(dig);
 }
 sum = sum + digit;
 isbn1 = sum % 11;
 }
 if (isbn1 == 0) {
 System.out.println(isbn1);
 System.out.println("it's valid ISBN number");
 } else {
 System.out.println("sorry it's not valid ISBN");
 }
 }
 public static void main(String[] args) {
 ISBN aa = new ISBN();
 aa.digits = new int[10];
 Scanner scan = new Scanner(System.in);
 int i = 0;
 while (scan.hasNextInt()) {
 aa.digits[i] = scan.nextInt();
 i++;
 if (i == 10) // aa.CheckISBN();
 {
 break;
 }
 for (int j = 0; j < aa.digits.length; j++) {
 // System.out.print(aa.digits[j]);
 }
 //System.out.println();
 }
 aa.CheckISBN();
 }
}

SAMPLE OUTPUT: 0 3 0 6 4 0 6 1 5 2
it's valid ISBN number

AJNeufeld
8,7471 gold badge32 silver badges46 bronze badges
asked Aug 16, 2016 at 16:20
3
  • why don't you take input in string rather than using array ? Commented Aug 16, 2016 at 17:07
  • I don't understand the question. Your sample output and actual output are completely different. You don't even print the isbn in your output Commented Aug 16, 2016 at 17:23
  • Scanner depends on whitespace to delimit tokens, so hasNextXXX works properly. Without spaces, your ISBN is basically a valid integer. Using a Pattern with a regex would be better for pattern-matching. Commented Aug 16, 2016 at 17:24

2 Answers 2

0

When the number is given without spaces,

import java.io.*;
import java.util.*;
public class ISBN {
 int digits[];
 int dig = 11;
 int sum;
 int isbn1;
 public void CheckISBN() {
 if(this.digits.length != 10)
 {
 System.out.println("sorry it's not valid ISBN");
 return;
 }
 for (int digit : digits) {
 // System.out.println(digit);
 if (dig >= 1) {
 dig--;
 digit = digit * dig;
 // System.out.println(dig);
 }
 sum = sum + digit;
 isbn1 = sum % 11;
 }
 if (isbn1 == 0) {
 //System.out.println(isbn1);
 System.out.println("it's valid ISBN number");
 } else {
 System.out.println("sorry it's not valid ISBN");
 }
 }
 public static void main(String[] args) {
 ISBN aa = new ISBN();
 Scanner scan = new Scanner(System.in);
 String num = scan.next(); //take input as a string
 int[] digits = new int[num.length()]; 
 for(int i = 0; i<digits.length; i++)
 digits[i] = num.charAt(i) - '0';
 aa.digits = digits;
 aa.CheckISBN();
 }
}
answered Aug 17, 2016 at 12:36

3 Comments

You can accept the answer, if you think it answered your question
yes, and can you explain this stmt please digits[i] = num.charAt(i) - '0';
num is of type String. Strings are made of chars. We take the char at index i and convert it to an integer to put to digit array. How converstion occur is explained here.
0

Or scan it as int to get number format validation for free:

public class ISBN {
 public static void main(String[] args) {
 Scanner scan = new Scanner(System.in);
 if (scan.hasNextInt()) {
 checkISBN(scan.nextInt());
 }
 }
 public static void checkISBN(int isbn) {
 int sum = sum(digits(isbn));
 int isbn1 = sum % 11;
 if (isbn1 == 0) {
 System.out.println(isbn1);
 System.out.println("it's valid ISBN number");
 } else {
 System.out.println("sorry it's not valid ISBN");
 }
 }
 private static int sum(int[] digits) {
 return IntStream.rangeClosed(1, digits.length)
 .map(i -> i * digits[digits.length - i])
 .sum();
 }
 private static int[] digits(int isbn) {
 return Integer.toString(isbn)
 .chars()
 .map(c -> c - '0')
 .toArray();
 }
}

N.B.: It works for ISBN both with or without leading zeros.

answered Aug 17, 2016 at 13:59

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.