• Post Reply Bookmark Topic Watch Topic
  • New Topic

Challenge with Basic Array  RSS feed

 
Stacy Beery
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a beginner and having a challenge with a basic Array. I am putting what I have learned to work by creating a very simple program to display a recipe in the print window. My Recipe class contains several instance variables with two for arrays. The arrays are meant to be for the size of the ingredient and the name of the ingredient.

Within my test file (so I could test the method within my recipe class file), I have managed to print what I wanted as well as my method being successful. The problem I am having is I want to add more than just one ingredient. So i picked to go with a basic array with the goal of using an arraylist in the future. I understand that I need to have the dot operator to access the instance variables in my recipe class file, but I also need to create a new Array for the ingredients. My question is how do I create a new Array when it is being referenced from within the class. Without the new array, it just gives me a "null" when it is run.

 
Zachary Griggs
Ranch Hand
Posts: 83
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sounds like an array is correct for this purpose.



This looks almost correct to me (but usually loop index variables will be called "i", "j", or "k")
Why are you looping to 5? How do you know that you will have exactly 5 ingredients? What if a recipe calls for 3 or for 7 ingredients?
In this case, we can directly use the length of the array as the upper bound for the loop. You can access the length of the array exactly how it sounds: ingredientSize.length



Why have you chosen to make this an inner class? The class around it doesn't do anything but make this more complicated.


So, here are you trying to initialize the arrays ingredient[] and ingredientSize[]? If so, you'll need to reference them by their name and create the new array, like this:

The reason why this is a String is because you declared it as a String here:
 
Stacy Beery
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To answer most of your questions, because it is a work in progress.

1. I have my recipe file and my test file as separate - not together - as I plan on building on. Its not actually an inner class. There just listed here together so I could better explain.
2. It looping 5 even though I haven't put five in yet because it is still a work in progress. I plan on getting to were it uses an ArrayList so then whatever recipe is put in then I can adjust it without having to always change the array. I haven't done this to begin with just because I am working on putting what I have learned into use before moving on.
3. my method worked just fine - the whole thing worked fine - when I only had one ingredient.

my problem came when I changed it from one ingredient to being able to add more by using the array. Initializing the array is were I am having a problem. I understand how to initialize, like in your example, but if i put your example:



then it doesn't work because ingredient isn't a local variable - its in the recipe class file. I have to use the dot operator to be able to access the variable.


This works great with one ingredient, but it doesn't work with an array because of the initializing. So how do I initialize the variable as an array when it isn't a local variable?

 
Zachary Griggs
Ranch Hand
Posts: 83
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you wanted to initialize an instance variable inside another class, you could do the same
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!