简体   繁体   English

为什么我的方法未定义为 java.lang.String 类型

[英]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.

相关问题 错误:类型java.lang.Object的方法add(java.lang.String)未定义 - Error: The method add(java.lang.String) is undefined for the type java.lang.Object Java &amp;&amp;错误“未为参数类型java.lang.String,java.lang.String定义运算符&amp;&amp;” - Java && Error “The operator && is undefined for the argument type(s) java.lang.String, java.lang.String” 运算符|| 在Android应用中未为参数类型java.lang.String,java.lang.String定义 - The operator || is undefined for the argument type(s) java.lang.String, java.lang.String in Android app 类型 java.lang.String 中没有由 @DynamoDBHashKey 注释的方法或字段 - No method or field annotated by @DynamoDBHashKey within type java.lang.String 构造函数 String(java.lang.String) 未定义 - The constructor String(java.lang.String) is undefined SpelEvaluationException:EL1004E:方法调用:在java.lang.String类型上找不到方法getValue(java.lang.String) - SpelEvaluationException: EL1004E: Method call: Method getValue(java.lang.String) cannot be found on java.lang.String type How to solve this error “cannot resolve method 'addData(java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String)'”? - How to solve this error “cannot resolve method 'addData(java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String)'”? Spring JPAF无法将值“为什么?”从类型[java.lang.String]转换为类型[java.lang.Long]。 - Spring JPAFailed to convert from type [java.lang.String] to type [java.lang.Long] for value 'Why?' foreach 不适用于类型“java.lang.String” - foreach not applicable to type 'java.lang.String' 没有适合的方法read(java.lang.String) - no suitable method for read(java.lang.String)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM