Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Java Problem

  • 27-03-2008 01:30PM
    #1
    Closed Accounts Posts: 1


    I have a print statement with surname, forename and number as Strings
    System.out.println(surname + ", " + forename + "\t" + number);
    
    But I need to get into two lef aligned columns one with suname and forename and the other with number. I'm not quite sure how printf works. Could anyone help?


Comments

  • Closed Accounts Posts: 81 ✭✭dzy


    debo wrote: »
    I have a print statement with surname, forename and number as Strings
    System.out.println(surname + ", " + forename + "\t" + number);
    
    But I need to get into two lef aligned columns one with suname and forename and the other with number. I'm not quite sure how printf works. Could anyone help?

    I don't understand. Isn't this exactly what your code is doing?
    class PersonLister
    {
        public static void main(String[] args)
        {
            String forename = "Joe";
            String surname = "Soap";
            int number = 1;
    
            for (int i = 0; i < 10; i++)
            {
                System.out.println(surname + ", " + forename + "\t" + number);
            }
        }    
    }
    


  • Registered Users, Registered Users 2 Posts: 26,449 ✭✭✭✭Creamy Goodness


    to do a line like this

    System.out.println(surname + ", " + forename + "\t" + number);

    i think you need to override the toString() method for your class as number is of type int and you're concatinating it on to a string (my java is really rusty).


  • Closed Accounts Posts: 81 ✭✭dzy


    Cremo wrote: »
    to do a line like this

    System.out.println(surname + ", " + forename + "\t" + number);

    i think you need to override the toString() method for your class as number is of type int and you're concatinating it on to a string (my java is really rusty).

    Java's clever like that and will automatically convert the integer to a String when concatenating.


  • Registered Users, Registered Users 2 Posts: 1,922 ✭✭✭fergalr


    I think he's asking a question about printf from reading his post. Not println.


    If you want to do alignment in a big way (more than you already have in your original post), you probably do want to be using printf... best just look up the docs with it and learn how it works.
    It's a bit tricky to get used to place holders and stuff when you've only used println, but spend a little time and it becomes clear...

    Edit:

    At least, that's what I'm assuming based on
    I'm not quite sure how printf works.

    You probably want to read this stuff:
    http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html#syntax
    or maybe look for an introduction to C printf as that might be a bit more comprehensible...

    http://java.sun.com/j2se/1.5.0/docs/api/java/io/PrintStream.html#printf(java.util.Locale,%20java.lang.String,%20java.lang.Object...)


  • Registered Users, Registered Users 2 Posts: 1,922 ✭✭✭fergalr


    Here, you're looking for something like this:

    PrintStream p = new PrintStream(System.out);
    p.printf("%-20s%-20s%-20d","firstname","lastname",10);

    or maybe:

    PrintStream p = new PrintStream(System.out);

    String firstname = "Dave";
    String lastname = "Johnson";
    int age = 25;
    String firstColString = lastname + "," + firstname;

    p.printf("%-20s%-20d",firstColString,age);


    To answer your question more exactly..?


  • Advertisement
Advertisement