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

Thread: Having problems with output to console....help!

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Having problems with output to console....help!

    My output is not printing to the screen, I only get zero's. I ran a dummy out.print in the getCustType and the values of sCustomerType and subtotal were printing right. But when I call the getInvoice method, the values do not follow. Can someone help me figure this out? do I need to use a setter method?
    Here is my code:
    Main method:

    public class InvoiceApp
    {
        public static void main(String[] args)
        {
    	 	String message;
    		double discountPercent=0;
    		double discountAmount=0;
    		double total=0;
    		String customerType="";
    		double subtotal=0;
    		String sCustomerType="";
     
    	 	// display a welcome message
                    System.out.println("Welcome to the Invoice Total Calculator");
                    System.out.println();  // print a blank line
     
                     Invoice myInvoice = new Invoice;
    		//get customer type
    		myInvoice.getCustType(sCustomerType, subtotal);
     
     
    		//get invoice output
    		myInvoice.getInvoice(sCustomerType, customerType, discountPercent, discountAmount, total, subtotal);//get formatted output in a string for printing
    	   }
    	}

    Then the Invoice Class:

    public class Invoice{
     
    double percent;
    double currency;
    String sSubtotal;
    String sCustomerType;
     
     
    	public String getCustType(String sCustomerType, double subtotal){
     
    		Scanner sc = new Scanner(System.in);
           		String choice = "y";
           		while(choice.equalsIgnoreCase("y"))
          		 {
             		// get user entries
             		String customerType = Validator.getString(sc,
                             "Enter customer type (r/c):   ");
    			 subtotal = Validator.getDouble(sc,
                             "Enter subtotal:              ", 0, 10000);
     
                if (customerType.equalsIgnoreCase("r"))
                	sCustomerType = "Retail";
                else if (customerType.equalsIgnoreCase("c"))
                	sCustomerType = "College";
     
    				//dummy line to check output
    				System.out.println("Customer Type: " + sCustomerType);	
    				System.out.println("subtotal: "+subtotal);
     
     
    	    System.out.print("Continue? (y/n): ");
                choice = sc.next();
                System.out.println();
    				}
     
    	    return sCustomerType;
    		}	
     
    		public void getInvoice(String sCustomerType, String customerType, double subtotal){	// output formatted invoice	
     
    				double discountPercent;
    				//double subtotal;
    				// customerType;
    				double discountAmount;
    				//double discountPercent;
    				double total;
     
     
    		// calculate the discount amount and total
                discountPercent = getDiscountPercent(subtotal, customerType);
                discountAmount = subtotal * discountPercent;
                total = subtotal - discountAmount;
     
    		// format the values
                NumberFormat currency = NumberFormat.getCurrencyInstance();
                NumberFormat percent = NumberFormat.getPercentInstance();
     
    	    String sDiscountPercent = percent.format(discountPercent);
                String sDiscountAmount = currency.format(discountAmount);
                String sTotal = currency.format(total);
                String sSubtotal = currency.format(subtotal);
     
     
                // format and display the results
                String message = "Subtotal:         " + sSubtotal + "\n"
                               + "Customer type:    " + sCustomerType + "\n"
                               + "Discount percent: " + sDiscountPercent + "\n"
                               + "Discount amount:  " + sDiscountAmount + "\n"
                               + "Total:            " + sTotal + "\n";
                System.out.println();
                System.out.println(message);
     
     
     
    	}		
     
    	 public double getDiscountPercent(double subtotal, String type)
        {
            double discountPercent = 0.0;
            if (type.equalsIgnoreCase("r"))
            {
                if (subtotal >= 500)
                    discountPercent = .2;
                else if (subtotal >= 250 && subtotal < 500)
                    discountPercent =.15;
                else if (subtotal >= 100 && subtotal < 500)
                    discountPercent =.1;
                else if (subtotal < 100)
                    discountPercent =.0;
            }
            else if (type.equalsIgnoreCase("c"))
            {
                    discountPercent = .2;
            }
            else
                discountPercent = .05;
     
            return discountPercent;
         }
     
            }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Having problems with output to console....help!

    Are there any error messages printed?
    when I call the getInvoice method, the values do not follow.
    What is printed out by the getInvoice() method?
    Add some more printlns to show the values of variables as they are set and as they change.

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having problems with output to console....help!

    I placed prinln's along the way to see what was happening, but there was nothing in the variables-blanks were printed. And no errors.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Having problems with output to console....help!

    there was nothing in the variables-blanks were printed
    Ok. Did the code execute thru the methods the way you expect? Did all the printlns execute?
    Next question, why do the variables contain blanks instead of what you want there?

    Are all the variables you printed Strings?
    If you print any double or int variables, they will NOT print as blanks.

  5. #5
    Junior Member
    Join Date
    Apr 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having problems with output to console....help!

    Yes the program executed the way i expected it to.
    The string output is the way i have to have it formatted
    I'm not sure why blanks are being printed...thats what i'm trying to figure out.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Having problems with output to console....help!

    Can you post the output from the program that shows the problem?
    And add some comments on the lines that are blanks.
    Also can you post the code for the program.

  7. #7
    Junior Member
    Join Date
    Apr 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having problems with output to console....help!

    Quote Originally Posted by Norm View Post
    Can you post the output from the program that shows the problem?
    And add some comments on the lines that are blanks.
    Also can you post the code for the program.

    My Code:
    Main:

    public class InvoiceApp
    {
        public static void main(String[] args)
        {
    	 	String message;
    		double discountPercent=0;
    		double discountAmount=0;
    		double total=0;
    		String customerType="";
    		double subtotal=0;
    		String sCustomerType="";
     
          // display a welcome message
          System.out.println("Welcome to the Invoice Total Calculator");
          System.out.println();  // print a blank line
     
    		Invoice myInvoice = new Invoice();
     
    		Scanner sc = new Scanner(System.in);
           		String choice = "y";
           		while(choice.equalsIgnoreCase("y"))
          		 {
             		// get user entries
             		 customerType = Validator.getString(sc,
                    "Enter customer type (r/c):   ");
    					 subtotal = Validator.getDouble(sc,
                    "Enter subtotal:              ", 0, 10000);
     
                if (customerType.equalsIgnoreCase("r"))
                	sCustomerType = "Retail";
                else if (customerType.equalsIgnoreCase("c"))
                	sCustomerType = "College";
     
    				//dummy line to check output
    				System.out.println("Customer Type: " + sCustomerType);	
    				System.out.println("subtotal: "+subtotal);
     
     
    	    System.out.print("Continue? (y/n): ");
                choice = sc.next();
                System.out.println();
    				}

    getters and setters:
    public class Invoice{
     
    double percent;
    double currency;
    String sSubtotal;
    String sCustomerType;
     
     
    		public void calculate(String customerType, double subtotal){
               	double discountPercent=0;
    				double discountAmount=0;
    				double total=0;
     
     
    				// calculate the discount amount and total
                discountPercent = getDiscountPercent(subtotal, customerType, discountPercent);
                discountAmount = subtotal * discountPercent;
                total = subtotal - discountAmount;		      
    				//return discountPercent;			
    				}	
     
    		public String getMessege(String sCustomerType, double subtotal, double discountPercent, double discountAmount, double total){	// output formatted invoice	
     
     
    			System.out.println("D%: "+discountPercent+"D$: "+discountAmount+"ST: "+subtotal+"T: "+total);
     
    				// format the values
                NumberFormat currency = NumberFormat.getCurrencyInstance();
                NumberFormat percent = NumberFormat.getPercentInstance();
     
    				String sDiscountPercent = percent.format(discountPercent);
                String sDiscountAmount = currency.format(discountAmount);
                String sTotal = currency.format(total);
                String sSubtotal = currency.format(subtotal);
     
     
                // format and display the results
                String message = "Subtotal:         " + sSubtotal + "\n"
                               + "Customer type:    " + sCustomerType + "\n"
                               + "Discount percent: " + sDiscountPercent + "\n"
                               + "Discount amount:  " + sDiscountAmount + "\n"
                               + "Total:            " + sTotal + "\n";
                System.out.println();
                System.out.println(message);
     
    				return message;
     
    				}		
     
    	 public double getDiscountPercent(double subtotal, String customerType, double discountPercent)
        {
           // double discountPercent;
            if (customerType.equalsIgnoreCase("r"))
            {
                if (subtotal >= 500)
                    discountPercent = .2;
                else if (subtotal >= 250 && subtotal < 500)
                    discountPercent =.15;
                else if (subtotal >= 100 && subtotal < 500)
                    discountPercent =.1;
                else if (subtotal < 100)
                    discountPercent =.0;
            }
            else if (customerType.equalsIgnoreCase("c"))
            {
                    discountPercent = .2;
            }
            else
                discountPercent = .05;
     
            return discountPercent;
         }
     
            }

    Output:
    Welcome to the Invoice Total Calculator

    Enter customer type (r/c): r
    Enter subtotal: 33
    Customer Type: Retail <-----------dummy to check output after if/elses
    subtotal: 33.0 <-----------dummy to check output after if/elses
    Continue? (y/n): n

    Customer Type: Retail
    Subtotal: 33.0
    D%: 0.0D$: 0.0ST: 33.0T: 0.0 <-----------dummy to check output after calculate()

    Subtotal: $33.00
    Customer type: Retail
    Discount percent: 0%
    Discount amount: $0.00
    Total: $0.00
    I got it to stop printing blanks, but it seems like my vars are being "stopped" somewhere.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Having problems with output to console....help!

    it seems like my vars are being "stopped" somewhere.
    Please explain what "stopped" means.

    Where there any lines in the printed output you posted that show variables being "stopped"?
    Can you edit the post and write comments next to the output describing what's wrong?

  9. #9
    Junior Member
    Join Date
    Apr 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having problems with output to console....help!

    By "stopped" I mean they either are not being passed to getInvoice() or the output is not formatted right. After the calculate setter, the customerType and subtotal are still being passed and even into the getInvoice getter. Everything else is throwing zeros.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Having problems with output to console....help!

    This is what I get When I execute the code you posted:

    Welcome to the Invoice Total Calculator

    Customer Type: Retail
    subtotal: 33.0
    Continue? (y/n):

  11. #11
    Junior Member
    Join Date
    Apr 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having problems with output to console....help!

    right, now hit "n" and look at output.

  12. #12
    Junior Member
    Join Date
    Apr 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having problems with output to console....help!

    Can you tell me if I'm using the setter-calculate- right? Usually it's a void with no arguments right?

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Having problems with output to console....help!

    The code you posted does not call any methods to print out any report.
    Its sort of junk. It did not compile without my adding some additional }s

    BTW I removed all the console input and hard coded the values into the variables. My IDE does not support console input.

  14. #14
    Junior Member
    Join Date
    Apr 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having problems with output to console....help!

    Now type "n" and see what the formatted output does. Can you tell me if the setter method-calculate is right? Aren't setters void but have no parameters?

  15. #15
    Junior Member
    Join Date
    Apr 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having problems with output to console....help!

    If you look at my #7 post, you will see what output I get. Also, the copy/paste method here might leave some stuff behind. It compiles and runs on my machine.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Having problems with output to console....help!

    My program does not do console I/O. I hardcoded the responses in the program.

    Did you see this part of my last post?
    The code you posted does not call any methods to print out any report.
    Its sort of junk. It did not compile without my adding some additional }s


    If I don't have the code you are working with, I can NOT test it.

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Having problems with output to console....help!

    the copy/paste method here might leave some stuff behind
    Fix it so there is a good version of your code to work with.

  18. #18
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Having problems with output to console....help!

    get and set methods...can someone plese explain?

    Duplicate post!

    Sorry about starting new thread about this same prob.

    Obviously not that sorry that you posted the same question in a different forum.

    never again.

    Bullsh!t
    Improving the world one idiot at a time!

  19. #19
    Junior Member
    Join Date
    Apr 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having problems with output to console....help!

    my code:
    Main:
    public class InvoiceApp
    {
        public static void main(String[] args)
        {
    	 	String message;
    		double discountPercent=0;
    		double discountAmount=0;
    		double total=0;
    		String customerType="";
    		double subtotal=0;
    		String sCustomerType="";
     
    	 	// display a welcome message
          System.out.println("Welcome to the Invoice Total Calculator");
          System.out.println();  // print a blank line
     
          Invoice myInvoice = new Invoice();
     
    		Scanner sc = new Scanner(System.in);
           		String choice = "y";
           		while(choice.equalsIgnoreCase("y"))
          		 {
             		// get user entries
             	 customerType = Validator.getString(sc,
                    "Enter customer type (r/c):   ");
    		 subtotal = Validator.getDouble(sc,
                    "Enter subtotal:              ", 0, 10000);
     
                if (customerType.equalsIgnoreCase("r"))
                	sCustomerType = "Retail";
                else if (customerType.equalsIgnoreCase("c"))
                	sCustomerType = "College";
     
    				//dummy line to check output
    				System.out.println("Customer Type: " + sCustomerType);	
    				System.out.println("subtotal: "+subtotal);
     
     
    	    System.out.print("Continue? (y/n): ");
                choice = sc.next();
                System.out.println();
    				}
     
     
     
     
     
     
     
    		//get customer type
    		myInvoice.calculate(sCustomerType, subtotal);
     
    		//another check where is vars
    		System.out.println("Customer Type: " + sCustomerType);
    		System.out.println("Subtotal: " + subtotal);
     
    		//get invoice output
    		myInvoice.getMessage(sCustomerType, subtotal, discountPercent, discountAmount, total);//get formatted output in a string for printing
     
     
       }
    	}//end class

  20. #20
    Junior Member
    Join Date
    Apr 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having problems with output to console....help!

    Sorry I didn't realize part of my code didn't make it through.

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Having problems with output to console....help!

    Ok. I'm done. The code you posted does not compile with the earlier code.
    One has:
    public String getMessege(
    and the latest has:
    myInvoice.getMessage(

    Notice the spelling differences.

    You are wasting my time by posting this mismatched code over and over.

  22. #22
    Junior Member
    Join Date
    Apr 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having problems with output to console....help!

    Appreciate your patience.

Similar Threads

  1. Console box with scanner?
    By Hallowed in forum Java Theory & Questions
    Replies: 1
    Last Post: May 26th, 2011, 12:50 AM
  2. How do i run this Accounting console app?
    By mirzahat in forum AWT / Java Swing
    Replies: 2
    Last Post: November 16th, 2010, 12:22 AM
  3. Console Application - How to run it?
    By mirzahat in forum AWT / Java Swing
    Replies: 3
    Last Post: November 16th, 2010, 12:21 AM
  4. Changing output from console to .html
    By RSYR in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: December 10th, 2009, 07:55 PM
  5. printing output to console & to a text file at the same time...
    By prasanna in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: August 26th, 2009, 03:43 AM