[英]Why is my method undefined for type java.lang.String
I have a Parser class which takes in a List of strings, and prints them out line by line.我有一个 Parser 类,它接收一个字符串列表,并逐行打印出来。 I want the method
linecount()
(which does not take in any parameter) to output the number of lines in total ie:我希望方法
linecount()
(它不接受任何参数)输出总行数,即:
import java.util.*;
public class Parser{
private static List<String> lines;
public static String parse(List<String> lines){
return String.join(System.lineSeparator(), lines);
}
public static int linecount(){
String newString = String.join(System.lineSeparator(), lines);
return (newString.split("[\r\n]")).length;
}
}
Such that when the List of string is given as: List lines = Arrays.asList(new String[]{"one", "two three", ""})
, calling Parser.parse(lines)
will give you这样当字符串列表给出为:
List lines = Arrays.asList(new String[]{"one", "two three", ""})
Parser.parse(lines)
List lines = Arrays.asList(new String[]{"one", "two three", ""})
,调用Parser.parse(lines)
会给你
one
two three
and thus, Parser.parse(lines).linecount()
should output 3
.因此,
Parser.parse(lines).linecount()
应该输出3
。 However, I came across the error The method linecount() is undefined for the type java.lang.String
.但是,我遇到了错误
The method linecount() is undefined for the type java.lang.String
。 Why is this so?为什么会这样?
Parser.parse(lines).linecount();//<--------- Here is the problem
Parser.parse()
return a String
and linecount()
is the static function of Parser
class. Parser.parse()
返回一个String
并且linecount()
是Parser
类的静态函数。 Its not the function of String
class.它不是
String
类的功能。 you cannot call in that way Parser.parse(lines).linecount()
and thats why it gives a error linecount() is undefined for the type java.lang.String
.你不能以这种方式调用
Parser.parse(lines).linecount()
,这就是为什么它给出错误linecount() is undefined for the type java.lang.String
。 you have to call separately.你必须单独打电话。
Parser.parse(lines);
Parser.linecount();
And you have to popultae List<String> lines
Object before calling linecount()
.并且您必须在调用
linecount()
之前 popultae List<String> lines
Object 。
Your Regex is not Right also.你的正则表达式也不对。 Replace
"[\\r\\n]"
with "[\\\\r | \\\\n]"
.将
"[\\r\\n]"
替换为"[\\\\r | \\\\n]"
。 Output of program is stated at the end.程序的输出在最后说明。
import java.util.Arrays;
import java.util.List;
public class Parser {
private static List<String> lines;
public static String parse(List<String> lines) {
return String.join(System.lineSeparator(), lines);
}
public static int linecount() {
String newString = String.join(System.lineSeparator(), lines);
return (newString.split("[\\r | \\n]")).length;
}
public static void main(String[] args) {
lines = Arrays.asList("one", "two three", "");
lines.forEach(System.out::println);
int count = Parser.linecount();
System.out.println(count);
}
}
Output :输出 :
one
two three
3
import java.util.*;
public class Parser{
private List<String> lines;
public String parse(List<String> lines){
this.lines = lines;
return String.join(System.lineSeparator(), lines);
}
public int linecount(){
String newString = String.join(System.lineSeparator(), lines);
return (newString.split("[\r\n]")).length;
}
}
main()主要的()
Parser p = new Parse();
List lines = p.parse(Arrays.asList(new String[]{"one", "two three", ""}));
System.out.println(lines);
System.out.println(p.linecount());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.