Results 1 to 9 of 9
  1. #1
    jrelvi23 is offline Member
    Join Date
    Sep 2009
    Posts
    16
    Rep Power
    0

    Question Im new and need help!

    You guys are basically going to be my best friends this semester!! Already, I appreciate your responses!!

    Im having trouble adding a couple numbers that the user inputs in a string. So for example, if the user inputs "12", what I want it to do, is manipulate it to so when it prints, it wil read 3, or 2 if I choose to multiply them. My question is, what method should I use to accomplish this? and remember I am newbie! so speaks slowly! :p

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    First, be careful with your requirements. If I enter "351", is that three numbers, two numbers or one number?

  3. #3
    jrelvi23 is offline Member
    Join Date
    Sep 2009
    Posts
    16
    Rep Power
    0

    Default

    lol... I almost hit "report" on you thinking it was "reply."

    Here's the exercise that I am trying to do:

    import java.util.Scanner;

    public class ISBNExercise {
    public static void main(String[] args) {

    //Create a Scanner
    Scanner input = new Scanner(System.in);


    System.out.print("Enter the first 9 numbers of an ISBN: ");
    int firstNumbers = input.nextInt();

    //int calcFirstNumbers = (d1 * 10 + d2 * 9 + d3 * 8 + d4 * 7 + d5 * 6 + d6 * 5 + d7 * 4 + d8 * 3 + d9 * 2);
    //System.out.println("Adding the first 9 numbers equals to: " + calcFirstNumbers);


    System.out.print("Your 10-digit ISBN Number is: " + firstNumbers);

    }
    }

    When the user inputs 9 numbers, I need each of those individuals numbers to represent d1,d2,d3,d4...

    so if the user inputs: "394834005", each number represents d1 = 3; d2 =9, etc.
    each number will then calculate into the formula that is set above. I know I need to set up a checksum and there's more to the formula... but just dissecting those numbers is what I am having trouble with.. ..I hope I didn't lose you!
    Last edited by jrelvi23; 09-30-2009 at 09:57 PM.

  4. #4
    Dieter's Avatar
    Dieter is offline Member
    Join Date
    Sep 2009
    Posts
    91
    Rep Power
    0

    Default

    did you learn about arrays?
    Programming today is a race between software engineers striving to build bigger and better idiot proof programs,and the Universe trying to produce bigger and better idiots...

  5. #5
    jrelvi23 is offline Member
    Join Date
    Sep 2009
    Posts
    16
    Rep Power
    0

    Default

    No... I do not think so. However the teacher is slowly draining my will to live so I may be wrong but I don't think so. I am basically teaching myself here. I am guessing there are other ways to do this without using arrays, though?

    BTW, you guys are really fast! :D
    Last edited by jrelvi23; 09-30-2009 at 10:02 PM.

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    If the string represents single digit numbers then use the toCharArray method in the String class to get the individual characters.

  7. #7
    oxano is offline Member
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Default

    problem here:

    System.out.print("Enter the first 9 numbers of an ISBN: ");
    int firstNumbers = input.nextInt();
    9 numbers is a way to big nummer to put in an integer.
    an integer's range is : −32,768 to +32,767
    anything bigger or smaller will result in an error (overflow or something?)

    Maybe the sc.nextInt() is smart enough to detect this and will only read the first 4-5 digits of the full 9 digits and return that so if you type in '9876543210' the sc.nextIn() will give '9876' . and leave '543210' in the buffer..... maybe. I would have to test this to be sure.

    Anyway, the point is that you can better use a String to read it in.
    String string = sc.next()

    The easiest way would be to indeed use arrays. The only possibility i currently see with not using arrays is using the substring method.
    Java Code:
    for(int i=0 ; i<9 ; i++){
       string.substring(i,i+1)
    }
    (hope i didn't make any indexoutofbounds errors here)

    PS: put code between code tags: [CODE] [/ CODE] (without the space in [/ CODE])

  8. #8
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Quote Originally Posted by oxano View Post
    problem here:


    9 numbers is a way to big nummer to put in an integer.
    an integer's range is : −32,768 to +32,767
    anything bigger or smaller will result in an error (overflow or something?)

    Maybe the sc.nextInt() is smart enough to detect this and will only read the first 4-5 digits of the full 9 digits and return that so if you type in '9876543210' the sc.nextIn() will give '9876' . and leave '543210' in the buffer..... maybe. I would have to test this to be sure.
    since when is 9 numbers too long for an int? You're thinking of a short.

    Quote from The Java Tutorials:
    # short: The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As with byte, the same guidelines apply: you can use a short to save memory in large arrays, in situations where the memory savings actually matters.

    # int: The int data type is a 32-bit signed two's complement integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive). For integral values, this data type is generally the default choice unless there is a reason (like the above) to choose something else. This data type will most likely be large enough for the numbers your program will use, but if you need a wider range of values, use long instead.
    @OP: Ase was mentioned above, use String.toCharArray() to get the individual characters, then use Integer.parseInt(c +""); (The + "" make it a string, which is what the parameter needs to be) to get the value.

    Hope this helps,
    SingingBoyo
    Last edited by Singing Boyo; 10-01-2009 at 04:11 AM. Reason: Typos
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  9. #9
    jrelvi23 is offline Member
    Join Date
    Sep 2009
    Posts
    16
    Rep Power
    0

    Default

    Yessssss!!! I got it to work! thanks a lot everyone! great site! :)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •