0

I'm trying to figure out how to write logic to convert an array of string into an ArrayList weekday object

String[] weekdays = request.getParameterValues("weekday");

I've made my ArrayList weekday2 the length of my String[] weekdays.

ArrayList<WeekDay> weekdays2 = new ArrayList<WeekDay>(weekdays.length);

I know there is a way to utilize enums, and that's what I'm trying to do.

for (int i = 0; i < weekdays.length; i++) {
            weekdays2.add(new WeekDay().valueOf(weekdays2, weekdays));
        }

I know I'm definitely missing something.

error:

The method valueOf(Class<T>, String) in the type Enum<WeekDay> is not applicable for the arguments (ArrayList<WeekDay>, String[])
3
  • How WeekDay looks like? Commented Sep 3, 2020 at 14:20
  • @alexander - It just has an enum in it. The array and arraylist above are in one of my servlets. My WeekDay enum is just this currently. Commented Sep 3, 2020 at 14:50
  • @alexander public enum WeekDay { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY } Commented Sep 3, 2020 at 14:50

1 Answer 1

1

Code to get list of WeekDay may be such:

    for (int i = 0; i < weekdays.length; i++) {
        weekdays2.add(WeekDay.valueOf(weekdays[i]));
    }
Sign up to request clarification or add additional context in comments.

3 Comments

I would suggest to add toUpperCase() since valueOf() matches for the exact identifier.
I think when you develop web application you control all possible parameter values.
Yeah you're right, however this would make the code a little safer at low cost.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.