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

Thread: Code Validation help??

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Code Validation help??

    I am in a beginner Java class and we are supposed to validate the user input to only accept customer type r, c, or t. My code however, throws the error message with every entry, even valid ones. Can someone help me know what i'm doing wrong? Thanks in advance!
    import java.text.NumberFormat;
    import java.util.*;
     
    public class ValidatedInvoiceApp
    {
    	public static void main(String[] args)
        {
     
            Scanner sc = new Scanner(System.in);
            String choice = "y";
     
            while (!choice.equalsIgnoreCase("n"))
    		{
            	// get the input from the user
            	double subtotal = 0.0;
            	String customerType = "";
     
            	System.out.print("Enter customer type (r/c/t): ");
    			if (sc.hasNext())
    			{
    				if (customerType.equalsIgnoreCase ("r") || customerType.equalsIgnoreCase ("c") || customerType.equalsIgnoreCase ("t"))
    				{
    					customerType = sc.next();
    				}
    				else
    				{
     
    					sc.nextLine();
    					System.out.println("Error! Invalid Customer Type.  Please Try Again. \n");
    					continue;
    				}
    			}
    			else
    			{
    			System.out.print("Enter subtotal:   ");
    			subtotal = sc.nextDouble();
     
            	double discountPercent = getDiscountPercent(customerType, subtotal);
     
            	// calculate the discount amount and total
            	double discountAmount = subtotal * discountPercent;
            	double total = subtotal - discountAmount;
     
            	// format and display the results
            	NumberFormat currency = NumberFormat.getCurrencyInstance();
            	NumberFormat percent = NumberFormat.getPercentInstance();
            	System.out.println(
            	    "Discount percent: " + percent.format(discountPercent) + "\n" +
            	    "Discount amount:  " + currency.format(discountAmount) + "\n" +
            	    "Total:            " + currency.format(total) + "\n");
     
            	// see if the user wants to continue
    			System.out.print("Continue? (y/n): ");
    			choice = sc.next();
                System.out.println();
    			}
    		}
     
     
        }
    private static double getDiscountPercent (String customerType, double subtotal)
    	{
    	// get the discount percent
    		double discountPercent = 0;
    		if (customerType.equalsIgnoreCase("R"))
    		{
    			if (subtotal < 100)
    			    discountPercent = 0;
    			else if (subtotal >= 100 && subtotal < 250)
    			    discountPercent = .1;
    			else if (subtotal >= 250 && subtotal < 500)
    				 discountPercent = .25;
    	  	    else
    		    	discountPercent = .30;
    		 	}
    		   	else if (customerType.equalsIgnoreCase("C"))
    		  	{
    		        discountPercent = .20;
    		   	}
    		  	else if (customerType.equalsIgnoreCase("T"))
    		   	{
    			if (subtotal < 500)
    				discountPercent = .40;
    			else if (subtotal >= 500)
    				discountPercent = .50;
    			}
    		return discountPercent;
    	}
     
     
    }
    Last edited by helloworld922; September 29th, 2010 at 10:33 PM.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Code Validation help??

    Quote Originally Posted by kmh90210 View Post
    I am in a beginner Java class and we are supposed to validate the user input to only accept customer type r, c, or t. My code however, throws the error message with every entry, even valid ones. Can someone help me know what i'm doing wrong? Thanks in advance!

    import java.text.NumberFormat;
    import java.util.*;
     
    public class ValidatedInvoiceApp
    {
    	public static void main(String[] args)
        {
     
            Scanner sc = new Scanner(System.in);
            String choice = "y";
     
            while (!choice.equalsIgnoreCase("n"))
    		{
            	// get the input from the user
            	double subtotal = 0.0;
            	String customerType = "";
     
            	System.out.print("Enter customer type (r/c/t): ");
    			if (sc.hasNext())
    			{
    				if (customerType.equalsIgnoreCase ("r") || customerType.equalsIgnoreCase ("c") || customerType.equalsIgnoreCase ("t"))
    				{
    					customerType = sc.next();
    				}
    				else
    				{
     
    					sc.nextLine();
    					System.out.println("Error! Invalid Customer Type.  Please Try Again. \n");
    					continue;
    				}
    			}
    			else
    			{
    			System.out.print("Enter subtotal:   ");
    			subtotal = sc.nextDouble();
     
            	double discountPercent = getDiscountPercent(customerType, subtotal);
     
            	// calculate the discount amount and total
            	double discountAmount = subtotal * discountPercent;
            	double total = subtotal - discountAmount;
     
            	// format and display the results
            	NumberFormat currency = NumberFormat.getCurrencyInstance();
            	NumberFormat percent = NumberFormat.getPercentInstance();
            	System.out.println(
            	    "Discount percent: " + percent.format(discountPercent) + "\n" +
            	    "Discount amount:  " + currency.format(discountAmount) + "\n" +
            	    "Total:            " + currency.format(total) + "\n");
     
            	// see if the user wants to continue
    			System.out.print("Continue? (y/n): ");
    			choice = sc.next();
                System.out.println();
    			}
    		}
     
     
        }
    private static double getDiscountPercent (String customerType, double subtotal)
    	{
    	// get the discount percent
    		double discountPercent = 0;
    		if (customerType.equalsIgnoreCase("R"))
    		{
    			if (subtotal < 100)
    			    discountPercent = 0;
    			else if (subtotal >= 100 && subtotal < 250)
    			    discountPercent = .1;
    			else if (subtotal >= 250 && subtotal < 500)
    				 discountPercent = .25;
    	  	    else
    		    	discountPercent = .30;
    		 	}
    		   	else if (customerType.equalsIgnoreCase("C"))
    		  	{
    		        discountPercent = .20;
    		   	}
    		  	else if (customerType.equalsIgnoreCase("T"))
    		   	{
    			if (subtotal < 500)
    				discountPercent = .40;
    			else if (subtotal >= 500)
    				discountPercent = .50;
    			}
    		return discountPercent;
    	}

    }
    Shouldn't Scanner be static? main is static. Also, what type of error message?

    Also, if customer type is "" when it reaches the if loop, since you don't change it unless it's equal to those values, won't it always go to the else every time?

  3. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Code Validation help??

    When I run the code regardless of what customer type I enter it displays the error message in my loop. I can't get it to where it goes to the outside loop asking for the subtotal amount.

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Code Validation help??

    boolean isAValidType = false;
    while (isAValidType == false)
    {
    System.out.print("Enter customer type (r/c/t): ");
    customerType = sc.next();
    if (customerType.equalsIgnoreCase ("r") || customerType.equalsIgnoreCase ("c") || customerType.equalsIgnoreCase ("t"))
    				{
    					isAValidType = true;
    				}
    				else
    				{
     
    					sc.nextLine(); // what's this do?
                                           customerType = ""; // resets customer type
    					System.out.println("Error! Invalid Customer Type.  Please Try Again. \n");
    					//continue;
                                        isAValidType = false
     
    				}
     
    } // end while

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Code Validation help??

    throws the error message
    What is the error message?
    Can you run the program in a console window and copy and paste the contents of the window here?

    Please edit your code and add code tags to preserver the formatting. Info here: Java Forums - BB Code List

  6. #6
    Junior Member
    Join Date
    Sep 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Code Validation help??

    Enter customer type (r/c/t): r
    Error! Invalid Customer Type. Please Try Again.
    Norm,

    Below is what the console prints out...the error message that I referred to was the error message I had created when invalid data was entered.


    Enter customer type (r/c/t): c
    Error! Invalid Customer Type. Please Try Again.

    Enter customer type (r/c/t): t
    Error! Invalid Customer Type. Please Try Again.

    Enter customer type (r/c/t):

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Code Validation help??

    Try debugging your code by adding print outs of the values that are being tested in the if statement
    Also print out the value of the choice variable that controls the loop.


    Also edit your code and add code tags. It is hard to read.

  8. #8
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Code Validation help??

    Your problem is that you don't put anything in the String customerType. Just add the commented line.

    import java.text.NumberFormat;
    import java.util.*;
     
    public class ValidatedInvoiceApp
    {
        public static void main(String[] args)
        {
     
            Scanner sc = new Scanner(System.in);
            String choice = "y";
     
            while (!choice.equalsIgnoreCase("n"))
            {
                // get the input from the user
                double subtotal = 0.0;
                String customerType = "";
     
                System.out.print("Enter customer type (r/c/t): ");
                ////////////////////customerTyper = sc.nextLine();
                if (sc.hasNext())
                {
                    if (customerType.equalsIgnoreCase ("r") || customerType.equalsIgnoreCase ("c") || customerType.equalsIgnoreCase ("t"))
                    {
                        customerType = sc.next();
                    }
                    else
                    {
     
                        sc.nextLine();
                        System.out.println("Error! Invalid Customer Type.  Please Try Again. \n");
                        continue;
                    }
                }
                else
                {
                System.out.print("Enter subtotal:   ");
                subtotal = sc.nextDouble();
     
                double discountPercent = getDiscountPercent(customerType, subtotal);
     
                // calculate the discount amount and total
                double discountAmount = subtotal * discountPercent;
                double total = subtotal - discountAmount;
     
                // format and display the results
                NumberFormat currency = NumberFormat.getCurrencyInstance();
                NumberFormat percent = NumberFormat.getPercentInstance();
                System.out.println(
                    "Discount percent: " + percent.format(discountPercent) + "\n" +
                    "Discount amount:  " + currency.format(discountAmount) + "\n" +
                    "Total:            " + currency.format(total) + "\n");
     
                // see if the user wants to continue
                System.out.print("Continue? (y/n): ");
                choice = sc.next();
                System.out.println();
                }
            }
     
     
        }
    private static double getDiscountPercent (String customerType, double subtotal)
        {
        // get the discount percent
            double discountPercent = 0;
            if (customerType.equalsIgnoreCase("R"))
            {
                if (subtotal < 100)
                    discountPercent = 0;
                else if (subtotal >= 100 && subtotal < 250)
                    discountPercent = .1;
                else if (subtotal >= 250 && subtotal < 500)
                     discountPercent = .25;
                else
                    discountPercent = .30;
                }
                else if (customerType.equalsIgnoreCase("C"))
                {
                    discountPercent = .20;
                }
                else if (customerType.equalsIgnoreCase("T"))
                {
                if (subtotal < 500)
                    discountPercent = .40;
                else if (subtotal >= 500)
                    discountPercent = .50;
                }
            return discountPercent;
        }
     
     
    }

Similar Threads

  1. Should validation code exist in set methods or in a validate method
    By mydarkpassenger in forum Java Theory & Questions
    Replies: 4
    Last Post: May 27th, 2010, 08:37 AM
  2. Exception during xml validation
    By vijeta in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 6th, 2010, 02:50 AM
  3. IPv6 validation
    By subhvi in forum Java Networking
    Replies: 1
    Last Post: November 27th, 2009, 07:19 AM
  4. Input Validation
    By nic in forum AWT / Java Swing
    Replies: 4
    Last Post: November 18th, 2009, 10:54 AM
  5. Problems with If validation
    By websey in forum Loops & Control Statements
    Replies: 1
    Last Post: November 18th, 2009, 09:43 AM

Tags for this Thread