File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes processing with arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "processing with arrays" Watch "processing with arrays" New topic
Author

processing with arrays

Alison Denise
Greenhorn

Joined: May 02, 2012
Posts: 6
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?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18495
    
  40

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?


One possible way to start with this is to make a list of what you have. What have you already learned? Input/Output? Loops? Variables? etc. I am assuming it is a "yes" for array variables.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Alison Denise
Greenhorn

Joined: May 02, 2012
Posts: 6
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...
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18495
    
  40

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...



Baby steps.... One small step at a time..... For example, do the "ask for input" first. Don't worry about parsing it, where to store it, or the print out yet.

Henry
Alison Denise
Greenhorn

Joined: May 02, 2012
Posts: 6
That's
Scanner keyboard = new Scanner(System.in);
int hold;

System.out.println("Please enter a one-digit number, or 10 to quit input:");
hold = keyboard.nextInt;

So I'm looking in my book about how to put the data into an array, but the examples I have are for arrays of a set length. I'm thinking if hold != 10 then send it into a for loop but it's that heading that confuses me because those often presume a set length.
Rameshwar Soni
Ranch Hand

Joined: Feb 03, 2011
Posts: 247
Firstly why are you asking this
Alison Denise wrote: System.out.println("Please enter a one-digit number, or 10 to quit input:");


When you want to achieve this
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


Instead ask

Now take the input. But the way you are taking input is incorrect. The correct one is


Since nextInt() is a method it will have parenthesis.

Now "hold" variable has the the number of integers that the user wants to enter. Say for example the user entered 7.

Now you create an array of type integer (you must be knowing how to do that) and use a for-loop to take the 7 numbers from user and store it in the array.
You can use a for-loop as below


As you can see this for-loop is going to iterate 7 times which means you can take 7 numbers as input from the user
Inside the for-loop you can take store numbers (i.e. take input from user) into the array
Then finally check how many 1s, 2s etc are entered.
Alison Denise
Greenhorn

Joined: May 02, 2012
Posts: 6
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
Ranch Hand

Joined: Feb 03, 2011
Posts: 247
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.


Ok the entry is sentinel-controlled then the way you are asking the input is correct i.e.

i.e. keep on taking the input from the user till it is a single digit and stop when the user enters a two digit number.

So what you can do is take a while loop which is always true. For example


Now inside the while-loop you should first check whether the user has entered single digit numbers or a two or three digit (or more) numbers.
You can do this using if-else statements inside the while loop.
To be more precise take the input number in an variable check it single-digit or more and if it is single digit store it in array else break the loop


Alison Denise
Greenhorn

Joined: May 02, 2012
Posts: 6
how do you store the data in an array without knowing the length of an array, though?
Rameshwar Soni
Ranch Hand

Joined: Feb 03, 2011
Posts: 247
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.
Alison Denise
Greenhorn

Joined: May 02, 2012
Posts: 6
thank you so much!!
Rameshwar Soni
Ranch Hand

Joined: Feb 03, 2011
Posts: 247
You're Welcome
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3415
    
  12
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.

Alison
If you've followed this advice then your program is wrong. Your array is supposed to hold a count of how many of each single digit number is entered, not the numbers themselves.
Therefore you can use an array. It will need to have a size of 10. If you initialise all the elements to zero, you can then use

At the end of the loop myarray[0] will be the number of zeroes entered, myarray[1] will be the number of ones entered, etc


Joanne
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
Rameshwar Soni wrote: . . . You can use a for-loop as below
. . .
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.

Alison Denise, welcome to the Ranch

The answer is actually much simpler than you think, but you are going to have to work it out on paper first. Once you have the logic of counting, you can work out how to get it into code. Forget all about coding for the time being, and also forget all the advice you have been given.

Write down the algorithm, pseudocode, plain simple English or whatever. Then we can see whether it is any good, and only then should you try to code anything.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
I hadn’t notice what Joanne Neal had posted. Sorry, Joannne.

When Joanne gives you advice, you make sure to follow it
Rameshwar Soni
Ranch Hand

Joined: Feb 03, 2011
Posts: 247
I am really sorry everyone and specially Alison for telling you to use ArrayList when the problem can be solved by Array as shown by Joanne.

I too do not know (or i might have not understood the problem logic) when we started thinking about STORING numbers in array without knowing the size (i.e. dynamic arrays), since this isn't possible therefore ArrayList came in my mind.

I am really sorry Alison
Hope you will forgive me.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: processing with arrays
 
Similar Threads
how to check null and empty values for a field ?
Palindrome
missing return statement?
Rock paper scissors project
input number to array --not displaying right item [code included]