Results 1 to 12 of 12
  1. #1
    miss_peaches is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Red face The "while" statement that never ends!!"

    Hello, My code compiles and gives me a .class file but my code keeps looping after 5 names have been entered. I've tried moving the end of the "while" statement to the end of "enter dollar amount of donations" but it still keeps looping. Please help!!



    Java Code:
    /**
     * The AddressBookApp1 class implements an application that
     * simply prints address and charitable donations for 5 individuals
     * and terminates. If a negative value is entered, the user is 
     * prompted to enter a positive value.
     */
    class AddressBookApp1 {
    	// main method begins program execution
        	public static void main(String[] args) {
    
            //create scanner to obtain input from command window
    	java.util.Scanner input = new java.util.Scanner (System.in);
    
    	String name; 	// Donor's name
    	String address;	// Donor's address
    	String city; 	// City where donor lives
    	String state;	// State where donor lives
    	int zipcode;	        // Zipcode of owner's address
    	int numDonat;	// Number of donations
    	int amtDonat;	// Dollar amount of donations
    	int nameCounter; // Number of names to be entered
    	double total;	// Total amount of donations this year
    	
    	// initialization phase
    	name = "Name"; 
    	address = "Address";
    	city = "City";
    	state = "State";
    	zipcode = 0;
    	nameCounter = 0;
    	total = 0;
    	amtDonat = 0;
    	numDonat = 0;
    
    	// processing phase
    	while (nameCounter <= 5) // loop 5 times
    	{
    		System.out.print ("Enter the donor's name:"); //prompt
    		name = input.nextLine(); // read donor's name from user input
    	
    		System.out.print ("Enter donor's address:"); //prompt
    		address = input.nextLine(); // read address from user input
    	
    		System.out.print ("Enter donor's city:"); //prompt
    		city = input.nextLine(); // read city from user input
    
    		System.out.print ("Enter donor's state:"); // prompt
    		state = input.nextLine(); // read state from user input
    	
    		System.out.print ("Enter donor's zipcode:"); //prompt
    		zipcode = input.nextInt(); // read zipcode from user input
    
    		System.out.print ("Enter the number of donations this year:"); 
    		numDonat= input.nextInt(); // read number of donations 
    		
    		System.out.print ("Enter dollar amount of donations:"); //prompt
    		amtDonat= input.nextInt(); // read donation amount 
    	
    		total = numDonat * amtDonat;
    
    	System.out.print ("The donor, whose name is"+ name);
    	System.out.print ("and resides at"+ address);
    	System.out.print ("in the city of"+ city);
    	System.out.print ("in the state of"+ state);
    	System.out.print ("with a zipcode of"+ zipcode);
    	System.out.print ("has donated $" + total);
    	System.out.println ("this year!");
    	} //end while
    
            } // end method main
    } // end class AddressBook

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,956
    Rep Power
    8

    Default

    When do you increment nameCounter?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    sharkymo is offline Member
    Join Date
    Apr 2011
    Posts
    1
    Rep Power
    0

    Default

    Increment the nameCounter inside the while loop like nameCounter=namecounter+1;

  4. #4
    M11ke is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    do a Do while loop hun:

    /* A do while loop*/

    public class doWhile
    {
    public static void main(String[] args)
    {
    int count = 1;

    //start doing things
    do
    {
    System.out.println("Count is: " + count);
    count++;
    }
    //Keep on doing things while the condition is TRUE
    while (count <=10);
    }
    }

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,956
    Rep Power
    8

    Default

    Calm down, fellas. Spoonfeeding != helping.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by M11ke View Post
    do a Do while loop hun:
    Not the same thing. A do..while always executes once (post-conditional). A while loop may not execute at all (pre-conditional).

  7. #7
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    You're better off using a for loop, since you know how many repetitions you need. Use while when you're looping an unknown number of times.

  8. #8
    miss_peaches is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default

    Thanks everyone. I incorporated the counter and it works fine now....only my output isn't coming out the way I want it to. I've tried: $%d, $%.2f\n, and $%,20.2f\n Please help.....again :(

    Java Code:
    /**
     * The AddressBookApp1 class implements an application that
     * simply prints address and charitable donations for 5 individuals
     * and terminates. If a negative value is entered, the user is 
     * prompted to enter a positive value.
     */
    class AddressBookApp1 {
    	// main method begins program execution
        	public static void main(String[] args) {
    
            //create scanner to obtain input from command window
    	java.util.Scanner input = new java.util.Scanner (System.in);
    
    	String name; 	// Donor's name
    	String address;	// Donor's address
    	String city;	// City where donor lives
    	String state;	// State where donor lives
    	int zipcode;	// Zipcode of owner's address
    	int numDonat;	// Number of donations
    	int amtDonat;	// Dollar amount of donations
    	int nameCounter;// Number of names to be entered
    	double total;	// Total amount of donations this year
    	
    	// initialization phase
    	name = "Name"; 
    	address = "Address";
    	city = "City";
    	state = "State";
    	zipcode = 0;
    	nameCounter = 0;
    	total = 0;
    	amtDonat = 0;
    	numDonat = 0;
    
    	// processing phase
    	while (nameCounter <= 5) // loop 5 times
    	{
    	   System.out.print ("Enter the donor's name:"); //prompt
    	   name = input.nextLine(); // read donor's name from user input
    	
      	   System.out.print ("Enter donor's address:"); //prompt
               address = input.nextLine(); // read address from user input
    	
               System.out.print ("Enter donor's city:"); //prompt
               city = input.nextLine(); // read city from user input
    
    	   System.out.print ("Enter donor's state:"); // prompt
    	   state = input.nextLine(); // read state from user input
    	
    	   System.out.print ("Enter donor's zipcode:"); //prompt
    	   zipcode = input.nextInt(); // read zipcode from user input
    
    	   System.out.print ("Enter the number of donations this year:"); //prompt
    	   numDonat= input.nextInt(); // read number of donations from user input
    		
    	   System.out.print ("Enter dollar amount of donations:"); //prompt
    	   amtDonat= input.nextInt(); // read donation amount from user input
    	
    	
    	   System.out.print ("The donor, whose name is"+ name);
    	   System.out.print ("and resides at"+ address);
    	   System.out.print ("in the city of"+ city);
    	   System.out.print ("in the state of"+ state);
    	   System.out.print ("with a zipcode of"+ zipcode);
    	   System.out.print ("has donated $%d" + total);
    	   System.out.println ("this year!");
    
    	   total = amtDonat * numDonat;
    	   nameCounter = nameCounter + 1;
    	} //end while
    
    	} // end main method
    } // end class AddressBookApp1

  9. #9
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,956
    Rep Power
    8

    Default

    Where is this print statement you're talking about? And what does it do instead of what you'd expect?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    miss_peaches is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default

    Quote Originally Posted by Iron Lion View Post
    You're better off using a for loop, since you know how many repetitions you need. Use while when you're looping an unknown number of times.
    You were indeed correct!! Thanks a lot. The while loop didn't pan out all that great but it works fine with the for loop.

  11. #11
    miss_peaches is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    Where is this print statement you're talking about? And what does it do instead of what you'd expect?
    It says System.out.print ("has donated $%d" + total");
    near the bottom..
    I realized one error.....I forgot to put the "f" on the end of print(f) but my output is still incorrect.

  12. #12
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,956
    Rep Power
    8

    Default

    Read the documentation on the printf() function: PrintStream (Java Platform SE 6)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 3
    Last Post: 10-12-2010, 04:21 PM
  2. An "if" statement inside a "for" loop?
    By soccermiles in forum New To Java
    Replies: 18
    Last Post: 04-20-2010, 03:44 AM
  3. Replies: 1
    Last Post: 01-21-2010, 09:20 AM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •