by doesnt work i mean the in.nextLine will not actually store information into the variable
import java.util.Scanner;
public class MusicShop {
CompactDisk[] collection;
int size;
String CDInfo = "";
Double CDCost =0;
Scanner in = new Scanner(System.in);
public MusicShop(int maxCDs) {
collection = new CompactDisk[maxCDs];
size = 0;
}
public void CDInformtaion() {
int choice = 0;
int i;
for (i = 0; i != -1; i++) {
System.out.println("Please choose option:");
System.out.println("1. Enter CD name");
System.out.println("2. Enter CD cost");
System.out.println("3. Exit");
choice = in.nextInt();
if (choice == 1) {
System.out.print("Please enter CD name:");
CDInfo = in.nextLine();
}
else
if (choice == 2) {
System.out.println("Please enter CD cost:");
CDCost = in.nextDouble();
collection[i] = new CompactDisk(CDInfo, CDCost);
size ++;
}
else
if(choice == 3) {
i = -2;
}
}
}
public void display() {
for (int i = 0; i < size; i++) {
String theInformation = collection[i].toString();
System.out.println(theInformation);
}
}
public double lowerPrice(int highPrice) {
double currentPrice;
int counter = 0;
for (int i = 0; i < size; i++) {
currentPrice = collection[i].getPrice();
if (currentPrice < highPrice) {
counter++;
}
}
return counter;
}
}
the display function cant work right because it says that CDInfo is still null for some reason
nextInt() doesn't pull the new line after you enter the choice.
After:
choice = in.nextInt();
Do this (just discard the newline):
in.nextLine();
Then try again.
for (i = 0; i != -1; i++) {
should probably say i != -2, since that what your choice 3 sets 'i' to.
The 'for' loop doesn't make much sense, anyway. You should set "collection[size]", not "collection".
> well i have to set i like this i = -2
> because the update adds one to i before it tests for
> thw value of it again so if i set at - 1 by teh time
> the loop was ready again it would be 0. size is
> supposed to count how many new cds i enter, while i
> is where i want to put it in the array.
Yes, I guess you are right about adding 1 to i in the loop, so you need -2. But, as I said, that loop doesn't make sense. Your loop should likely be
while (choice != 3)
Then, as I said, use 'size' to put it in the collection array. Try it and see what I mean. As you have it, i will jump twice for every CD. Also, having to choose "Enter name" and then choose "Enter cost" doesn't make much sense. You should have:
1. Enter CD details.
2. Display CD details.// for example
For 1, you'd ask for name and cost, then add the CD to the collection.
For 2, you'd call the display method.
Your CDInfo and CDCost should be local to CDInformation (which you spelled wrong, by the way). They aren't needed as instance variables. And, CDCost should probably be a primitive double, not a Double object.
> ok i see what u mean about size but how would i be
> able to tell when the name ends and the cost begins
> if i ask for it at the same time?
Ask for it in two steps, as you do now. Just put both steps in the same choice. Choice 1 would say:
// print prompt for Info
// read info using in.nextLine();
// print prompt for cost.
// read cost using in.nextDouble();
// call in.nextLine() to discard the unneeded newline.
// create a CD with the info and cost you just read, and put the CD in the collection[size]
// Go back to prompt for choice.