简体   繁体   中英

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'.

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 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;

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

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

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 eg

    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 (eg String allDatesStr = String.join("\\n", all_dates) ), not an array variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM