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 3 of 3

Thread: while loop wont exit

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default while loop wont exit

    when I run the program the while loop never stops and i thought i had it right cause my while loop it says while(bookTitle!="done") this means while bookTitle is not equal to done than execute. but when i type in done it still goes into the loop. can someone tell me what i am doing wrong? Please



    // BookSales.java - This program calculates the total of daily

    sales for a bookstore.
    // Input: Book title and book transaction amount.
    // Output: Prints the total of your book sales.

    import javax.swing.JOptionPane;

    public class BookSales
    {
    public static void main(String args[])
    {
    String bookTitle=""; // Title of book.
    String stringAmount;
    double bookAmount;
    double sum = 0;

    bookTitle = JOptionPane.showInputDialog( "Enter title of book or

    the word done to quit.");


    while(bookTitle!="done"){

    stringAmount = JOptionPane.showInputDialog("Enter price of book");

    bookAmount = Double.parseDouble(stringAmount);

    sum+=bookAmount;

    bookTitle = JOptionPane.showInputDialog( "Enter title of book or

    the word done to quit.");
    }

    System.out.println("Sum of daily book sales is $:

    " + sum);

    System.exit(0);
    } // End of main() method.

    } // End of BookSales class.


  2. #2
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: while loop wont exit

    try:

    while (!bookTitle.equalsIgnoreCase("done"))

  3. The Following User Says Thank You to d9988 For This Useful Post:

    NewAtJava (November 1st, 2011)

  4. #3
    Member
    Join Date
    Oct 2011
    Posts
    42
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: while loop wont exit

    Do not use operators like == or != to compare Strings, use String's equals() method is the correct way.

    java exception
    Last edited by hns1984; January 11th, 2012 at 05:53 PM.

  5. The Following User Says Thank You to hns1984 For This Useful Post:

    NewAtJava (November 1st, 2011)

Similar Threads

  1. Exit Status from Perl program called by java using runtime
    By twk in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 2nd, 2011, 02:19 PM
  2. Can't seem to understand why my while loop doesn't exit!
    By vortexnl in forum Loops & Control Statements
    Replies: 6
    Last Post: February 11th, 2011, 02:43 PM
  3. [SOLVED] Simple While loop wont work??
    By chuckie987 in forum Loops & Control Statements
    Replies: 1
    Last Post: January 31st, 2011, 01:58 PM
  4. Jar self delete on exit?
    By KiwiProg in forum Java Theory & Questions
    Replies: 1
    Last Post: December 19th, 2010, 01:54 AM
  5. While Loop Exit with String
    By Zaroth in forum Loops & Control Statements
    Replies: 8
    Last Post: November 9th, 2009, 03:20 PM