1

All I would like to know what's wrong with this is why do things get pushed to the right when there is 3 numbers a decimal point and a decimal. Why isn't this being aligned? I've had this problem with other code, even copied someones code that should've displayed decimal points aligning in a column but it always doesn't align. Please ignore everything above "_________________________________" line, I know it's not aligned with that, i just want to know why it's doing what it's doing below that line. (why mis aligning everything by pushing top 3 rows to the right)

Book Answer's Console Result in MY eclipse console

watf

What the actual answer is supposed to look like according to book answer result

watg

Books Answer Code

package Ch5_Methods;
import java.util.Scanner;
public class testch5 {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    System.out.printf("%-15s%-15s|    %-15s%-15s\n","Celsius","Fahrenheit","Fahrenheit","Celsius");
      System.out.println( String.format("%62s"," ").replace(' ', '-') );

      for (int c = 40, f = 120  ; c >=31; c--, f-=10) {

       System.out.printf("%-15.1f%-15.1f|    %-15.1f%-15.2f\n",(float)c,celsiusToFahrenheit(c),(float)f, fahrenheitToCelsius(f));

      }


     }

     /** Convert from Celsius to Fahrenheit */
     public static double celsiusToFahrenheit(double celsius) {
      return (9.0 / 5) * celsius + 32;
     }

     /** Convert from Fahrenheit to Celsius */
     public static double fahrenheitToCelsius(double fahrenheit) {
      return (5.0 / 9) * (fahrenheit - 32);

    }




}

I cannot figure out how to align the code, I've tried formatting different ways with printf, I'm still a beginner and i'm sure there is an easier way to do this, but i'm trying to accomplish this having only covered methods, loops, if/else, formatting and other basic stuff.

2
  • If you want to ask about output and alignment, make sure your question shows your intent. Please edit your question and improve it. Commented Feb 17, 2016 at 20:20
  • 3
    You can make your code output text with the correct amount of space to align things, but it won't look right if your console is using a variable width font. Switch your console to a monospaced font. Commented Feb 17, 2016 at 20:21

1 Answer 1

1

You can see that periods take up less space than the other characters. You need to switch to a mono spaced font. Eclipse has some documentation for how to do that here.

You can see here that it works with a mono spaced font.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I really appreciate the link, this solved my problem, It wasn't anything wrong with the code it was just different fonts. I switched to Courier font, I was on sequoia font.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.