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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Trouble with detecting digits

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    15
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Trouble with detecting digits

    Here is the question assigned to me in class...

    Suppose you want to develop a program to play lottery. The program randomly
    generates a lottery of a two-digit number, prompts the user to enter a two-digit number,
    and determines whether the user wins according to the following rule:
    a. If the user input matches the lottery in exact order, the award is $10,000. 2/2

    b. If all the digits in the user input match all the digits in the lottery, the award is
    $3,000.
    c. If one digit in the user input matches a digit in the lottery, the award is $800.

    Note that, in the program, make sure that the lottery number generated must be two
    digits.
    __________________________________________________ _______________________
    So far I have created the random generator and scanner to get the user input. The problem I am having though is for example if i get a number 1-9, how can i make that into 2 digits for example 09 or 07 etc. I'm sure I have to use decimalformat but that doesnt seem to work. Also, I am having a problem detecting each individual digit. I know I have to use the number%10 but then how do i get the first digit. Any help would be greatly appreciated.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Trouble with detecting digits

    This might be easier if you deal with the numbers as Strings and chars instead of ints. For single digit numbers simply concat a '0' to the front.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    15
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Trouble with detecting digits

    I imported the java.util.Random and then I created a random object called myGenerator. When I try to declare my random number as a String I get an error:
    Error: cannot find symbol
    symbol: method nextLine(int)
    location: variable myGenerator of type java.util.Random
    my code looks like this:
    String lottery = myGenerator.nextLine(99) + 1;

    What am I doing wrong?

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Trouble with detecting digits

    As the error message says: the Random class does not have a nextLine method. You can only call methods that exist not ones you think exist. Even if it was to work the Random class generates numbers. You would have to convert it to a String before assigning it to a String variable. String class and the Integer class both have a method to do this.
    Improving the world one idiot at a time!

Similar Threads

  1. Efficient Object Detecting?
    By Gravity Games in forum Object Oriented Programming
    Replies: 6
    Last Post: October 4th, 2012, 11:01 AM
  2. help with detecting certain characters
    By goochasauras in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 27th, 2012, 03:30 PM
  3. [SOLVED] detecting pc type
    By usama8800 in forum Java Theory & Questions
    Replies: 1
    Last Post: June 26th, 2012, 02:17 PM
  4. Collision Detecting
    By iams3b in forum AWT / Java Swing
    Replies: 0
    Last Post: February 6th, 2011, 01:48 AM