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: Extra spaces in output - why & how to fix?

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Extra spaces in output - why & how to fix?

    Hi! I'm working on a Java assignment in my beginning Java class. The assignment is to write a program using sqrt() method in the Math class and use a for loop to product the output results. My output is correct according to the chart the instructor gave. My only problem is the extra carriage returns between the output. I don't want any spaces between the output.

    This is how the assignment should look:

    Number  SquareRoot 
        0           0.0000 
        2           1.4142 
        4           2.0000 
        6           2.4495 
        8           2.8284 
       10          3.1623 
       12          3.4641 
       14          3.7417 
       16          4.0000 
       18          4.2426 
       20          4.4721

    Which part of my program is causing the extra spaces? How do I fix it?

    public class Week5Assignment {
      public static void main(String[] args) {
     
     
       // Print title of square root chart
      System.out.println("Number" + "\tSquare Root");
     
      // Print square root of even numbers 0 - 20 
      for (int x = 0; x <= 20; x++){
        if (x % 2 == 0)
        System.out.printf("  " + x + "\t   " + 
          "%.4f", x + Math.sqrt(x)); 
            System.out.println("\n");   
        }
      }
    }

    This is the part of the output from my code (I didn't post all through #20 since the length is long):
    Number  Square Root
        0           0.0000
     
     
     
        2           3.4142
     
     
     
        4           6.0000
     
     
     
        6           8.4495

    I'm wondering why I'm getting all the spaces between each line. Which part of the code is doing that?
    Last edited by KL1209; July 7th, 2012 at 10:32 PM. Reason: Add [code] and more detail to assignment.


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Extra spaces in output - why & how to fix?

    My output is correct according to the chart the instructor gave. My only problem is the extra carriage returns between the output.
    Correct, but not correct? So what is the desired output, and what is the output you observe? It would be a good idea to describe the desired output precisely: ie, indicate in some way whether the horizontal whitespace in the instructor's chart is made up of space characters or a tab character.

    -----

    printf() format strings are powerful and flexible. Perhaps you could consider altering the width of the numerals you display rather than using the " " and "\t " strings. Also format strings can use %n which produces a newline in a way that looks the same (same vertical space) on any operating system.

    Without knowing the output you are supposed to get its impossible to say for sure.

  3. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Extra spaces in output - why & how to fix?

    And please edit your post so the code is displayed properly. Put [code] at the start of the code and [/code] at the end. The idea is to preserve formatting. See the faq.

  4. #4
    Junior Member
    Join Date
    Jul 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Extra spaces in output - why & how to fix?

    Well, I don't know how to get the formatting correct on my output sample. The '0, 2, 4, etc.' should be centered under 'Number' and '0.0000, etc.' centered under Square Root'.

  5. #5
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Extra spaces in output - why & how to fix?

    Well, I don't know how to get the formatting correct on my output sample.
    The code tags should also do that. (It's a bit of an abuse of markup, but that's the way people commonly do it.)

    The '0, 2, 4, etc.' should be centered under 'Number' and '0.0000, etc.' centered under Square Root'.
    Here we have a bit of a problem because "Number" has six characters, so you can't exactly centre "0" beneath "Number". Ditto the 6 character numerals under "Square Root".

    Did you understand what I meant by describing whether there really were tab characters in the output you were supposed generate? A string like "Number"+"\tSquare Root" could end up printed like

    Number Square Root

    or

    Number         Square Root

    depending on how tab characters are rendered. If the,re is some clear indication that you are supposed to use tab characters, fair enough. But, otherwise, they are better avoided imho.

    -----

    Sorry to be fussy. But formatting output is a fussy business.
    Last edited by pbrockway2; July 7th, 2012 at 10:10 PM. Reason: corrected problem with own petard

  6. #6
    Junior Member
    Join Date
    Jul 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Extra spaces in output - why & how to fix?

    Forget it. Can't win with the formatting. -- Marking this as solved.

  7. #7
    Junior Member
    Join Date
    Jul 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Extra spaces in output - why & how to fix?

    Is there a way to delete this thread? Do I need to request a mod to delete it? I can't get the formatting correct for people to be able to help.
    Last edited by KL1209; July 7th, 2012 at 10:39 PM. Reason: spelling

  8. #8
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Extra spaces in output - why & how to fix?

    You can request it, I guess. But there are other things in the thread that might prove valuable to others: the advice to be precise - really precise - about format before attempting to implement that format in code, and the advice to use the newline format and the width specifiers in preference to tab characters.

    The (amended) original post doesn't show the output lining up at all. And it is still not clear whether the output ought to include tab characters.

    -----

    Here's an example of a table whose format is implemented in code using those suggestions. It shows radius, the square of the radius, and the area of a circle (to 3dp). The intention is for the numerals to be aligned at the units place, be centred (or one character left of centre) in their column, and for there to be two characters between adjacent columns.

    public class FormatExample {
        public static void main(String[] args) {
            System.out.println("Radius  Square  Area (pi r^2)");
            System.out.println("------  ------  -------------");
     
            for(int r = 0; r < 30; r += 2) {
                System.out.printf("  %2d    %4d      %8.3f%n", r, r * r, Math.PI * r * r);
            }
        }
    }

    The output is:

    Radius  Square  Area (pi r^2)
    ------  ------  -------------
       0       0         0.000
       2       4        12.566
       4      16        50.265
       6      36       113.097
       8      64       201.062
      10     100       314.159
      12     144       452.389
      14     196       615.752
      16     256       804.248
      18     324      1017.876
      20     400      1256.637
      22     484      1520.531
      24     576      1809.557
      26     676      2123.717
      28     784      2463.009
    Last edited by pbrockway2; July 7th, 2012 at 11:48 PM.

Similar Threads

  1. reading from text file produces extra s p a c e s, even on numbers 1 0 1
    By Spidey1980 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: September 3rd, 2011, 09:18 PM
  2. Double to string giving extra values
    By sinsand in forum Collections and Generics
    Replies: 1
    Last Post: August 23rd, 2011, 05:57 AM
  3. Cannot find where extra brace is at causing my errors
    By eagle09 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: June 27th, 2011, 07:30 PM
  4. Area of a triangle (using 2 extra methods) error help
    By SilentPirate in forum What's Wrong With My Code?
    Replies: 9
    Last Post: September 12th, 2010, 06:08 PM
  5. [SOLVED] How to Put Spaces in the Output of Passed Arguments?
    By EmSaint in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 25th, 2010, 04:04 PM