Results 1 to 15 of 15
  1. #1
    Jason is offline Member
    Join Date
    Sep 2011
    Posts
    23
    Rep Power
    0

    Default Display occurrence of number based off user input… array

    I am trying to have a user enter in any amount of numbers they like then based upon their input use an array to count each occurrence of the numbers entered by the user. , basically i am kind of stumped, yes this is homework and i do want to learn so i do not expect the full answer, just some input , thanks. Also i did post this in another forum but am still a bit confused, than you anyone for some input. I just do not know my next step in the code? Also the user can only enter in values between 1 and 100
    Java Code:
    package chapter_6;
    
    import java.util.Scanner;
    
    /**
     *
     * @author jason
     */
    public class Six_Three {
    
      
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            int[] num = new int [0];
           
            // prompt user for numbers
            System.out.println("Enter integers between 1 and 100: ");
            for (int data = input.nextInt();data != 0;data++){
                
            }










    }

    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,581
    Rep Power
    25

    Default Re: Display occurrence of number based off user input… array

    What classes and java language constructs do you know and are allowed to use for this assignment?
    You will need a place to store the numbers that are read in. You could use a very large array to hold the numbers if you were sure that the user would not enter more numbers than the size of the array. Or you could use an ArrayList to hold the numbers which wouldn't have that problem.
    Then you will need a way to count the occurrences of each unique number. Don't know what you know so I could recommend a solution.

    So for now work on getting all the numbers from the user. Then we'll work on how to count them.

    Or you could count the numbers as you read them in. That could be the better solution if that is all that is required. Look at using an array to hold the counts.

  3. #3
    Jason is offline Member
    Join Date
    Sep 2011
    Posts
    23
    Rep Power
    0

    Default Re: Display occurrence of number based off user input… array

    Thanks for the input, pretty much i do not know much in the grand scheme of things but will learn! The numbers the user enters could be any amount as long as each individual number does not exceed 100. That is where my issue is, i know how to create a simple array and assign its indexes etc BUT here i think my array needs to be built off of random user input, that's where i am getting stumped, the word random! Hope i am even making sense!

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,581
    Rep Power
    25

    Default Re: Display occurrence of number based off user input… array

    Look at the problem another way. Say you had 100 letter boxes with labels. There is a user handing You letters with a label, you put it the box with the matching label. The letters will always have one of the 100 labels on it. Then you get another letter and another and so on until the user get tired of giving you letters. Now count the number of letters in each box.
    Hint: The letter boxes could be represented by an array.

  5. #5
    Jason is offline Member
    Join Date
    Sep 2011
    Posts
    23
    Rep Power
    0

    Default Re: Display occurrence of number based off user input… array

    Thanks, that puts things in better perspective, i almost have it, i am only reading the first number of user input, same with my counter for occurrence , so i think i need a loop to read input as well as a loop to read occurrence

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,581
    Rep Power
    25

    Default Re: Display occurrence of number based off user input… array

    i need a loop to read input as well as a loop to read occurrence
    Yes, you will need to use loops.

  7. #7
    Jason is offline Member
    Join Date
    Sep 2011
    Posts
    23
    Rep Power
    0

    Default Re: Display occurrence of number based off user input… array

    I keep getting stuck with this problem, i am a little confused as how to initialize a count according to my array, here is what i have
    Java Code:
    package chapter_6;
    
    import java.util.Scanner;
    
    /**
     *
     * @author jason
     */
    public class Six_Three {
    
      
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
             int[] num = new int[100];
             int count = 0;
             
             //prompt user for input
            System.out.println("Enter integers between 1 and 100: ");
            int data = input.nextInt();
            
            
            for(int i = 0; i < num.length;i++){
                ++data;
                
    
            for(int j = 0;j < num.length;j++){
                count++;}
            
                data = input.nextInt();
                
                System.out.println("" + data + " occurs " + count + " times");
            
        }
                
                
            
           
            
           
    
        }
    }
    Last edited by Norm; 09-10-2011 at 07:16 PM. Reason: added code tags

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,581
    Rep Power
    25

    Default Re: Display occurrence of number based off user input… array

    Where are you "putting the letters into their mailbox slot" from my earlier example.
    When you get a number from the user, you need to count how many of those numbers you have gotten so far.
    The number is the same as the "label on the letter" in my earlier example.

    What are the steps your program is supposed to take to solve the problem. List them one small step at a time.

    Your current program does this:
    ask user for a number
    begin outer loop
    add one to data
    begin inner loop
    add one to count
    end inner loop
    // Count has been incremented num's length times here ???
    get number from user into data (this replaces the add done above)
    print out a message
    end outer loop

    This has no logic for what your program is supposed to do.
    I'd erase all of this and start over.

  9. #9
    Jason is offline Member
    Join Date
    Sep 2011
    Posts
    23
    Rep Power
    0

    Default Re: Display occurrence of number based off user input… array

    With some help and a lot of thought and explanation i finally got it, thank you again for your help, patience and most of all not giving me the answer but pushing me in the right direction to help me learn, i appreciate it, here is what i ended up with
    Java Code:
    package chapter_6;
    
    import java.util.Scanner;
    
    /**
     *
     * @author jason
     */
    public class Six_Three {
    
      
        public static void main(String[] args) {
           
            Scanner input = new Scanner (System.in);
            System.out.print("enter integers between 1 and 100: ");
            int[] num = new int[100];
            int data = 0;
           
            
            
             while ((data = input.nextInt()) != 0) {
                num[data]++;
            }
    
            for (int i = 1; i < 100; ++i) { 
                if (num[i] > 0)
                   if(num[i] < 2)
                    System.out.println(i + " occurs " + num[i] + " time ");
                else
                    System.out.println(i + " occurs " + num[i] + " times ");
            }
            
            } 
        }
    Last edited by Norm; 09-12-2011 at 03:19 AM. Reason: added code tags

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,581
    Rep Power
    25

    Default Re: Display occurrence of number based off user input… array

    One test you could add is for the user's input to be out of range and to give him an error message if it is and ask him to try again.

  11. #11
    Jason is offline Member
    Join Date
    Sep 2011
    Posts
    23
    Rep Power
    0

    Default Re: Display occurrence of number based off user input… array

    hmmm good thinking, i will give it a shot , i believe that would be implemented in my for loop, thats a good idea, round out all the input variables
    Last edited by Jason; 09-12-2011 at 03:29 AM.

  12. #12
    Jason is offline Member
    Join Date
    Sep 2011
    Posts
    23
    Rep Power
    0

    Default Re: Display occurrence of number based off user input… array

    interesting, when i do that i get a arrayoutofbounds error at runtime, would that be because i set my array to [100] and my if statement in my for loop to be out of range is above 100?

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,581
    Rep Power
    25

    Default Re: Display occurrence of number based off user input… array

    i get a arrayoutofbounds error
    What is the value of the index that is out of bounds?
    an array with 100 elements can use values of 0-99

  14. #14
    Jason is offline Member
    Join Date
    Sep 2011
    Posts
    23
    Rep Power
    0

    Default Re: Display occurrence of number based off user input… array

    Well I fixed it to be from 1 -100 , so if I try an out of range "if" based of user input I get that runtime error, any number above 100 would be out of range and render the error. At least that is how the program is behaving so I hope my theory holds true .

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,581
    Rep Power
    25

    Default Re: Display occurrence of number based off user input… array

    Your code should not allow the index for the array to be out of range. Test it and reject it.

Similar Threads

  1. Displaying a square based on user's input
    By luke in forum New To Java
    Replies: 15
    Last Post: 07-14-2012, 09:43 AM
  2. How do I retrieve data based on user input?
    By tunali2 in forum New To Java
    Replies: 1
    Last Post: 04-03-2011, 02:40 PM
  3. Creating a number of objects based on user input.
    By Kevinius in forum New To Java
    Replies: 21
    Last Post: 04-03-2011, 08:53 AM
  4. Dynamic Create Object based on user's input?
    By diskhub in forum New To Java
    Replies: 2
    Last Post: 03-08-2011, 04:47 PM
  5. Replies: 2
    Last Post: 01-13-2011, 05:43 AM

Posting Permissions

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