Hey guys! So I'm writing a code that takes 10 numbers from the user which is stored in a array. The user then inputs the problem number (1 or 2) depending on what they want to do to the array. Unfortunately, I'm getting and error on line array[i].prob1();.. Here's what I have:

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int[] array = new int[10]; 
        System.out.println("Enter 10 numbers: ");
        for(int i = 0; i < array.length; i++){
            int num = s.nextInt();
            array[i] = num;
        }
        System.out.print("Enter problem number 1 or 2: ");
        int probNum = s.nextInt();

        if (probNum == 1){
            array[i].prob1(); 
        }

    }
    public static int prob1 (int[] array){
    for (int i = 0; i < array[i]; i++){
                if (array[i] % 2 != 0)
           {
               System.out.print(array[i]);
           }
        }   
    return array[1];
    }
}

Thanks guys!

Member Avatar for [NOPE]FOREVER

Hi laguardian,

You cannot access the index of i because it is outside the for loop, because of this your array is out of scope and cannot be accessed outside the for loop.

If you put your if statment inside the for loop the array will be in scope. see below

public static void main(String[] args)
    {
        Scanner s = new Scanner(System.in);
        int[] array = new int[10];
        //int index = s.nextInt();
        System.out.println("Enter 10 numbers: ");
        for(int i = 0; i < array.length; i++)
        {
            int num = s.nextInt();
            array[i] = num;
            if(array.length == 10)
            {
                System.out.print("Enter problem number 1 or 2: ");
                int probNum = s.nextInt();
                if (probNum == 1)
                {
                    prob1(array);
                }
            }

        }


    }

Hope this helps.

Cheers

commented: t +0
commented: tested +0

Thanks for your help! I was able to display the odd numbers from my array. Now, I've created another method which takes a n input from the user and displays what index the user's input is in.

    public static int prob2 (int num, int[] array){
        for (int i = 0; i < array.length; i++){
            if (num == array[i]){
              System.out.print("  " +num+ "is in index " + array[i]); 
            }
        }
        return num;    
    }
}

But when I call the method, it displays the wrong index:
Enter 10 numbers:
1
2
3
4
5
6
7
8
9
10
Enter problem number 1 or 2: 2
Enter number to find: 4
4 is in index 4

It's supposed to be in index 3. What am I missing here?

System.out.print(" " +num+ "is in index " + array[i]);

You are printing the value in array[i], not the index.

Member Avatar for [NOPE]FOREVER

ok obvoiusly the index is supposed to start from 0 through to 9.

Where in your code are you calling prob2?

commented: are you any good at php? +0
commented: tested +0

Here yoy go:

public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int[] array = new int[10]; 
        System.out.println("Enter 10 numbers: ");
        for(int i = 0; i < array.length; i++){
            int num = s.nextInt();
            array[i] = num;
        }
        System.out.print("Enter problem number 1 or 2: ");
        int probNum = s.nextInt();

        if (probNum == 1){
            prob1(array);
        }

        if (probNum == 2){
            System.out.print("Enter number to find: ");
            int numFind = s.nextInt();
            prob2(numFind, array);

        }

        else if (probNum <= 0 || probNum > 2){
            System.out.print("Invalid problem number!");
        }
Member Avatar for [NOPE]FOREVER

The outter for loop here is going through the values the user enters until it reaches the length of the array. Then the inner for loop loops through the indexes of the array until index of 9 is reached then through any of the repitions if the num is equal to the element of i then it will print array[j] which is the index.

public static int prob2 (int num, int[] array)
    {
        for (int i = 0; i < array.length; i++)
        {
            for (int j = -1; i <= 9; j++)
            {
                if (num == array[i])
                {
                    System.out.print(" " + num + "is in index " + array[j]);
                }
            }
        }
        return num;
    }

Hope that helps you

commented: Incorrect untested code -3
commented: cheers +0
commented: ggggggg +0

NopeForever, no, that doesn't help.

array[j] doesn't print the index, it prints the value.
System.out.println(" " + num + " is in index "+ j);
should do the trick.

I doubt very much that that will help anyone. Sorry, but that code is complete nonsense compared to the original. Did you try executing it? I thought not. Just run it and see how bad the output is.

It also includes the mistake from the original code, array[i] (or array[j]) is the value, not the index he is looking for.

commented: countering imo not deserved downvote +13

System.out.println(" " + num + " is in index "+ j);
should do the trick.

... as long as the trick you want to do is chosing between an array index out of bounds or an infinite loop (depending on whether num == array[0] or some other element

... as long as the trick you want to do is chosing between an array index out of bounds or an infinite loop (depending on whether num == array[0] or some other element

the trick I was talking about was printing the index, rather than the value of the element at that index.

Yes, sure. I was just explaining a bit more why the double-loop code was so bad.

Thanks guys! Got the code running fine!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.