1

My array looks something like this

String[] dayNames = new String[DAYS_IN_WEEK];
    dayNames[0] = "Sunday";
    dayNames[1] = "Monday";
    dayNames[2] = "Tuesday";
    dayNames[3] = "Wednesday";
    dayNames[4] = "Thursday";
    dayNames[5] = "Friday";
    dayNames[6] = "Saturday";

I need to print the index of the array using a method findDay.

so if "Saturday" was selected, i would need 6 to be returned.

Thanks for your time =D P.s. No answers please? Just suggestions =)

UPPDATE**

my array will not compile. This is exactly what i have:

private static final int DAYS_IN_WEEK = 7;
    String[] dayNames;
    dayNames = new String[DAYS_IN_WEEK]
    // Declare an array of Strings named dayNames
    dayNames[0] = "Sunday";
    dayNames[1] = "Monday";
    dayNames[2] = "Tuesday";
    dayNames[3] = "Wednesday";
    dayNames[4] = "Thursday";
    dayNames[5] = "Friday";
    dayNames[6] = "Saturday";

and I get multiple errors starting with:

Weekdays.java:12: error: <identifier> expected
    dayNames = new String[DAYS_IN_WEEK]

I don't understand why. I literally copied the EXACT format from http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

5
  • 3
    Do you mean "Saturday" and "6"? Or maybe "Sunday" and "0"? Also, have you learned about for loops? If yes, then try to come up with a solution using a for loop. If not, go learn it :) Commented Dec 1, 2012 at 3:06
  • Suggestions: iteration, comparison. Commented Dec 1, 2012 at 3:08
  • @jonathanasdf I meant "Saturday" and 6 lol fail... and yes I have gone over for loops, will defiantly look over my notes! Thanks!! Commented Dec 1, 2012 at 3:13
  • You are missing a semicolon. Commented Dec 1, 2012 at 5:50
  • @WannaBeDroidProgrammer Yep that would be the best idea. The answers below are all correct but I think would be a bit advanced for now. Once you have figured out how to solve the problem with a for loop though, I suggest you take a look at xagyg's solution with indexOf. I would recommend against looking at sdasdadas's enum solution at this point though, it will just be needlessly confusing. Commented Dec 2, 2012 at 2:11

3 Answers 3

2

You could do this...

int index = Arrays.asList(dayNames).indexOf("Saturday");

I'll leave printing the index value as an exercise for you.

Sign up to request clarification or add additional context in comments.

Comments

1

Use a map your key is day and value is index.

Key => Sunday , Monday , Tuesday .....

Value = > 0,1,2 ..

Map the value against the key you required.

Comments

1

Well, one way to do it would be like this:

public int findDay(String dayString) {
    if (dayString.equals("Sunday") {
        return 0;
    }
    else if (dayString.equals("Monday") {
    ...

You get the gist of it - but that's a fairly crufty solution.

An alternative would be to use Java's enumerated values:

public enum Day {
    SUNDAY(0), MONDAY(1), TUESDAY(2), WEDNESDAY(3) ...

    private final int i;
    private Day(int value) {
        i = value;
    }

    public int getNumericRepresentation() {
        return i;
    }
}

Then you can actually have an array of enumerations, like:

Day[] days = new Day[Day.values().size()];
int i = 0;
for (Day day : Day.values()) {
    days[i] = day;
    i++;
}

And to print out a day's numeric representation you just use:

day.getNumericRepresentation();

Comments

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.