Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 2nd, 2013, 07:09 AM
DJ2334 DJ2334 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 15 DJ2334 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 7 m 49 sec
Reputation Power: 0
Delete thread

EDIT: Nevermind, I figured the rest out myself. Thanks for the help!

Reply With Quote
  #2  
Old March 2nd, 2013, 09:54 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
Please edit you post and change the quote tags to code tags.

Reply With Quote
  #3  
Old March 2nd, 2013, 05:26 PM
DJ2334 DJ2334 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 15 DJ2334 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 7 m 49 sec
Reputation Power: 0
Ok, I've updated the OP. Hope it makes more sense now!

Reply With Quote
  #4  
Old March 2nd, 2013, 05:34 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
Quote:
I want extra spacing for this:

Add a \n at the end of the String printed by println() to get an extra newline characer printed which will cause a blank line to be printed.

Reply With Quote
  #5  
Old March 2nd, 2013, 05:40 PM
DJ2334 DJ2334 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 15 DJ2334 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 7 m 49 sec
Reputation Power: 0
Quote:
Originally Posted by NormR
Add a \n at the end of the String printed by println() to get an extra newline characer printed which will cause a blank line to be printed.


haha, can't believe I forgot that. I feel stupid now. So, do you know how to solve my first problem to get rid of the extra "Enter subtotal: Error! Invalid number. Try again." that shows up when I enter an invalid subtotal like a letter?

Reply With Quote
  #6  
Old March 2nd, 2013, 06:12 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
Quote:
Problem here:
Enter customer type (r/c): r
Enter subtotal: j
Error! Invalid number. Try again.

Enter subtotal: Error! Invalid number. Try again.

Enter subtotal:

Code:
import java.text.NumberFormat;
import java.util.Scanner;
import java.util.*;

public class InvoiceApp
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner("r j\n"); //System.in);
        String choice = "y";
        while (!choice.equalsIgnoreCase("n"))
        if(choice.equalsIgnoreCase("y"))    
        {
            // get the input from the user
            String customerType = getValidCustomerType(sc);
            double subtotal = 0.0;
 
            // get the discount percent
            double discountPercent = 0.0;
            if (customerType.equalsIgnoreCase ("r") || customerType.equalsIgnoreCase ("c"))
            {        
                if (customerType.equalsIgnoreCase("R"))
                {
                    try
                    {   
                        subtotal = getValidSubtotal(sc);
                    }
                    catch (InputMismatchException e)
                    {
                        sc.next();
                        System.out.println("1Error! Invalid number. Try again.\n");
                        continue;
                    }   
                    if (subtotal < 100)
                        discountPercent = 0;
                    else if (subtotal >= 100 && subtotal < 250)
                        discountPercent = .1;
                    else if (subtotal >= 250)
                        discountPercent = .2;
                }
                else if (customerType.equalsIgnoreCase("C"))
                {
                    try
                    {   
                        subtotal = getValidSubtotal(sc);
                    }
                    catch (InputMismatchException e)
                    {
                        sc.next();
                        System.out.println("2Error! Invalid number. Try again.\n");
                        continue;
                    } 
                    if (subtotal < 250)
                    discountPercent = .2;
                    else
                    discountPercent = .3;
                }
            }    
            else if (customerType != null)
            {    
                sc.nextLine();
                System.out.println("Error! Invalid Customer Type. Please enter either 'r' or 'c' to continue.\n");
                continue;
            }
            // 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();
            sc.nextLine();
            System.out.println();
        }
    }
    public static String getValidCustomerType (Scanner sc)
    {
        // get the input from the user
        String customerType = "";
        boolean isValid = false;
        while (isValid == false)
        { 
            System.out.print("Enter customer type (r/c): ");
            if (sc.hasNext())
            {
                customerType = sc.next();
                System.out.println(customerType);           //<<<<<<<<<<<<
                isValid = true;
            }
            else
            {
                sc.nextLine();
                System.out.println("Error! Invalid Customer Type. Please enter either 'r' or 'c' to continue.\n");
            }         
        }
        return customerType;
    }   
    public static double getValidSubtotal (Scanner sc)
    {
        double subtotal = 0.0;
        boolean isValid = false;

        while (isValid == false)
        {
            //get a valid double value
            System.out.print("Enter subtotal:   ");
            if (sc.hasNextDouble())
            {
                subtotal = sc.nextDouble();
                isValid = true;
            }
            else
            {
                String str = sc.nextLine();
                System.out.println(str);        //<<<<<<<<<<<<<
                System.out.println("3Error! Invalid number:"+str+" Try again.\n");
            }    
      
            if (isValid == true && subtotal <= 0)
            {                
                System.out.println("Error! Number must be greater than 0. Try entering a greater number.");
                isValid = false;
            }
            else if (subtotal >= 10000)
            {
                System.out.println("Error! Number must be less than 10000. Try entering a number with a lower value.");
                isValid = false;
            }    
        }     //  end while()
        return subtotal;
    }        
}



I see that same message is printed in three places. Which one is being printed? Or are two different ones printing? For debugging add some letter to the messages to make them all unique. Also include the invalid value in the message that is printed.

Last edited by NormR : March 3rd, 2013 at 05:38 AM.

Reply With Quote
  #7  
Old March 2nd, 2013, 09:23 PM
DJ2334 DJ2334 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 15 DJ2334 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 7 m 49 sec
Reputation Power: 0
Quote:
Originally Posted by NormR
I see that same message is printed in three places. Which one is being printed? Or are two different ones printing? For debugging add some letter to the messages to make them all unique. Also include the invalid value in the message that is printed.


I messed with it a little and it's still acting a little funny. I notice whenever I put "r +SPACE q" (I do this to see if my code ignores the second entry) that the extra line that says "Enter subtotal: Error! Invalid number. Try again." shows up and it also shows up when I put a random letter in the subtotal entry, but only after I first put an entry in for "Enter customer type (r/c)". Is this a glitch or something? (Sorry, if that didn't make sense).

Reply With Quote
  #8  
Old March 2nd, 2013, 11:09 PM
DJ2334 DJ2334 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 15 DJ2334 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 7 m 49 sec
Reputation Power: 0
I found a way around the last problem, but now I'm wondering what do I have to do to make that one error message display. More info in the OP which I edited!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Very confused. Help would be appreciated.

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap