Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Program does not work

1. ## Program does not work

Hi, newbie to the forum
I am a beginner in java eclipse and having a problem with the program i had to write. The program ask the user to enter 9 digit then it is to produce the 9 numbers plus the 10th number. That does not happen. Any help would be appreciated thanks.

John
P/S please let me know if i posted correctly.

[highlight = java]

import java.util.Scanner;
public class Exercise3_9 {

public static void main(String[] args) {
Scanner input = new Scanner (System.in);

while (true) {
//Prompt the user to enter first 9 ISBN digits
System.out.print("Enter the first 9 digits of the ISBN: ");
int d1 = input.nextInt();
int d2 = input.nextInt();
int d3 = input.nextInt();
int d4 = input.nextInt();
int d5 = input.nextInt();
int d6 = input.nextInt();
int d7 = input.nextInt();
int d8 = input.nextInt();
int d9 = input.nextInt();

// Calculate
int d10 = ((d1 * 1) + (d2 * 2) + (d3 * 3) + (d4 * 4) + (d5 * 5) + (d6
* 6) + (d7 * 7) + (d8 * 8) + (d9 * 9))% 11;

if (d10 ==10)
System.out.println("The ISBN-10 number is " + "x");
else
System.out.println("The ISBN-10 number is " + "");
System.out.println();
}
}
}

[/highlight]

2. ## Re: Program does not work

What happens instead? What did you find out when you stepped through this with a debugger, or at least added some print statements, to figure out when the program's execution differs from your expectation?

3. ## Re: Program does not work

Originally Posted by KevinWorkman
What happens instead? What did you find out when you stepped through this with a debugger, or at least added some print statements, to figure out when the program's execution differs from your expectation?

Kevin,
This is what i get.

Enter the first 9 digits of the ISBN: 013601267

What needs to happen is this:
Enter the first nine digits of an ISBN as integer: 013601267
The ISBN-10 number is 0136012671

John

4. ## Re: Program does not work

Put a print statement after the first "input.nextInt();" and tell us when it gets executed. I suspect the nextInt() method doesn't trigger until an "end-case" for the input is reached. By that, I mean: how does the Scanner know your first integer is supposed to be "0" instead of "013601267"? If it is taking "013601267" as your first int (which I suspect it is), your program will hang on the second nextInt(), since it is waiting another int to be inputted.

5. ## Re: Program does not work

I am sorry but i am really new at this programming stuff, so i don't quite understand the print statement.

6. ## Re: Program does not work

KW's point is that you have 9 calls to input.nextInt(), and the first call is taking the entire user input of "013601267", and is waiting for the user to input a second digit, because that's how the nextInt() method works. Try giving the input statements one character (or digit) at a time separated by white space, as in: 0<space>1<space>3<space>6, etc. . .