Results 1 to 6 of 6
  1. #1
    random0munky is offline Member
    Join Date
    Oct 2008
    Location
    Washington, US
    Posts
    28
    Rep Power
    0

    Default Having printing trouble

    Alright I'm printing out this movie list from a txt file. Here's a little snippet of what the txt file looks like:

    Movies.txt
    Halloween,1978,R,47,93,7.7
    Hard Rain,1998,R,19.819,95,5.2
    Men in Black,1997,PG-13,250.147,98,7.4

    And here's what my output looks like so far
    Java Code:
         Title                          Year      Rate    Profit    Length     Popularity
    
     Halloween                          1978         R      47.0        93            7.7
     Hard Rain                          1998         R    19.819        95            5.2
    Men in Black                          1997     PG-13   250.147        98            7.4

    Here's my toString method in my Movies.java class

    Java Code:
    	public String toString()
    	{
    		StringBuilder sb = new StringBuilder() ;
    		sb.append(String.format("%10s%30s%10s%10s%10s%15s",
    			movieTitle, movieYear, movieRate, movieProfit, movieLength, moviePop));
    		return sb.toString() ;
    	}

    Here's my for each method in my moviesTest.java class where my main method is in:

    Java Code:
    private static void printList(List<Movie> someList)
    	{
    		for (Movie m : someList)
    		{
    			System.out.println(m);
    		}
    	}
    Now my problem is, if you notice, there's a space in front of the movies Halloween and Hard Rain where in the text file there's absolutely no whitespaces whatsoever. With this, for those two movies, the fields do not line up with the columns. I've tried implementing the .trim() method but to no avail. Have any ideas/suggestions..?
    Last edited by random0munky; 12-07-2009 at 08:06 PM.

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

    Default

    The %10s format specifier pads spaces from the left; try %-10s so the padding is done on the right. Also 10 positions for a title isn't much, many titles will be longer than 10 positions and ruin your layout if you don't take precautions (like the String.substring( ... ) method).

    kind regards,

    Jos

  3. #3
    random0munky is offline Member
    Join Date
    Oct 2008
    Location
    Washington, US
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    The %10s format specifier pads spaces from the left; try %-10s so the padding is done on the right. Also 10 positions for a title isn't much, many titles will be longer than 10 positions and ruin your layout if you don't take precautions (like the String.substring( ... ) method).

    kind regards,

    Jos
    Ah ah gotcha okay great thank you that deff solves it. Made the first number instead of a 10 changed it to 35 and good to go =)

    Here's the output now of a couple lines:

    Java Code:
    Title                                                        Year      Rate    Profit    Length     Popularity
    
    Halloween                                                    1978         R      47.0        93            7.7
    Hard Rain                                                    1998         R    19.819        95            5.2
    Men in Black                                                 1997     PG-13   250.147        98            7.4
    Well it kind of wrapped I think we'll see when its posted

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

    Default

    Quote Originally Posted by random0munky View Post
    Ah ah gotcha okay great thank you that deff solves it. Made the first number instead of a 10 changed it to 35 and good to go =)
    Always RTFM (F == Fine or Fancy or anything else starting with an F) It really helps ;-)

    kind regards,

    Jos

  5. #5
    random0munky is offline Member
    Join Date
    Oct 2008
    Location
    Washington, US
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Always RTFM (F == Fine or Fancy or anything else starting with an F) It really helps ;-)

    kind regards,

    Jos
    What does the R and T and M stand for though X_X lolz kinda lost lolz.

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

    Default

    Quote Originally Posted by random0munky View Post
    What does the R and T and M stand for though X_X lolz kinda lost lolz.
    Is Google down in your part of the world?

    kind regards,

    Jos ;-)

Similar Threads

  1. Help in Printing
    By kirly in forum Advanced Java
    Replies: 3
    Last Post: 10-03-2011, 03:40 PM
  2. Here comes trouble... :-)
    By sargehendricks in forum Introductions
    Replies: 1
    Last Post: 04-23-2009, 03:18 PM
  3. Printing Help...
    By chiragkini in forum AWT / Swing
    Replies: 1
    Last Post: 02-17-2009, 06:07 AM
  4. having some trouble
    By Unknown1369 in forum New To Java
    Replies: 13
    Last Post: 07-21-2008, 11:52 PM
  5. Printing Example
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-11-2008, 04:41 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
  •