Results 1 to 4 of 4
  1. #1
    Findil is offline Member
    Join Date
    Feb 2017
    Posts
    2
    Rep Power
    0

    Default Matching User inputted numbers to randomly generated digits

    Hi,

    I have a program which asks the user to enter a Month and a Day into the console, the program then takes those inputs and matches them to randomly generated months and days, which it generates 100 times. It then adds the total amount of times both of the values match into a final count integer. The month value can be successfully compared and matched with a test input of 12, 12. However the day comparison is not working properly and brings up matches for days that have not been inputed by the user and are not consistent. (The code for this is around line 67) Was wondering if anyone could spot the error as i cannot seem to detect what is wrong

    Java Code:
    import java.util.Random;
    import java.util.Scanner;
    
    public class RandomGenerator {
        
        public static int getDayInMonth( int month ){
            
        final int JANUARY = 1; final int JULY = 7;
        final int FEBRUARY = 2; final int AUGUST = 8;
        final int MARCH = 3; final int SEPTEMBER = 9;
        final int APRIL = 4; final int OCTOBER = 10;
        final int MAY = 5; final int NOVEMBER = 11;
        final int JUNE = 6; final int DECEMBER = 12;
    
        Random dayGenerator = new Random();
    switch (month) {
        case JANUARY: case MARCH: case MAY: case JULY:
        case AUGUST: case OCTOBER: case DECEMBER:
        return dayGenerator.nextInt(31) + 1;
    
        case APRIL: case JUNE: case SEPTEMBER: case NOVEMBER:
        return dayGenerator.nextInt(30) + 1;
    
        case FEBRUARY:
        return dayGenerator.nextInt(28) + 1;
    
        default:
        return -1;
    }
    }
        
        public static void main (String args[]){
            boolean check1 = false;
            boolean check2 = false;
            int targetMonth = 0;
            int targetDay = 0;
            int numberOfMatches = 0;
            
        do{ System.out.println("Please enter a Target Month");
            Scanner month = new Scanner(System.in);
            targetMonth = month.nextInt();
        if(targetMonth <13 && targetMonth > 0)
            check1 = true;
        else
            System.out.println("Month is not valid");
        }
        while (!check1);
            
            do{ System.out.println("Please enter a Target Day");
            Scanner day = new Scanner(System.in);
            targetDay = day.nextInt();
        if(targetDay <31 && targetDay > 0)
            check2 = true;
        else
            System.out.println("Day is not valid");
        }
        while (!check2);
            
            Random rand = new Random();
    for (int index = 1; index <= 100; index++) {
            int  n = rand.nextInt(12) + 1;
            System.out.println("Run number is " + index);
            System.out.println("The Month is " + n);
            if(n == targetMonth) {
                System.out.println("MATCH");
            }
            System.out.println("The Day is " + getDayInMonth(n)); 
            if(targetDay == getDayInMonth(n)){ 
                System.out.println("MATCH");
            }
            if(n == targetMonth && getDayInMonth(n) == targetDay){
                numberOfMatches += 1;
            }
            System.out.println();
            
            
    }
        System.out.println("The number of date matches were " + numberOfMatches);
    
        }
        
        
    }
    Thanks!

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Matching User inputted numbers to randomly generated digits

    Here's a hint. Every time you call getDayInMonth(n) you get a different day since they are generated randomly.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Findil is offline Member
    Join Date
    Feb 2017
    Posts
    2
    Rep Power
    0

    Default Re: Matching User inputted numbers to randomly generated digits

    Hi,

    Thanks for replying so quickly, I would assume that even though the month is generated randomly each time, line 68 should still compare these two correctly as the user inputted targetDay stays the same.

    Thanks

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Matching User inputted numbers to randomly generated digits

    For lines 67 thru 74, print out the value of each call to getDayInMonth(n). And in general, sprinkle print statements throughout your program
    to facilitate debugging.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 6
    Last Post: 02-02-2017, 08:13 AM
  2. Matching numbers !
    By Ajinkya in forum New To Java
    Replies: 3
    Last Post: 07-21-2012, 11:57 AM
  3. imagej and creating randomly generated data
    By daggaz in forum New To Java
    Replies: 0
    Last Post: 05-29-2012, 11:20 PM
  4. Sorting printed ArrayList of user inputted strings.
    By movsesinator in forum New To Java
    Replies: 3
    Last Post: 04-03-2010, 10:27 PM
  5. restrictions on randomly generated values?
    By busdude in forum New To Java
    Replies: 6
    Last Post: 10-23-2008, 12:48 PM

Posting Permissions

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