Results 1 to 8 of 8

Thread: Not Reading \n

  1. #1
    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0

    Default Not Reading \n

    I'm trying to create a bingo card application, for my high school AP Computer Science class, and I can't get the \n command to work. I've already tried asking my teacher and my classmates and none of them can figure out what's wrong, so I figured that maybe someone on the forums could help me. Here is my code.

    Java Code:
    import java.lang.Math;
    public class BingoCard {
    
    	public static void main(String[] args) {
    		
    		System.out.format("%-1s %8s %8s %8s %8s", "B", "I", "N", "G", "O\n" );
    		System.out.format("%-1s %8s %8s %8s %8s", (int) (15 * Math.random() + 1), (int) (15 * Math.random() + 16), (int) (15 * Math.random() + 31), (int) (15 * Math.random() + 46), (int) (15 * Math.random() + 61),"\n");
    		System.out.format("%-1s %8s %8s %8s %8s", (int) (15 * Math.random() + 1), (int) (15 * Math.random() + 16), (int) (15 * Math.random() + 31), (int) (15 * Math.random() + 46), (int) (15 * Math.random() + 61),"\n");
    		System.out.format("%-1s %8s %8s %8s %8s", (int) (15 * Math.random() + 1), (int) (15 * Math.random() + 16), "FREE", (int) (15 * Math.random() + 46), (int) (15 * Math.random() + 61),"\n");
    		System.out.format("%-1s %8s %8s %8s %8s", (int) (15 * Math.random() + 1), (int) (15 * Math.random() + 16), (int) (15 * Math.random() + 31), (int) (15 * Math.random() + 46), (int) (15 * Math.random() + 61),"\n");
    		System.out.format("%-1s %8s %8s %8s %8s", (int) (15 * Math.random() + 1), (int) (15 * Math.random() + 16), (int) (15 * Math.random() + 31), (int) (15 * Math.random() + 46), (int) (15 * Math.random() + 61),"\n");
    	}
    
    }
    And here is my output.

    B I N G O
    15 20 36 49 747 18 32 51 7313 17 FREE 56 709 21 45 60 667

    For some reason I can only get the first \n to work, so if anyone can help me that would be great, thank you.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default Re: Not Reading \n

    There is no trailing \n character in the other format String, so none is printed. b.t.w. print ints with the %d format character, not the %s character and only five fields are printed (not the sixth "\n" field).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: Not Reading \n

    Why not put the \n in the format String directly instead of trying to substitute it in with a %X?
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0

    Default Re: Not Reading \n

    Quote Originally Posted by JosAH View Post
    There is no trailing \n character in the other format String, so none is printed. b.t.w. print ints with the %d format character, not the %s character and only five fields are printed (not the sixth "\n" field).

    kind regards,

    Jos
    Sorry, the \n got cut off, I have no idea why, when it didn't get cut off when I was typing it.
    Here is the rest of my code.

    Java Code:
    15 * Math.random() + 46), (int) (15 * Math.random() + 61),"\n");
    15 * Math.random() + 46), (int) (15 * Math.random() + 61),"\n");
     (int) (15 * Math.random() + 61),"\n");
    15 * Math.random() + 46), (int) (15 * Math.random() + 61),"\n");
    15 * Math.random() + 46), (int) (15 * Math.random() + 61),"\n");

  5. #5
    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0

    Default Re: Not Reading \n

    Quote Originally Posted by The Etheral Hawk View Post
    Sorry, the \n got cut off, I have no idea why, when it didn't get cut off when I was typing it.
    Here is the rest of my code.

    Java Code:
    15 * Math.random() + 46), (int) (15 * Math.random() + 61),"\n");
    15 * Math.random() + 46), (int) (15 * Math.random() + 61),"\n");
     (int) (15 * Math.random() + 61),"\n");
    15 * Math.random() + 46), (int) (15 * Math.random() + 61),"\n");
    15 * Math.random() + 46), (int) (15 * Math.random() + 61),"\n");
    Never mind, I tried Norm's advice after writing that, and it worked.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: Not Reading \n

    You've got five placeholders in the format string ("%-1s %8s %8s %8s %8s").
    You supply 6 parameters...the sixth being the new line character.
    The formatter will ignore anything after the fifth.
    Please do not ask for code as refusal often offends.

  7. #7
    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0

    Default Re: Not Reading \n

    Quote Originally Posted by Norm View Post
    Why not put the \n in the format String directly instead of trying to substitute it in with a %X?
    Thank you, that worked perfectly.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: Not Reading \n

    Well, that is what Jos said...not that the forum had cut off the code.

    And Norm's right. It should be part of the format String, since it's a constant value.
    Please do not ask for code as refusal often offends.

Similar Threads

  1. Reading XML
    By JSN1 in forum XML
    Replies: 6
    Last Post: 05-16-2012, 06:43 PM
  2. Reading log
    By Darth Blue Ray in forum New To Java
    Replies: 1
    Last Post: 03-15-2011, 09:35 PM
  3. reading from a .txt
    By Flamespewer in forum New To Java
    Replies: 1
    Last Post: 09-14-2009, 08:35 AM
  4. Replies: 5
    Last Post: 08-21-2009, 11:03 AM

Posting Permissions

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