scanner problems

how can i make this work right. //this doesnt workCDinfo = in.nextLine() //but this doesCDinfo = in.next()i need to be able to include spaces and more than one word. thanks
[277 byte] By [gtrdude18a] at [2007-10-2 16:25:05]
# 1
Define "doesn't work". And please include a little more code so that we can see the context.
MLRona at 2007-7-13 17:23:40 > top of Java-index,Java Essentials,New To Java...
# 2

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

gtrdude18a at 2007-7-13 17:23:40 > top of Java-index,Java Essentials,New To Java...
# 3

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".

MLRona at 2007-7-13 17:23:40 > top of Java-index,Java Essentials,New To Java...
# 4
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
gtrdude18a at 2007-7-13 17:23:40 > top of Java-index,Java Essentials,New To Java...
# 5

> 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.

MLRona at 2007-7-13 17:23:40 > top of Java-index,Java Essentials,New To Java...
# 6
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?
gtrdude18a at 2007-7-13 17:23:40 > top of Java-index,Java Essentials,New To Java...
# 7

> 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.

MLRona at 2007-7-13 17:23:40 > top of Java-index,Java Essentials,New To Java...
# 8
ok ill give it a try here soon, thanks for the help
gtrdude18a at 2007-7-13 17:23:40 > top of Java-index,Java Essentials,New To Java...