我想在Java中创建一个名为URLTransformer
的类。
假设我有一个博客条目,并希望格式化我的文本(例如因为某些单词是瑞典语)。
这是我现在的代码:
public class URLTransformer {
public static void main(String[] args) {
String basurl = "http://www.mybloggsys.com/user_abc/";
String[] rubriker = {
"Nu Kommer Vintern till aland",
"Onskningar ar nagot man vill uppna",
"Just English Letters"
};
for (String rubrik : rubriker) {
String rubrik2=rubrik.toLowerCase().replace(" ", "_").replace("Å", "a").replace("å", "a").replace("ä", "a").replace("Ö", "o");
URLTransformer transformer = new URLTransformer();
URLTransformer(basurl, rubrik2);
String url = transformer.getURL();
String aElement = transformer.getAElement();
System.out.println(rubrik2);
System.out.println(url);
System.out.println(aElement);
}
URLTransformer transformer = new URLTransformer();
}
private String getAElement() {
// TODO Auto-generated method stub
return null;
}
private String getURL() {
// TODO Auto-generated method stub
return null;
}
private static void URLTransformer(String basurl, String rubrik2) {
// TODO Auto-generated method stub
}
}
现在,我可以让Eclipse执行此代码,但我收到了很多“空”消息。是否有可能在编译我的代码后无法获得它或甚至是否重要?在我的家庭作业任务中,最终的代码看起来应该是这样的:
Nu kommer Vinter till Åland
http://www.mybloggsys.com/user_abc/nu_kommer_vintern_till_aland
<a href="(url link same as above)/nu_kommer_vintern_till_aland">Nu Kommer Vintern till Åland</a>
Önskningar är något man vill uppnå
(url link)/onskningar_ar_nagot_man_vill_uppna
<a href="(url link)/onskningar_ar_nagot_man_vill_uppna">Önskningar är något man vill uppnå</a>
Just English Letters
(url link)/just_english_letters
<a href="(url link)/just_english_letters">Just English Letters</a>
任何帮助都会非常感激,感觉它几乎只能处理这个</a>
元素和剩下的空消息。
答案 0 :(得分:0)
以下是您的代码:
public class URLTransformer {
private String baseurl;
private String baseString;
// Constructor of the class that uses a base url and a base string
public URLTransformer(String baseurl, String baseString) {
super();
this.baseurl = baseurl;
this.baseString = baseString;
}
// Method that return the href calculated
private String getHref() {
// transform the string to remove non english characters with _ instead of spaces
return baseurl + baseString.toLowerCase().replace(" ", "_").replace("Å", "a").replace("å", "a").replace("ä", "a").replace("Ö", "o");
}
// return a <a> link with a call to the href link method
private String getAElement() {
StringBuilder aElement = new StringBuilder();
aElement.append("<a href=\"");
aElement.append(getHref());
aElement.append("\">");
aElement.append(baseString);
aElement.append("</a>");
return aElement.toString();
}
public static void main(String[] args) {
String basurl = "http://www.mybloggsys.com/user_abc/";
String[] rubriker = { "Nu Kommer Vintern till aland", "Onskningar ar nagot man vill uppna", "Just English Letters" };
// iterate over the
for (String rubrik : rubriker) {
URLTransformer transformer = new URLTransformer(basurl, rubrik); // creation of the class object that is able to make a link from the string
System.out.println(rubrik); // prints the provided string
System.out.println(transformer.getHref()); // use of the getHref method to get an url from the string
System.out.println(transformer.getAElement()); // display of the <a> element
}
}
}
注意:我已在代码中添加了注释,并在下面看到我的评论,以解释所提供代码的错误。