2011-03-02 149 views
0

当我把// private String namespace =“### Name:”+ name +“###”;和system.out.println(命名空间)它出来的空?字符串赋值,空格返回null

我想创建一个tagmaker程序,但我坚持如何排列#在右侧。

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

如何将它们排列成任何给定的名称,我将其设置为? 我想设置名称,设置组织,打印标签的名称和组织,清除名称和组织,并打印一个空白标签。

---------------- UPDATE 4:09 AM -------------------------- - 我只想在这之后我的代码模型:

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)); 
    } 
    } 

,但有人可以告诉我怎么我能做到这一点的其他方式?仅用于学习目的

public 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); 

    } 
    } 
+0

你使用标签或空格吗? – 2011-03-02 11:51:33

+4

提示:http://download.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#length(%29 – 2011-03-02 11:52:27

+0

我正在使用空格 – CuriousStudent 2011-03-02 12:03:04

回答

1

您需要为输出的最大宽度(以字符为单位)设置一个常量。然后你可以做一些简单的算术来计算出你需要多少空格/字符来正确地格式化你的输出。

说,如果MAX_WIDTH = 60

这里有一个标题: ############################################################(全60)

在你的字符串,你的字符制定出基于长度缩进和其他东西。重新输出。

例如如果你想打印Name: Simon得到这个长度,并从max_length等减去等。

HTH。

+0

我如何按照您所描述的方式进行操作?在这之后我想修改我的代码 public class Divers {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(格式,“John”,“D”,“Doe”); String ex [] = {“John”,“F.”,“Kennedy”}; System.out.format(String.format(format,(Object [])ex)); } } – CuriousStudent 2011-03-02 12:08:25

+0

如果你不介意,你能告诉我怎么做到这一点吗?我将修改我的代码到.format选项,但为了学习的目的,我想要学习你描述的方式。谢谢 – CuriousStudent 2011-03-02 12:13:33

+0

如果有人在StackOverflow上为你写代码,你不会从这个实验练习中学到很多东西;-) 想想我的和@ DerMike的建议,并*尝试*。你有你需要的所有基本信息。 提示:使用if/else之类的低级构造,并从循环中获得锻炼的最大好处。 祝你好运。 – 5arx 2011-03-02 12:18:05

1

看看String.format(...),特别是Formatter documentation。首先,它有点复杂,但它确实允许您打印固定长度的字符串(即用空格填充)。

您可能还有find this页面有帮助,它有几个工作示例(尽管您必须修改和修改一些公平的位)。

+0

谢谢,我将在此之后为我的代码建模。 – CuriousStudent 2011-03-02 12:13:57

0

首先你需要找出你的线条有多长。其次,确定徽章内的文字长度。最后你动态地插入空格,使其具有相同长度的行。

+0

如果你不介意,你能告诉我如何做到这一点?我将修改我的代码到.format选项,但为了学习的目的,我想要学习你描述的方式。谢谢 – CuriousStudent 2011-03-02 12:12:55