Reputation: 1
I am supposed to write a program that makes entries in a gift registry. User can enter as many gift item desired and the store where this can be purchased. Once the user express desire to stop entering a new item, a summary of all the gift item & stores will be displayed.
Below is a sample output
Do you wish to make a gift registry list? (y/n): y
Enter item: watch
Enter store: Swatch
Any more items? (y/n): y
Enter item: ballpen
Enter store: National Bookstore
Any more items? (y/n): n
Gift Registry:
watch - Swatch
ballpen - National Boo
If I'm not mistaken, I am supposed to make use of arrays for this program right? Is it possible to have a length of an array dependent on the counter(the number of times the user inputs)?
So far these are my codes:
package arrays;
import java.util.*;
import java.util.List;
import java.util.ArrayList;
public class GiftRegistry
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
String choice;
// Declare array num
ArrayList<String> items = new ArrayList<String>();
ArrayList<String> stores = new ArrayList<String>();
items.add(items);
stores.add(stores);
System.out.print("Do you wish to make a gift registry list? (y/n):");
choice = input.nextLine();
while (choice.charAt(0) != 'n')
{
System.out.print("Enter item: ");
items.add(items) = input.nextInt();
System.out.print("Enter store: ");
stores.add(stores) = input.nextInt();
System.out.print("Any more items? (y/n):");
choice = input.nextLine();
}
System.out.println("Gift regisrty: ");
}
}
I really don't know how
Upvotes: 0
Views: 190
Reputation: 1105
You are correct in the idea that you should use arrays - However, Java's standard array type does not dynamically resize - In other words, here:
int[] item = new int[ctr+1];
int[] store = new int[ctr+1];
You are creating a reference to an array object of size 1. But when you call
ctr++;
You are not affecting the size of the array - you are affecting value that is associated with the integer ctr, which, when you use to initialize the new array, does not automatically associate it with the array.
If you wanted to still use primitive array, you'd have to make a new one when the desired number of gifts is > that the size of the array - by the way, you'd have to store the items as strings:
//If the array 'item' is full....
String [] oldItemArray = item;
//You can increase by 1 or more, to add more empty slots
String [] newItemArray = new String[oldItemArray.length + 1];
for (int i = 0; i < newItemArray.length; i++){
//...Put each item from the old array into the new one.
}
item = newItemArray;
Java includes a type of data structure for these situations, since it's so common - ArrayList - which dynamically resize when the amount of memory allocated is fillled and I would highly recommend using it:
ArrayList<String> items = new ArrayList<String>();
ArrayList<String> stores = new ArrayList<String>();
...
...
items.add(enteredItem);
stores.add(storedItem);
There are several other small problems with the code, if you are also asking about them as well. However this is the primary problem with the code.
Upvotes: 0
Reputation: 50203
1) You cannot insert "watch" and "Swatch" as int
;
2) Why using Arrays
, when a List
is better ?
EDIT:
java.util.List
: the Interface.
java.util.ArrayList
: the best implementation for your case.
Usage:
List<String> list = new ArrayList<String>();
list.add("myFirstString");
list.add("mySecondString");
etc.
For reading it on a for each loop:
for (String currentValue : list)
System.out.println(currentValue);
Upvotes: 1
Reputation: 794
You can use ArrayList, size of which is dynamically and automatically adjusted.
Upvotes: 0
Reputation: 3416
Arrays are fixed in length, therefore you can't use arrays, due to the nature of your infinite length. You should be using a List, and in your case an ArrayList.
http://docs.oracle.com/javase/6/docs/api/java/util/List.html documentation for list http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html documentation for arraylist
Upvotes: 0