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.


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

Results 1 to 3 of 3

Thread: Quick easy Java question.

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Thanked 0 Times in 0 Posts

    Default Quick easy Java question.

    I'm in a beginning Java class and am running into an issue right off the bat.

    I need to modify this so that the program ends only when "n" or "N" is entered. I've been using the not operator, but I'm either using it incorrectly, or I'm using it in the wrong place.

    Here is the code for the application:
    import java.util.Scanner;
    public class InvoiceApp
    public static void main(String[] args)
    System.out.println("Welcome to the Invoice Total Calculator");
    Scanner sc = new Scanner(System.in);
    String choice = "y";
    while (choice.equalsIgnoreCase("y")) //This is where I've been entering the not operator as (!choice.equalsIgnoreCase("n")), but when doing so, the program compiles without everything that follows (below), which leads me to believe I'm putting it in the wrong place.
    System.out.print("Enter subtotal:   ");
    double subtotal = sc.nextDouble();
    double discountPercent = 0.0;
    if (subtotal >= 200)
    discountPercent = .2;
    else if (subtotal >= 100)
    discountPercent = .1;
    discountPercent = 0.0
    double discountAmount = subtotal * discountPercent;
    double total = subtotal - discountAmount;
    String message = "Discount percent:  " + discountPercent + "\n"
    + "Discount amount:   " + discountAmount + "\n"
    + "Invoice total:   " + total + "\n";
    System.out.print("Continue?  (y/n):   ");
    choice = sc.next();
    System.out.println();  // Should the not operator go here?

  2. #2
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Minneapolis, MN
    Thanked 45 Times in 41 Posts

    Default Re: Quick easy Java question.

    When I run the code you have, replacing the equalsIgnoreCase with what you have commented out on the same line, it runs as expected.

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

    DHG (January 25th, 2011)

  4. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    United Kingdom
    My Mood
    Thanked 293 Times in 226 Posts
    Blog Entries

    Default Re: Quick easy Java question.

    I have compiled your code and it almost works.

    When you first run it, if you type N straight away, an exception is thrown. This is because the scanner is looking for a double value. It works ok if you enter a double value. When you are prompted to press y/n to continue at the end of the program, this function works fine and the program exits when N is entered.

    Are you looking to be able to exit this program with N on the very first prompt?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. [SOLVED] Begginer Question please help ASAP! (Easy to Answer)
    By Bagzli in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 11th, 2010, 10:00 PM
  2. scope - quick question
    By bbr201 in forum Java Theory & Questions
    Replies: 4
    Last Post: July 28th, 2010, 08:30 AM
  3. quick question - new keyword
    By bbr201 in forum Java Theory & Questions
    Replies: 2
    Last Post: July 18th, 2010, 09:43 PM
  4. Quick Question about Mergesort
    By Shadow703793 in forum Algorithms & Recursion
    Replies: 4
    Last Post: March 4th, 2010, 05:48 PM
  5. A Few Quick, Easy Questions
    By TheAsianMenace in forum Object Oriented Programming
    Replies: 1
    Last Post: February 24th, 2010, 02:47 PM