[英]Getting the error Cannot resolve method 'join' in 'String' when using the join() method
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'.基本上我想要做的就是将每个数组元素放在一个新行上,但是当使用 join() 方法时,我收到错误,无法解析“String”中的方法“join”。
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
}
Most likely you wanted to use the String.join
method instead of all_dates.join
:很可能您想使用
String.join
方法而不是all_dates.join
:
String allDatesJoined = String.join("\n", all_dates);
You are also assigning the result back into the all_dates
array.您还将结果分配回
all_dates
数组。 You need to tell Java which position in the array you want to use.您需要告诉 Java 您要使用数组中的哪个位置。 If you want to assign to the first array position, use
all_dates[0]
:如果要分配给第一个数组位置,请使用
all_dates[0]
:
all_dates[0] = allDatesJoined;
You can implement your logic using Java8 stream API as below.您可以使用 Java8 流 API 实现您的逻辑,如下所示。
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.在这里我使用了
','
,但您也可以使用\\n
。
Arrays.stream(all_dates).collect(Collectors.joining("\\n")));
You will get detail information about Collctors.joining
您将获得有关
Collctors.joining
详细信息
First of all, you cannot assign the result of the joining operation to all_dates to itself, because it is a String[]
.首先,您不能将连接操作的结果分配给 all_dates 给它自己,因为它是一个
String[]
。 You will need to assign the results to a String
variable.您需要将结果分配给
String
变量。
You can concatenate the contents of the array with newlines by using the Java 8 Stream API as shown below:您可以使用 Java 8 Stream API 将数组的内容与换行符连接起来,如下所示:
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有关更多详细信息,您可以参考java.util.stream.Collectors类的 Javadoc
I can see three problems with your code:我可以看到您的代码存在三个问题:
Problem#1 - Not considering leap years : You are always using 28
days for Feb
which is not correct.问题#1 - 不考虑闰年:你总是在
Feb
使用28
天,这是不正确的。 A leap year has 29
days.闰年有
29
天。 This is where is java.time
API comes handy eg这是
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("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())
因此,您应该从代码中删除数组
days_in_months
并将day=days_in_months[month]
替换为day = Month.values()[month].length(Year.of(year).isLeap())
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)
.问题#2 - 尝试以非静态方式调用
static
函数String.join并且也在数组变量而不是String
变量上调用:您需要将其用作String.join("\\n", all_dates)
。
Problem#3 - Trying to assign the joined strings to an array : Note that String.join
returns a String
value, not an array.问题#3 - 尝试将连接的字符串分配给数组:注意
String.join
返回一个String
值,而不是数组。 Therefore, you need to assign the joined strings to a String
variable (eg String allDatesStr = String.join("\\n", all_dates)
), not an array variable.因此,您需要将连接的字符串分配给
String
变量(例如String allDatesStr = String.join("\\n", all_dates)
),而不是数组变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.