2

I'm trying to find an integer inside of an int[] array and return true or false depending on whether or not it's present anywhere, however my code, which I've taken from numerous websites.

My array is as follows:

private static final int[] thirtyOneDays = {1, 3, 5, 7, 8, 10, 12};

And I'm searching it like so:

Arrays.asList(thirtyOneDays).contains(month)

Where month is an integer.

When debugging the program all the variables and fields are set correctly as expected, it just always returns false. Does anyone know why this is?

As for month, it originates from a JTextField on my GUI:

new Validate(txtDay.getText(), txtMonth.getText(), txtYear.getText());

Here's the Validate constructor:

public Validate(String day, String month, String year)
{
    instance = this;

    if(this.isYearValid(year))
    {
        if(this.isMonthValid(month, year))
        {
            if(this.isDayValid(day, month, year))
            {
                isAllValid = true;
            }
        }
    }
}

And here's the very top of isDayValid():

private boolean isDayValid(String dayS, String monthS, String yearS)
{
    int year = Integer.parseInt(yearS);
    int month = Integer.parseInt(monthS);
    int day = Integer.parseInt(dayS);

~~~~~~~ P.S. What's the difference between:

int[] intArray = {1, 2, 3}
int[] intArray = new int{1, 2, 3, 4}
Integer[] intArray = new Integer{1, 2, 3, 4}
5
  • Can you show how month is initialized? Commented Nov 18, 2014 at 17:42
  • @SirRichie it's inputted by the user and then fed to the constructor of another class where it's fed to this method and converted from string to integer (successfully) Commented Nov 18, 2014 at 17:46
  • Can you show the code? Commented Nov 18, 2014 at 17:48
  • I'll edit my post now. Commented Nov 18, 2014 at 17:49
  • have a look here stackoverflow.com/questions/1073919/… Commented Nov 18, 2014 at 17:50

4 Answers 4

3

Arrays.asList(thirtyOneDays) returns a List containing a single entry of type int[] which is different from any of the integers in the original List.

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

3 Comments

Could you explain this in more depth please? My knowledge around arrays is pretty weak.
maybe you should add for clarification, that return type of asList will be List<int[]> an not List<Integer> in this case
Wow, I never noticed this subtle detail - definitely deserves an upvote.
2

Try this instead:

    for(int x:thirtyOneDays) {
        if (x == month) {
            return;
        }
    }

You are trying to compare an array of integers with a single integer. So it's a bit like saying is this pen like this group of ten pens. The answer is always going to be no.

Regarding this bit:

int[] intArray = {1, 2, 3}
int[] intArray = new int{1, 2, 3, 4}
Integer[] intArray = new Integer{1, 2, 3, 4}

int is a base type that you can't call methods on, whereas Integer is a wrapper class that allows you to essentially call methods.

I'm not sure what the difference between the first two lines is, but I suspect it concerns how Java allocates memory.

1 Comment

Thanks. This answer is quite simple and easy to understand and worked straight away.
0

int[] is an array of primitvs. Where Integer[] is an array of Integer objects. Thus it size is different. It is not the same type at all.

Comments

0

Or try this. With a while-loop and a boolean expression you don't have to iterate thru the whole array.

boolean match = false;
int index = 0;
while(!match && index < thirtyOneDays.length) {
    if(thirtyOneDays[index] == month) {
        match = true;

        //do what u want with the matching int
    }
index++;
}

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.