-2

I'm new here and very new to programming. I am actually taking a Intro to Programming in Java course right now and my teacher isn't super helpful so I would just like to have other people help me out with this. Anything will be appreciated. My program is designed to generate ISBN-10 numbers based on what the user enters into the console. the user enters 9 numbers in the console. What I am trying to have the program do is measure the length of the characters entered and if it does not equal nine then it will say "You must enter 9 numbers". Here's what I have:

    Scanner input = new Scanner(System.in);
    System.out.println("Enter 'C' for console generation of an ISBN, or 'R' for a random generation of an ISBN:");
    String letter = input.next().toLowerCase();

    if (letter==c) {
        System.out.println("Enter the first 9 digits of an 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();
        int numbers = (d1 + d2 + d3 + d4 + d5 + d6 + d7 + d8 + d9);

        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("Console generated ISBN-10 is " + d1 + d2 + d3 + d4 + d5 + d6 + d7 + d8 + d9 + "X");
        }
        else if (numbers.length() != 9) {

        System.out.println("You need to enter exactly 9 digits");
        System.exit(2);
        }

Any help is appreciated thank you!!!

MKBurns
  • 11
  • 1
  • And what is it doing differently from what you expect? – Nathan Tuggy Mar 01 '15 at 03:38
  • well it isn't actually working. Where it says "else if (numbers.length() !=9)" It says that it is wrong. There is a red line under the whole line and it won't work because the length() method only works is there is a string in there not an int. I just don't know how to fix this problem. – MKBurns Mar 01 '15 at 14:34
  • By the time your code reaches `int numbers = (d1 + d2 + d3 + d4 + d5 + d6 + d7 + d8 + d9);`, the user already entered exactly 9 numbers. That is why you can remove the `numbers` variable as well as your `if` part. – Clashsoft Mar 01 '15 at 14:44
  • Also, it should be `if (letter.equals("c"))`. – Clashsoft Mar 01 '15 at 14:45
  • but don't i need the if statement if the amount of numbers entered is not equal to 9? – MKBurns Mar 01 '15 at 16:00

1 Answers1

0

Change your code to:

String numbers =  asString(d1) + asString(d2) + asString(d3) ......  + asString(d9);

Also add this function:

private String asString(int num){
    return "" + num;
}

Either you can use this code:

String d1 = input.next();

Which will get input from the user as a String not as an int

roeygol
  • 4,908
  • 9
  • 51
  • 88