# Problem with loop?

• November 23rd, 2011, 07:18 PM
Rilstin81
Problem with loop?
I am trying to figure out why it prints "What kind of flower would you like?" twice after doing the initial "y". The rest of the program seems to run fine except for it printing that line out twice.

Code Java:

```import java.text.*; import java.util.Scanner;   public class FlowerCounter{ public static void main(String[] args){   DecimalFormat df = new DecimalFormat("0.00"); Scanner in = new Scanner(System.in);   //declare and create an array of ten rectangles //declare and create an int array of ten elements //int[] intArr = new int[10]; String[] flowerArray = {"petunia", "pansy", "rose", "violet", "carnation", "lily", "poppy", "daisy", "tulip", "hydrangea", "none"}; double[] flowerPrice = {.50, .75, 1.50, .75, .85, 1.25, 2.15, .85, 2.75, 1.30, 0.00}; char Answer = 'y'; String flower; int i = 0; int t = 0; double cost = 0.0; int stems = 0; double stemcost = 99.0;     System.out.print("Do you want to purchase flower (Y or N)?"); Answer = in.next().charAt(0); System.out.println();   while(Answer == 'y'){ System.out.print("What kind of flower would you like?"); flower = in.nextLine(); System.out.println(); System.out.println(); stemcost = 99.0; for(i =0; i<=10; i++){ if (flower.equalsIgnoreCase(flowerArray[i])){ System.out.print("How many flowers would you like?"); stems = in.nextInt(); cost = cost + (flowerPrice[i]*stems); stemcost = (flowerPrice[i]*stems); System.out.println(flowerArray[i]+": "+ stems + " at \$"+ df.format(flowerPrice[i])+ " will cost \$"+ df.format((flowerPrice[i]*stems))); System.out.println(); System.out.println(); System.out.println(); Answer = '\0'; System.out.print("Do you want to purchase flowers (Y or N)?"); String tstring = in.next(); char temp2 = tstring.charAt(0); Answer = temp2; System.out.println(i); }//end if //else if (flowerPrice[i]==0.00){System.out.println(i);}   //if(stemcost == 0) {System.out.println("Sorry we don't have that flower.");} }//end for }// end while   System.out.println("The total cost is : \$"+df.format(cost)); }//end class }//end main```
• November 23rd, 2011, 07:25 PM
Herah
Re: Problem with loop?
What is Answer = '\0' doing?
• November 23rd, 2011, 07:27 PM
Rilstin81
Re: Problem with loop?
It may be doing nothing, but what it was supposed to be doing was putting a null in to the Answer char because each time we ran it before it would automatically enter in a y for the loop and keep running it.
• November 23rd, 2011, 07:34 PM
Herah
Re: Problem with loop?
Nothing is automatic. If it was "automatically entering a y", it was coming from somewhere. Setting it null in the middle of the while loop wouldn't matter, since it's then changed again right after.
For one thing, you keep going between upper and lower case y. Maybe throw an ignorecase in there.

char Answer = 'y'; //lower case
System.out.print("Do you want to purchase flower (Y or N)?"); // upper case question
while(Answer == 'y'){ // would only recognize lower case
System.out.print("Do you want to purchase flowers (Y or N)?"); // upper case

also, your forloop is going to run 11 times no matter whether the user says yes or no to the second want to buy flowers question, since it doesn't check the answer again until the while loop exits.
• November 23rd, 2011, 07:41 PM
Rilstin81
Re: Problem with loop?
Yes I would assume that it kept grabbing the yes that was entered the first time and re-entering it in to the Answer before it allowed the user to put yes or no. I believe that we tried to do the same as what you posted but got a run time error saying that the stringindexoutofbounds on the second test. The first test would allow yes/no but when it went to the test inside the loop for the CharAt(0) it would give the outofbounds error.
• November 23rd, 2011, 07:48 PM
Junky
Re: Problem with loop?