• Post Reply Bookmark Topic Watch Topic
  • New Topic

Array trouble  RSS feed

 
Daniel Castellano
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, i have an assignment in which the user is asked for the number of city he would like to access, and when he does, he inserts the number of people living in it. The thing is i'm supposed to save the number of people living in each city in an array and then print all of them.

I have this so far,



Alright, i'm using case to access the "city" the user wants, it is probably poorly used and the "if" inside of it is redundant.

The other problem i'm having is that i have no idea how to insert the value of people living in certain city inside of certain index. For example i want to store the people living in city 1 inside the index 0 of the array, and so on. Any help would be appreciated. I'm trying to learn as much as i can and i'm researching on how to do it but i would like some help as well.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you combining a switch and an if?

I think you need to think again. Are you supposed to enter 7 cities with populations? Do you have a City class? With name and population as fields?
Why are you using option panes rather than a Scanner?
 
Daniel Castellano
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Why are you combining a switch and an if?

I think you need to think again. Are you supposed to enter 7 cities with populations? Do you have a City class? With name and population as fields?
Why are you using option panes rather than a Scanner?


You're right. I realized that they are redundant the moment i finished to post.

I am supposed to ask the user which city he wants the population to be added and then print the totals of each city. I do not have a City class. And because we are supposed to use OptionPane for this one.


EDIT:



Using this code in Case 1. It is working so far, i can obtain the number of people living in City 1 and the print the total value of the people living in there. However i can add a maxium of 8 values(because my array is of 8 thinking of the 7 cities).

But the thing is that i want to add the values of people living in City 1 in just the index 0 of my array. I'm not sure if that possible without using an array of array. Or should i use an array for each city?

To clarify: city.getInfo comes from City class, that method is used to obtain and store the value of the city population.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what is in the City class? The City class is probably the most important thing to get right. Once you have done that you can simply create a City[] array.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!