how to align things

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kenvin100
    New Member
    • Oct 2006
    • 24

    #1

    how to align things

    hi im new here and to java..im supposed to output a receipt that aligns the prices to the right. Since im new, i only know the basics. I was given a clue on how to do this:

    Strin someString = "Hello there, kenny.";
    int numChars = someString.leng th();

    so basically i need to find a way to align prices to the right, keeping in mind the length of the item and and length of the price..any ideas will be most appreciated
  • kenvin100
    New Member
    • Oct 2006
    • 24

    #2
    oh yeah..so basically wat i learned so far are if statements and loops (and delcaring stuff of course)

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by kenvin100
      oh yeah..so basically wat i learned so far are if statements and loops (and delcaring stuff of course)
      Assuming you want to right align things on the console, you can play around with the length of the strings as follows.

      Suppose you want the prices to appear at 60 spaces from the left of the console

      For each item, you get the length of the item's name using the method you were give above.

      int l = item.length();
      Then you get how many spaces you need to add using
      int spaces = 60 - l;

      So when printing item item1 and its price, You print the item name, then you print spaces spaces, then you print the item's price and go to the next line. If you do this for all the items, you will always get the prices aligned 60 spaces form the left of the screen assuming the names of the items are not more than 60 characters long

      Comment

      • kenvin100
        New Member
        • Oct 2006
        • 24

        #4
        [QUOTE=r035198x]Assuming you want to right align things on the console, you can play around with the length of the strings as follows.

        so for the output, wat do i put exactly..s.o.pl n (item + space)

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          [QUOTE=kenvin100]
          Originally posted by r035198x
          Assuming you want to right align things on the console, you can play around with the length of the strings as follows.

          so for the output, wat do i put exactly..s.o.pl n (item + space)
          Code:
          String space = "";
          for(int i = 0 ;i < spaces; i++) {
               space = space + " ";
          }
          System.out.println(item + space + price);

          Comment

          • kenvin100
            New Member
            • Oct 2006
            • 24

            #6
            k thx for your help..gonna try it now

            Comment

            • kenvin100
              New Member
              • Oct 2006
              • 24

              #7
              u kno where u put space as the output, am i supposed to put a number there or space..cause i declared space and it doesn't work

              Comment

              • kenvin100
                New Member
                • Oct 2006
                • 24

                #8
                my code doesnt seem to work...let me show u..


                String out = "";
                int space = 17-1;
                for(int i = 0 ;i < 17; i++) {
                out = " ";
                }

                is this rite?

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by kenvin100
                  u kno where u put space as the output, am i supposed to put a number there or space..cause i declared space and it doesn't work
                  what does the error say

                  Comment

                  • kenvin100
                    New Member
                    • Oct 2006
                    • 24

                    #10
                    there's no error it's just that its not spacing..

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Code:
                      String item = "something";
                      double price = 20.0;
                      int spaces = 50 - item.length();
                      
                      String space = ""; //declare the variable space
                      for(int i = 0 ;i < spaces; i++) {
                           space = space + " "; //
                      }
                      System.out.println(item + space + price);

                      Comment

                      • kenvin100
                        New Member
                        • Oct 2006
                        • 24

                        #12
                        + if i declare a space as 17-1, the output shows 17, i kno its an int but there should be a way to solve this..

                        Comment

                        • kenvin100
                          New Member
                          • Oct 2006
                          • 24

                          #13
                          and "space" and "spaces" are two different things..

                          Comment

                          • kenvin100
                            New Member
                            • Oct 2006
                            • 24

                            #14
                            it works a little but there's still some problem..appara ntly items with larger names arent jusitifed..subt raction sign is space

                            item---------------------------- quantity@price. ..
                            item------------------------------------------------------------quantity@price. ..

                            see the problem?

                            Comment

                            • kenvin100
                              New Member
                              • Oct 2006
                              • 24

                              #15
                              of course the second name is longer..

                              Comment

                              Working...