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

Why is ImageIO.read() so slow compared to Toolkit.getDefaultToolkit().getImage()?
Write a program using static methods
Accessing the index of the current interation of loop variable while looping through a string array
Calling repaint() without loosing previous graphic
Tomcat 7 startup problems: Could not initialize class org.apache.tomcat.util.descriptor.DigesterFactory
Running code when activity closes
Tomcat in Intellij Idea Community Edition
Count the Frequency of X letter words?
Why is SAXException serializable?
#inject doesn't work when the bean is not created via hk2
Can't add a value at the end of a sparse array in android
Spring - How to stop and start a Webserice from web application?
Adding a BigDecimal to BigDecimal in Java [closed]
How to pass parameters to post method in Spring MVC controller while doing junit?
Integer.Parse(String) throws exception
I'm trying to make a Harry Potter spell game

Categories

HOME
cloud
pycharm
json.net
lodash
syntax
cplex
gis
graphql
fsm
retrofit
ojdbc
django-imagekit
azure-storage-tables
podio
pheatmap
basic
facebook-messenger-bot
gorm
tomcat6
user-input
volttron
handsontable
etl
collectd
commonmark
designer
finite-automata
invantive-sql
firefox-webextensions
textfield
autocad-plugin
zapier
object-detection
shopware
rst2pdf
typo3-6.2.x
saas
webkitspeechrecognition
c++-amp
gsoap
nouislider
lightswitch-2013
yadcf
.net-4.0
data-manipulation
uiswipegesturerecognizer
wallpaper
mapbox-gl
wpf-controls
bower-install
vxworks
dartium
libvpx
dynamics-crm-2013
occlusion
twitch
splice
streamreader
android-tabhost
hp-ux
eclipse-scout
ncalc
reportingservices-2005
nand2tetris
nxlog
bootstrap-dialog
thin
jquery-nestable
jxcore
snmptrapd
dtexec
windows-mobile-6.5
knpmenubundle
sqldf
migradoc
google-cdn
nxt
removechild
radtreelist
adxstudio-portals
pcf
mathematica-frontend
merge-conflict-resolution
multiple-regression
apachebench
design-by-contract
pyke
jfugue
hsv
unity-networking
wireshark-dissector
citrus-pay
unity3d-gui
skos
mdt
codeigniter-routing
mesa
tld
hippomocks
comexception
app42
xsockets.net
tws
eclipse-memory-analyzer
dataadapter
type-equivalence
anonymous-methods
google-email-migration
dbproviderfactories
appconkit
regsvr32
netbeans-6.9
blackberry-playbook
table-footer
netbeans-7.1
inotifycollectionchanged
compiler-specific
memory-size
mediarss

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