Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java Problem

  • 27-03-2008 1: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,584 ✭✭✭✭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