Results 1 to 15 of 15
- 09-10-2011, 03:00 AM #1
Member
- Join Date
- Sep 2011
- Posts
- 23
- Rep Power
- 0
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++){ }
}
}
- 09-10-2011, 03:11 AM #2
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.
- 09-10-2011, 03:20 AM #3
Member
- Join Date
- Sep 2011
- Posts
- 23
- Rep Power
- 0
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!
- 09-10-2011, 03:33 AM #4
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.
- 09-10-2011, 04:05 AM #5
Member
- Join Date
- Sep 2011
- Posts
- 23
- Rep Power
- 0
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
- 09-10-2011, 04:08 AM #6
Re: Display occurrence of number based off user input… array
Yes, you will need to use loops.i need a loop to read input as well as a loop to read occurrence
- 09-10-2011, 07:01 PM #7
Member
- Join Date
- Sep 2011
- Posts
- 23
- Rep Power
- 0
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
- 09-10-2011, 07:25 PM #8
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.
- 09-12-2011, 03:17 AM #9
Member
- Join Date
- Sep 2011
- Posts
- 23
- Rep Power
- 0
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
- 09-12-2011, 03:21 AM #10
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.
- 09-12-2011, 03:26 AM #11
Member
- Join Date
- Sep 2011
- Posts
- 23
- Rep Power
- 0
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.
- 09-12-2011, 03:34 AM #12
Member
- Join Date
- Sep 2011
- Posts
- 23
- Rep Power
- 0
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?
- 09-12-2011, 01:49 PM #13
Re: Display occurrence of number based off user input… array
What is the value of the index that is out of bounds?i get a arrayoutofbounds error
an array with 100 elements can use values of 0-99
- 09-12-2011, 11:51 PM #14
Member
- Join Date
- Sep 2011
- Posts
- 23
- Rep Power
- 0
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 .
- 09-12-2011, 11:54 PM #15
Similar Threads
-
Displaying a square based on user's input
By luke in forum New To JavaReplies: 15Last Post: 07-14-2012, 09:43 AM -
How do I retrieve data based on user input?
By tunali2 in forum New To JavaReplies: 1Last Post: 04-03-2011, 02:40 PM -
Creating a number of objects based on user input.
By Kevinius in forum New To JavaReplies: 21Last Post: 04-03-2011, 08:53 AM -
Dynamic Create Object based on user's input?
By diskhub in forum New To JavaReplies: 2Last Post: 03-08-2011, 04:47 PM -
Making a loop and creating variables based off of user input
By seanfmglobal in forum New To JavaReplies: 2Last Post: 01-13-2011, 05:43 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks