I want to make the Output like this: 9-562-32458-4, 0-321-57351-X
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter 9 digit number: ");
String num = input.next();
int sum = 0;
for (int i = 1; i <= num.length(); ++i) {
sum += (i * num.charAt(i - 1) - '0');
}
int d10 = (sum % 11);
if (d10 == 0) {
System.out.println("Formatted ISBN: "+ num + "X");
} else {
System.out.println("Formatted ISBN: "+ num + d10);
}
}
}
I tried with printf but couldnt make it.
Using
System.out.printf()andString.substring():Or, to avoid repeated code:
*You should add a check to ensure that
num.length() == 9.