java


JAVA ISBN-10 Number: Find 10th digit [closed]


Question :
An ISBN-10 consists of 10 digits: d1,d2,d3,d4,d5,d6,d7,d8,d9,d10. The last digit, d10, is a checksum,which is calculated from the other nine digits using
the following formula:
(d1 * 1 + d2 * 2 + d3 * 3 + d4 * 4 + d5 * 5 + d6 * 6 + d7 * 7 + d8 * 8 + d9 * 9) % 11
If the checksum is 10, the last digit is denoted as X according to the ISBN-10
convention.
Write a program that prompts the user to enter the first 9 digits and displays the 10-digit ISBN (including leading zeros). Your program should read the input as an integer.
Here are sample runs:
Enter the first 9 digits of an ISBN as integer: 013601267
The ISBN-10 number is 0136012671
MY CODE:
import java.util.Scanner;
public class ISBN_Number {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int[] num = new int[9];
System.out.println("Enter the first 9 digits of the an ISBN as integer: ");
for (int i = 0; i < num.length; i++) {
for (int j = 1; j < 10; j++) {
num[i] = s.nextInt() * j;
}
}
int sum = 0;
for (int a = 0; a < 10; a++) {
sum += num[a];
}
int d10 = (sum % 11);
System.out.println(d10);
if (d10 == 10) {
System.out.println("The ISBN-10 number is " + num + "X");
} else {
System.out.println("The ISBN-10 number is" + num);
}
}
}
ISSUE:
I am new to learning java, hence I am having trouble trying to figure this question out. Can some tell me where I am going wrong because I am not getting the expected outcome. Thank you.
nextInt() consumes the entire token 013601267, not just a single digit, which was not your plan. A much easier approach could be to consume it as a single string and then iterate over the characters:
String num = s.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 == 10) {
System.out.println("The ISBN-10 number is " + num + "X");
} else {
System.out.println("The ISBN-10 number is " + num + d10);
}
Because
for (int i = 0; i < num.length; i++) {
for (int j = 1; j < 10; j++) {
num[i] = s.nextInt() * j;
}
}
here your every input will be multiply 9 times
like when user enters 2
then 2 will be multiply like (2*1)(2*2)(2*3) shown on....so here at num[0]==
18(2*9)

Related Links

Setting ChoiceBox item too slow in javafx
Why doesn't addAll() support the addition of a collection's copy to the collection?
Java - BufferedReader Stalls without an Escape Character
how to payara micro port 80
Java - Reversing the way the output is shown
libgdx: Error java.lang.NullPointerException [duplicate]
Unable to retrieve the file data from parse.com in android
Java transfer from HDFS to S3
Continue after catching exception (Android/Java)
Recover the deleted project android studio [duplicate]
setting ImageAdapter for retrieveing image
Java reflection isAccessible method
java.util.observer with multiple JFrame
Adding Views with Java LayoutInflater in a loop either doesn't set the width or only adds the first item in the loop
Using a ListenableFuture Azure call inside an AsyncTask
What is the error in the following Queue implementation using linked list?

Categories

HOME
ibm-bluemix
compiler-construction
ionic-framework
jdo
proxy
reflection
tizen
tesseract
rsyslog
gis
rubygems
c#-2.0
webpack-2
mvvmcross
acquia
ios-charts
adfs
session-variables
windows-10-universal
gnupg
rascal
vb.net-2010
gz
windows-phone-7
footer
errorlevel
mustache.php
autosys
firebase-crash-reporting
vlsi
javacv
selectedindexchanged
bootstrap-duallistbox
traffic
bootstrap-material-design
maxmind
compatibility
atlassian-crucible
google-cloud-endpoints-v2
accelerate-framework
atl
mpmediaquery
git-merge
dartium
retina-display
gesture
service-discovery
code-search-engine
c11
dynamic-reports
android-mediaprojection
osx-mavericks
appcompat
apache-fop
dandelion
rdfs
togetherjs
pdfclown
synchronous
topbeat
google-cdn
android-fonts
heidisql
radtreelist
intrusion-detection
thrust
fuzzy-search
testng-dataprovider
bgp
dlna
design-by-contract
itextpdf
two-factor-authentication
energy
hsv
retina
django-unittest
citrus-pay
deis
arcanist
sysinternals
method-parameters
sankey-diagram
lustre
kineticjs
offloading
winddk
qcodo
html-helper
wp-query
hippomocks
marmalade
monomac
imdbpy
c18
geos
tws
back-stack
jquery-mobile-dialog
dmoz
sublist
twrequest
viewswitcher
enter
removeclass
hirefire
infobox
xmemcached
horizontal-accordion
recent-documents
pyinotify
wise
forums
database-management
iweb
data-driven
pascal-fc
document-conversion

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App