Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Problem with loop?

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    My Mood
    Amazed
    Thanks
    2
    Thanked 0 Times in 0 Posts

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

    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


  2. #2
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Stressed
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Problem with loop?

    What is Answer = '\0' doing?

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    My Mood
    Amazed
    Thanks
    2
    Thanked 0 Times in 0 Posts

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

  4. #4
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Stressed
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default 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
    Answer = in.next().charAt(0);
    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.

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    My Mood
    Amazed
    Thanks
    2
    Thanked 0 Times in 0 Posts

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

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,549
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: Problem with loop?

    Answer = in.next().charAt(0);

    You issue is here. The next method reads a single word and does not consume the return. Then when you prompt the user to enter a flower the empty string between the previous input and the return is scanned.
    Improving the world one idiot at a time!

  7. The Following User Says Thank You to Junky For This Useful Post:

    Rilstin81 (November 23rd, 2011)

Similar Threads

  1. [SOLVED] for loop problem
    By javaneedhelp in forum Loops & Control Statements
    Replies: 3
    Last Post: October 9th, 2011, 10:25 AM
  2. While loop problem
    By ak120691 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 8th, 2011, 03:09 PM
  3. Problem with Loop
    By Dragonkndr712 in forum What's Wrong With My Code?
    Replies: 13
    Last Post: February 8th, 2011, 11:12 AM
  4. Problem with For Loop
    By mingming8888 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 27th, 2010, 12:23 PM
  5. Little Loop Problem
    By chronoz13 in forum Loops & Control Statements
    Replies: 1
    Last Post: October 17th, 2009, 04:40 AM

Tags for this Thread