-1

import java.util.Scanner;

public class hh {

Scanner input = new Scanner(System.in);

    System.out.print("Enter the size of the array:  ");
    int numbers = input.nextInt();

    // Declare an array called numbers with a size of 10
    int[] numbers1 = new int[numbers];

    insertRandomNumbers(numbers1);

    // Print size of numbers
    System.out.println("Initial Array: ");
    for (int i = 0; i < numbers1.length; i++) {
        System.out.print(numbers1[i] + " ");
    }
    System.out.println("");


    //Print First and Last Elements
    System.out.println();
    System.out.println("First and Last Elements");

    int [] lastStep = lastStep(numbers1);
    for (int i = 0; i < lastStep.length; i++) {
        System.out.print(lastStep[i] + ", ");
    }




} // end main

public static int[] lastStep(int[] numbers1) {

    //to get first
    int [] firstElement= numbers1.get(0);

    //last number
    int [] lastElement= numbers1.get(numbers1.size()-1);

}
return lastStep;









public static void insertRandomNumbers(int[] x) {
    for (int i = 0; i < x.length; i++) {
        x[i] = random();
        // System.out.print(x[i] + " ");
    }
    // System.out.println();
}

public static int random() {

    int r = 0 + (int) (Math.random() * (101 - 0)) + 0;
    return r;

}

My program ask the user to enter a number, then if 10 is entered 10 random numbers are created. With those 10 numbers I need to get the first and last numbers. The way I have my method right now I am getting ERROR: Cannot invoke get(int) on the array type int[]

WHEN I USE public static int[] lastStep(int[] numbers1) {

    //to get first
    int [] firstElement= numbers1.array[0];

    //last number
    int [] lastElement= numbers1.array[numbers1.size()-1];

}
return lastStep;

I get that array cannot be resolved or is not a field

1
  • To your last edit: You don't use numbers1.array[0], it's just numbers1[0]
    – Jeremy W
    Nov 29, 2014 at 0:46

3 Answers 3

0

That's because you're using an Array not an ArrayList. Try using numbers1[0] and numbers1[numbers.length - 1]

You should consider changing your lastStep function. From what I can see, it does nothing, because the return statement is outside the function braces. There is also no variable lastStep inside the function that can be returned. Try the following:

public static string firstAndLast(int[] numberArray)
{
    return numberArray[0] + ", " + numberArray[numberArray.length - 1];
}

Then just call it like:

System.out.println(firstAndLast(numbers1));
1
  • I tryed that but I still have a error. Look at my code update up Nov 29, 2014 at 0:45
0

You can't use .get(0) on an array, you have to use array[0].

In your case it would be: numbers1[0]

2
  • public static int[] lastStep(int[] numbers1) { //to get first int [] firstElement= numbers1.array[0]; //last number int [] lastElement= numbers1.array[numbers1.size()-1]; } return lastStep; I am getting ERROR: array cannot be resolved or is not a field Nov 29, 2014 at 0:43
  • You don't use numbers1.array[0], it's just numbers1[0]
    – Jeremy W
    Nov 29, 2014 at 0:46
0

Use Array List for to add random numbers , then print the last and first one.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.