Results 1 to 12 of 12
- 04-29-2011, 03:16 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
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
- 04-29-2011, 03:22 PM #2
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!
- 04-29-2011, 03:45 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 1
- Rep Power
- 0
Increment the nameCounter inside the while loop like nameCounter=namecounter+1;
- 04-29-2011, 04:08 PM #4
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
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);
}
}
- 04-29-2011, 04:13 PM #5
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!
- 04-29-2011, 04:39 PM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
- 04-29-2011, 04:44 PM #7
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
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.
- 04-29-2011, 05:00 PM #8
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
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
- 04-29-2011, 05:19 PM #9
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!
- 04-29-2011, 05:21 PM #10
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
- 04-29-2011, 05:26 PM #11
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
- 04-29-2011, 05:28 PM #12
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
-
connection = DriverManager.getConnection(DATABASE_URL,'"+userid +"','"+password+"');
By renu in forum New To JavaReplies: 3Last Post: 10-12-2010, 04:21 PM -
An "if" statement inside a "for" loop?
By soccermiles in forum New To JavaReplies: 18Last Post: 04-20-2010, 03:44 AM -
How to change my form design from "metal" to "nimbus" in Netbeans 6.7.1?
By mlibot in forum New To JavaReplies: 1Last Post: 01-21-2010, 09:20 AM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks