• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

processing with arrays

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Alison Denise
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how do you store the data in an array without knowing the length of an array, though?
 
Rameshwar Soni
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you so much!!
 
Rameshwar Soni
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're Welcome
 
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic