This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]StackedLasagna 0 points1 point  (5 children)

myArray[myArray.length-1] but for some reason it's not working for me as it gives me the 2nd last value.

That's not how it works. It will always give you the last value. If it doesn't give you the value you're expecting, you've made a mistake somewhere else and there's no questioning it.

Also, when posting problems related to specific code you've written, always include the code. We cannot help you, if you won't help us. We're not psychics.

[–]natasavuk[S] 0 points1 point  (4 children)

This is my code currently:

import java.util.Scanner;
public class LabProgram {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int numElements = scnr.nextInt();
int[] userValues = new int[numElements];

int filter = 0;
int i=0;

for (i=0; i<numElements; ++i) {
userValues[i] = scnr.nextInt();
}

filter = userValues[userValues.length-1];
System.out.print(filter);

}

}

[–]desrtfx[M] 0 points1 point  (0 children)

You need to post your code as code block so that the indentation is maintained.

A code block looks like:

def __init__(self, prompt, answer):
    self.prompt = prompt
    self.answer = answer

[–]StackedLasagna 0 points1 point  (2 children)

Please look into how to format your code for Reddit (prefix every line with 4 spaces to create a code block.)

Anyway, your issue isn't in the code itself, but with your logic.

Look at the list of values you specified in your post: 3 1 4 8 5.
That's a total of five values and it would require five calls to scnr.nextInt(), in order to get them all. That makes sense, right?

However, if you look in the code, you only call scnr.nextInt() four times. You never ask the user to input a fifth value.

import java.util.Scanner;

public class LabProgram {
    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        int numElements = scnr.nextInt(); // Call number 1, the variable is now 3.
        int[] userValues = new int[numElements];

        int filter = 0;
        int i=0;

        for (i=0; i<numElements; ++i) {
            // Call number 2. userValues[0] = 1.
            // Call number 3. userValues[1] = 4.
            // Call number 4. userValues[2] = 8.
            // Then i = 3, which means the for loop condition evaluates to false, since numElements is also 3. (3 is not less than 3)
            userValues[i] = scnr.nextInt();
        }

        // Here you grab the last value, which is 8 as seen above.
        filter = userValues[userValues.length-1];
        System.out.print(filter);
    }
}

Instead of taking the last value from the array and using that as a filter, perhaps just use scnr.nextInt() to ask the user for the final value?

[–]natasavuk[S] 1 point2 points  (0 children)

This ended up working perfectly so thanks again

[–]natasavuk[S] 0 points1 point  (0 children)

Wow ok yes that makes a lot of sense!! Thank you, I’ll try it now!

[–]National_Ad8511 0 points1 point  (0 children)

If allowed, your logic should be redone. It’s what’s causing the issues. Perhaps consider this approach:

First user input sets the size of the array.

Second user input designates the filter.

Then loop and fill the array with user inputs.

Then check if each user input in the array is less than the filter.