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

Thread: How to align text? Printf?

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    My Mood
    Depressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question How to align text? Printf?

    Ok,

    I have a CS assignment due tomorrow and I am missing one of the requirements for the project.
    I have to write a program that asks a student for a list of information and then generates a report.
    It should display something like this:
    image.jpg

    So, I finished writing the code. But, I was not able to align the text properly.

    Here's a part of my code that I want to align (the whole thing is huge):

    System.out.print (subj1 + "\t"); //Subject 1
     
    System.out.printf ("%03d",gradesubj1); // Mark for Subject 1
     
    System.out.println (subj2 + "\t" + gradesubj2 + "%"); // Subject and Mark for Subject 2
     
    System.out.println (subj3 + "\t" + gradesubj3 + "%"); // Subject and Mark for Subject 3
     
     System.out.println (subj4 + "\t" + gradesubj4 + "%"); // Subject and Mark for Subject 4   
     
    System.out.println ("\nAverage : " + gradeavg + "%"); // Grades Average         
     
    System.out.println ("\nTotal money earned : $" + workmoneytotal); 
     
    System.out.println ("Number of affordable tutoring hours: " + tutoringhours_int + " hours");

    As you can see, I tried to align with System.out.printf ("%03d"....);
    The program compiles correctly. But when I run it, I get this:

    (part of the output)

    java.util.IllegalFormatConversionException: d != java.lang.Double
    	at java.util.Formatter$FormatSpecifier.failConversion(Unknown Source)
    	at java.util.Formatter$FormatSpecifier.printInteger(Unknown Source)
    	at java.util.Formatter$FormatSpecifier.print(Unknown Source)
    	at java.util.Formatter.format(Unknown Source)
    	at java.io.PrintStream.format(Unknown Source)
    	at java.io.PrintStream.printf(Unknown Source)
    	at Budget.main(Budget.java:114)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    	at java.lang.reflect.Method.invoke(Unknown Source)
    	at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:271)

    What's wrong?

    Thanks in advance


  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: How to align text? Printf?

    IllegalFormatConversionException: d != java.lang.Double
    Look up the definitions for the formatting control letters.
    The program is telling you that d is not for doubles.

Similar Threads

  1. How to align text?
    By shifat96 in forum Java Theory & Questions
    Replies: 4
    Last Post: February 20th, 2012, 12:15 PM
  2. Can't seem to align the numbers
    By Wilkinsonr in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 29th, 2012, 05:37 PM
  3. How do you align?
    By JavaStudent1988 in forum Java Theory & Questions
    Replies: 5
    Last Post: October 18th, 2011, 05:33 PM
  4. about printf please help having errors
    By Macgrubber in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2010, 11:01 PM
  5. The printf() method explanation needed
    By darek9576 in forum Object Oriented Programming
    Replies: 1
    Last Post: March 14th, 2010, 12:11 AM