带空格的字符串赋值返回 null
为什么当我把 //private String namespace = "###Name:"+name+" ###"; 时和 system.out.println(namespace) 它输出为空?
我想创建一个标签制作程序,但我不知道如何在右侧排列 #。
/*################################################
### ANNUAL CONFERENCE ###
############################################### #
###Name:Simon ###
### ###
############################################### #
###Organization:NBA ###
### ###
################################################*/
如何将它们与我设置的任何给定名称对齐? 我应该设置名称,设置组织,打印带有名称和组织的标签,清除名称和组织,然后打印空白标签。
----------------更新凌晨 4:09---------------------------------------- 我将在此之后对我的代码进行建模:
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);
}
}
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
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要为输出的最大宽度(以字符为单位)设置一个常量。然后您可以做一些简单的算术来计算出正确格式化输出需要多少个空格/字符等。
假设 max_width = 60。
这是一个标题:
################################################ #############
(the full 60)您根据要输出的字符串的字符长度计算出缩进和内容。
例如,如果您想打印
Name:Simon
,请获取其长度并从 max_length 中减去,等等。HTH。
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.
仔细看看 String.format(...),特别是 格式化程序文档。一开始有点复杂,但它确实允许您打印固定长度的字符串(即用空格填充)。
您可能还会发现此页面很有帮助,其中有几个有效的示例(尽管您将不得不进行一些调整和修改)。
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.