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

Thread: What is wrong with my code?

  1. #1
    Junior Member
    Join Date
    Sep 2021
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post What is wrong with my code?

    Ntebogeng is running a car wash business in Soshanguve. The business offers three washing services as shown in the table below.

    Service code	Description	                    Cost (R)
    G	                Wash and go	                    50.00
    P	                Wash , dry and polish.	    110.00
    E	                Executive wash	                    150.00
    She needs an application that will display the above information. Create such an application for Ntebogeng.

    here is my source code:


     
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package carwashapp;
     
    /**
     *
     * @author SETH
     */
    public class CarWashApp {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // declare variables
            char   code1  = 'G', code2 = 'P', code3 = 'E';
            String descr1 = "Wash and go" ,descr2 = "Wash,dry and polish", descr3 = "Executive Wash";
            double cost1 = 50.00, cost2 = 110.00 ,cost3 = 150.00;
            System.out.printf("%-6s%-20s%6s\n", "Service code","Description","Cost(R)");
            System.out.printf("%-6c%-20s%6.2f\n", code1       ,descr1        , cost1);
            System.out.printf("%-6c%-20s%6.2f\n", code2       , descr2       , cost2);
            System.out.printf("%-6c%-20s%6.2f",   code3       , descr3       , cost3);
        }
     
    }

    here's the output:

     
    Service codeDescription         Cost(R)
    G     Wash and go          50.00
    P     Wash,dry and polish 110.00
    E     Executive Wash      150.00



    My problem is that i can't display it as in the statement.
    Last edited by Norm; December 1st, 2021 at 03:18 PM. Reason: Changed \ to / in end code tag

  2. #2
    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: What is wrong with my code?

    Do you ever read the responses I make to your posts?

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    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: What is wrong with my code?

    i can't display it as in the statement.
    Please explain what you want to be different about the program's output.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Sep 2021
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my code?

    I want to print the output properly in a table format

  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: What is wrong with my code?

    What is wrong with its current output? It looks to me like it is in table format.
    What does "properly" mean? Can you Explain what is wrong with the current output.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Sep 2021
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my code?

    As you can see in the ouput, the elements in the description's column are displayed disorderedly i want them to be aligned with the description 's word.Same thing for the cost(R) columns

  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: What is wrong with my code?

    i want them to be aligned with the description 's word.
    To put all the printed output on each row in the same columns as on the other rows, you need to make the width of the data in a column the same for all the rows.
    If the data is wider than the column it will overflow and push the data for the next column to the right.
    It looks like the data width of the first column on the first row is greater than the value specified by the format string.
    Either change the width of the data to match the column width (6 characters) or increase the width of the format string. Make it large enough to hold "Service code"
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. is something wrong with my code? Lab 10.1
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 17
    Last Post: October 28th, 2013, 09:05 PM
  2. What's wrong with my code!? seriously...
    By MLIAKIRA in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 23rd, 2013, 02:21 PM
  3. What Did I Do Wrong With My Code?
    By zendorz in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 14th, 2013, 02:01 PM
  4. What is the wrong with this code?
    By Subhasri in forum AWT / Java Swing
    Replies: 1
    Last Post: September 29th, 2011, 08:14 AM
  5. what is wrong in this code
    By rk.kavuri in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 6th, 2011, 03:13 PM