遇到使用 join() 方法时出现“无法解析方法 ‘join’ 在 ‘String’ 中”的错误。

huangapple go评论72阅读模式
英文:

Getting the error Cannot resolve method 'join' in 'String' when using the join() method

问题

以下是要翻译的代码部分:

public class revision_testing extends AppCompatActivity {

    start_timetable start_timetable = new start_timetable();
    revision_time revision_time = new revision_time();
    public int append_counter = 0;
    public int revision_days = 25; //FOR TEST
    String[] all_dates = new String[revision_days];

    String date=start_timetable.clicked_date;

    public int day=start_timetable.day;
    public int month=start_timetable.month;
    public int year=start_timetable.year;
    int [] days_in_months = {31,28,31,30,31,30,31,31,30,31,30,31};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_revision_testing);
        getSupportActionBar().hide();

        for(int i=revision_days;i>0;i--){
            if (day >0){
                all_dates[append_counter] = year+"/"+month+"/"+day;
                day--;
                append_counter++;

            }else {
                day=days_in_months[month];
                month++;
                all_dates[append_counter] = year+"/"+month+"/"+day;
                day--;
                append_counter++;
            }
        }

        all_dates= all_dates.join("\n",all_dates);     //This line is the issue 
    }
}

希望这对你有所帮助。

英文:

Essentially all I want to do is put each array element on a new line but when using the join() method I get the error, Cannot resolve method 'join' in 'String'.

public class revision_testing extends AppCompatActivity {
start_timetable start_timetable = new start_timetable();
revision_time revision_time = new revision_time();
public int append_counter = 0;
public int revision_days = 25; //FOR TEST
String[] all_dates = new String[revision_days];
String date=start_timetable.clicked_date;
public int day=start_timetable.day;
public int month=start_timetable.month;
public int year=start_timetable.year;
int [] days_in_months = {31,28,31,30,31,30,31,31,30,31,30,31};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_revision_testing);
getSupportActionBar().hide();
for(int i=revision_days;i>0;i--){
if (day >0){
all_dates[append_counter] = year+"/"+month+"/"+day;
day--;
append_counter++;
}else {
day=days_in_months[month];
month++;
all_dates[append_counter] = year+"/"+month+"/"+day;
day--;
append_counter++;
}
}
all_dates= all_dates.join("\n",all_dates);     //This line is the issue 
}

答案1

得分: 2

很可能您想使用 String.join 方法,而不是 all_dates.join

String allDatesJoined = String.join("\n", all_dates);

您还将结果分配回了 all_dates 数组。您需要告诉 Java 您想要使用数组中的哪个位置。如果您想分配给数组的第一个位置,请使用 all_dates[0]

all_dates[0] = allDatesJoined;
英文:

Most likely you wanted to use the String.join method instead of all_dates.join:

String allDatesJoined = String.join("\n", all_dates);

You are also assigning the result back into the all_dates array. You need to tell Java which position in the array you want to use. If you want to assign to the first array position, use all_dates[0]:

all_dates[0] = allDatesJoined;

答案2

得分: 1

你可以使用Java 8的Stream API来实现你的逻辑,代码如下:

System.out.println( Arrays.stream(all_dates).collect(Collectors.joining(",")));

你可以使用任何分隔符。在这里,我使用了**,,但你也可以使用\n**。

Arrays.stream(all_dates).collect(Collectors.joining("\n")));

你可以在Collectors.joining中获取关于详细信息。

英文:

You can implement your logic using Java8 stream API as below.

System.out.println( Arrays.stream(all_dates).collect(Collectors.joining(",")));

You can use any delimiter. Here i had used ',', but you can use \n as well.

Arrays.stream(all_dates).collect(Collectors.joining("\n")));

You will get detail information about Collctors.joining

答案3

得分: 1

首先,您不能将连接操作的结果分配给all_dates本身,因为它是一个String[]。您需要将结果分配给一个String变量。

您可以使用Java 8 Stream API将数组的内容按行连接起来,示例如下:

String all_dates_concatentated = Arrays.stream(all_dates).collect(Collectors.joining("\n"));

更多详情请参考Java文档中关于java.util.stream.Collectors类的说明。

英文:

First of all, you cannot assign the result of the joining operation to all_dates to itself, because it is a String[]. You will need to assign the results to a String variable.

You can concatenate the contents of the array with newlines by using the Java 8 Stream API as shown below:

String all_dates_concatentated = Arrays.stream(all_dates).collect(Collectors.joining("\n"));

For more details you can refer to the Javadoc for the java.util.stream.Collectors class

答案4

得分: 1

我可以看到你的代码中有三个问题:

  1. 问题#1 - 未考虑闰年:你总是使用 28 天来表示二月,这是不正确的。闰年有 29 天。这就是 java.time API 很方便的地方,例如:

    import java.time.Month;
    import java.time.Year;
    public class Main {
    public static void main(String[] args) {
    int year1 = 2019;
    int year2 = 2020;
    int month = 1;
    int lengthOfMonth = Month.values()[month].length(Year.of(year1).isLeap());
    System.out.println("Year1年的二月长度为:" + lengthOfMonth);
    lengthOfMonth = Month.values()[month].length(Year.of(year2).isLeap());
    System.out.println("Year2年的二月长度为:" + lengthOfMonth);
    }
    }
    

    输出结果:

    Year1年的二月长度为:28
    Year2年的二月长度为:29
    

    因此, 你应该从你的代码中移除数组 days_in_months,并用 day = Month.values()[month].length(Year.of(year).isLeap()) 替换 day=days_in_months[month]

  2. 问题#2 - 尝试以非静态的方式调用 static 函数,即 String.join,而且还是在一个数组变量上调用,而不是在 String 变量上调用:你需要将它使用为 String.join("\n", all_dates)

  3. 问题#3 - 尝试将连接的字符串赋值给数组:请注意,String.join 返回的是一个 String 值,而不是一个数组。因此,你需要将连接的字符串赋值给一个 String 变量(例如 String allDatesStr = String.join("\n", all_dates)),而不是一个数组变量。

英文:

I can see three problems with your code:

  1. Problem#1 - Not considering leap years: You are always using 28 days for Feb which is not correct. A leap year has 29 days. This is where is java.time API comes handy e.g.

    import java.time.Month;
    import java.time.Year;
    public class Main {
    public static void main(String[] args) {
    int year1 = 2019;
    int year2 = 2020;
    int month = 1;
    int lengthOfMonth = Month.values()[month].length(Year.of(year1).isLeap());
    System.out.println("Length of Feb in the year, " + year1 + " is " + lengthOfMonth);
    lengthOfMonth = Month.values()[month].length(Year.of(year2).isLeap());
    System.out.println("Length of Feb in the year, " + year2 + " is " + lengthOfMonth);
    }
    }
    

    Output:

    Length of Feb in the year, 2019 is 28
    Length of Feb in the year, 2020 is 29
    

    Thus, you should remove the array, days_in_months from your code and replace day=days_in_months[month] with day = Month.values()[month].length(Year.of(year).isLeap())

  2. Problem#2 - Trying to call static function, String.join in a non-static way and that too on an array variable instead of a String varaible: you need to use it as String.join("\n", all_dates).

  3. Problem#3 - Trying to assign the joined strings to an array: Note that String.join returns a String value, not an array. Therefore, you need to assign the joined strings to a String variable (e.g. String allDatesStr = String.join("\n", all_dates)), not an array variable.

huangapple
  • 本文由 发表于 2020年8月23日 22:19:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63548010.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定