开发者

String assignment with spaces returning null

How come when i put //private String namespace = "###Name:"+name+" ###"; and system.out.println(namespace) it comes out null?

I am suppose to create a tagmaker program, but I am stuck at how to line up the # on the right side.

/*################################################
###               ANNUAL CONFERENCE            ###
############################################### #
###Name:Simon                             ###
###                                           ###
############################################### #
###Organization:NBA             ###
###                                             ###
################################################*/

How do I line them up for any given name that I set it to? I am suppose to Set the Name, Set the organization, Print tag with the name and organization, Clear the name and organization, and Print a blank tag.

----------------UPDATE 4:09AM---------------------------- I am just going to model my code after this:

public class Divers {
    public static void main(String args[]){
      String format = "|%1$-10s|%2$-10s|%3$-20s|\n";
      System.out.format(format, "FirstName", "Init.", "LastName");
      System.out.format(format, "Real", "", "Gagnon");
      System.out.format(format, "John", "D", "Doe");

      String ex[] = { "John", "F.", "Kennedy" };

      System.out.format(String.format(format, (Object[])ex));
    }
   }

but can someone show me how i can do it the other way? just for learning purposes

p开发者_JAVA技巧ublic class AddressBookTester {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
TagMaker test =  new TagMaker("Simon", "NBA");

test.printlL();

    }

}

public class TagMaker {

private String name;
private String organization;
private String l = "################################################";
private String annual= "###               ANNUAL CONFERENCE            ###";
//private String namespace = "###Name:"+name+"                ###";
private String l2= "###                                             ###";
//private String organizationspace= "###Organization:"+organization+"###";
TagMaker (){

}

TagMaker (String tempName, String tempOrg){
name = tempName;
organization = tempOrg;

}
    void setname(String tempName){
        name = tempName;
    }

    String getname(){
        return name;
    }

    void setorganization(String tempOrg)
    {
            organization = tempOrg;
    }

    String getorganization(){
        return organization;
    }


    void printlL(){
        System.out.println(l);
        System.out.println(annual);
        System.out.println(l);
        System.out.println("###Name:"+name+"                             ###");
        System.out.println(l2);
        System.out.println(l);
        System.out.println("###Organization:"+organization+"             ###");
        System.out.println(l2);
        System.out.println(l);

    }
    }


You need to set a constant for the Maximum width (in characters) of your output. Then you can do some simple arithmetic to work out how many spaces/characters etc. you need to properly format your output.

Say if max_width = 60.

Here's a header: ############################################################ (the full 60)

The you work out the indents and things based on the length in chars of the strings you're outputting.

e.g. If you want to print Name: Simon get the length of this and subtract from max_length, etc. etc.

HTH.


Have a good look at String.format(...), particularly the Formatter documentation. It's a bit complex to start with, but it does allow you to print strings with a fixed length (i.e. padded out with spaces).

You might also find this page helpful, which has a couple of worked examples (although you will have to adapt and modify a fair bit).


First you need to find out how long your lines are. Second you determine the length of the text inside the badge. Finally you insert the spaces dynamically to have of lines with equal lengths.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜