Results 1 to 6 of 6
  1. #1
    GxS Nero is offline Member
    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0

    Default Help please D: Doesn't print out what I type in

    So basically I typed in this:

    public class AboutMeP2 {
    public static void main (String[] args) {
    System.out.format("%-10s %10s %10s %10s %10", "Period", "Course", "Time", "Teacher", "Room\n");
    System.out.format("%-10s %10s %10s %10s %10", "1", "Computer Science", "8:20a.m-9:35a.m", "Mr. Lee", "2044\n");
    System.out.format("%-10s %10s %10s %10s %10s", "2", "Accounting", "9:40a.m-10:55a.m", "Ms. Parujanwala", "2007\n");
    System.out.format("%-10s %10s %10s %10s %10s", "3", "Lunch", "11:00a.m-12:15p.m", "-", "-\n");
    System.out.format("%-10s %10s %10s %10s %10s", "4", "Biology", "12:20p.m-1:35pm", "Mr. Henderson", "2055\n");
    System.out.format("%-10s %10s %10s %10s %10s", "5", "Physics", "1:40p.m-2:55p.m", "Mr. Uppal", "2060\n");
    }
    }

    and it gave me this:

    java.util.UnknownFormatConversionException: Conversion = '1'
    at java.util.Formatter.checkText(Unknown Source)
    at java.util.Formatter.parse(Unknown Source)
    at java.util.Formatter.format(Unknown Source)
    at java.io.PrintStream.format(Unknown Source)
    at AboutMeP2.main(AboutMeP2.java:9)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.ru nCommand(JavacCompiler.java:272)

    What happened? ):

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Help please D: Doesn't print out what I type in

    Quote Originally Posted by GxS Nero View Post
    What happened? ):
    It's a copy and paste error: the first two format strings are not like the others.

  3. #3
    GxS Nero is offline Member
    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0

    Default Re: Help please D: Doesn't print out what I type in

    OHHHH loool fail >_< i feel so stupid now xD thank youu <3 it works now haha
    oh but uh how do you make it look more like a table? like right now its now aligned (my explaination probably sucks so i'll just show you xD )

    Period Course Time Teacher Room
    1 Computer Science 8:20a.m-9:35a.m Mr. Lee 2044
    2 Accounting 9:40a.m-10:55a.m Ms. Parujanwala 2007
    3 Lunch 11:00a.m-12:15p.m - -
    4 Biology 12:20p.m-1:35pm Mr. Henderson 2055
    5 Physics 1:40p.m-2:55p.m Mr. Uppal 2060

    so how do i get it to be more organized?

  4. #4
    GxS Nero is offline Member
    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0

    Default Re: Help please D: Doesn't print out what I type in

    oh nvm i got it (:

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Help please D: Doesn't print out what I type in

    You're welcome. In case anyone else happens on the thread, it pays to experiment with %10s and %-10s to see if aligning helps get the desired layout.

    Also, since I'm posting, you have the new line character as part of the room name whereas it might seem more natural to have the new line as part of the format string.

    Java Code:
    public class Foo {
        public static void main(String[] args) throws Exception {
            String fmt = "%-8s %-10s %-17s %-10s %-10s%n";
            System.out.printf(fmt, "Period", "Course", "Time", "Teacher", "Room");
            System.out.printf(fmt, "5", "Physics", "1:40p.m-2:55p.m", "Mr. Uppal", "2060");
            // etc
        }
    }
    Notice how there is a %n formatting symbol results in the "right" new line characters for whatever system you are on. (And not everything has to be a string. Perhaps %d would make more sense for the period.)

    A couple of personal preferences. Since the format string is reused I'd make a separate variable for it: this is "self documentation" that makes it clear I intend to format each line the same way. (It also stops your original error from ever happening in the first place!) And I use printf() in place of the (equivalent?) format(). In fact until I read your post more closely I hadn't realised there was a format() method in this class - so thanks for that!

  6. #6
    GxS Nero is offline Member
    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0

    Default Re: Help please D: Doesn't print out what I type in

    Ohh thanks! I didn't know you could do String fmt = "...";! (Lol I just started learning Java :P so I don't know a lot) Thanks so much for everything. (: I swear I learned more from you than my teacher. >_> Glad we both learned something from each other. :D

Similar Threads

  1. Replies: 10
    Last Post: 06-26-2011, 02:07 AM
  2. Replies: 1
    Last Post: 02-26-2011, 09:03 PM
  3. why constructor doesn't have any return type?
    By kaka in forum New To Java
    Replies: 1
    Last Post: 09-30-2010, 09:02 PM
  4. Why doesn't this work? (multi-type array)
    By 711groove in forum New To Java
    Replies: 2
    Last Post: 12-18-2009, 10:56 PM
  5. println doesn't print from inside for loop, et.al.
    By rdtindsm in forum New To Java
    Replies: 5
    Last Post: 03-27-2009, 02:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •