Alison Denise wrote:I'm in an intro class and we're supposed to write a program that asks the user to enter however many integers they like, and the program then returns how many of each number they entered. For instance, if the user enters 0, 7, 7, 2, 7, 10, the program returns that they entered 1 0, 3 7s, and 1 10. I have no idea where to start with this, but I know we're supposed to use arrays. Can anybody give me any help?
Alison Denise wrote:We literally just started with arrays yesterday. I figure I need to call a method that asks for input and stores in array, then call another one that goes through and finds all 1s, 2s, 3s, etc, then prints how many there are of each if there are more than 1? Except then I open JGrasp and I'm like "ehhhh just kidding."
We've gone over loops, variables, scanner objects, methods, decision structures...
Alison Denise wrote: System.out.println("Please enter a one-digit number, or 10 to quit input:");
Alison Denise wrote: I'm in an intro class and we're supposed to write a program that asks the user to enter however many integers they like
Alison Denise wrote:Here's the problem:
Write a program that asks the user to enter a series of one-digit numbers (you will need to do error checking). When they have finished (you will have to determine how the user indicates they are finished), print out how many of each number the user entered. There must be two methods. Name your program Lab10_ex2.java.
So what you said makes total sense except that the entry is to be sentinel-controlled. Also multiple methods.
Rameshwar Soni wrote:Yes i am sorry i forgot that an array cannot grow dynamically.
So the answer is you cannot use array you will have to use ArrayList class which is present in the java.util package.
So you can create an ArrayList in this manner
And then finally add the numbers in the "myArray" using the add() method
Example:
In your code in place of the numbers 5 and 6 the variable name will come which will have value inputed from user.
For more information on ArrayList you can google it.
Joanne
I don’t like that solution because the original assignment said there was no restriction to how many entries there were to be. I also do not think using a List is going to help.Rameshwar Soni wrote: . . . You can use a for-loop as below
. . .
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |