-1

i have this code :

import java.util.Scanner;
public class lectureArray {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int n = sc.nextInt();
        int [] arr = new int[n];

        for(int i = 0; i < n; i++)
            arr[i] = sc.nextInt();

        for(int i = n-1; i >= 0; i--);
        System.out.println(arr[i] ) ;
    }

}

and i have this error: "i cannot be resolved to a variable" . no matter how much i tried to get rid of it, it just making more errors or not resolving the error in the first place

Invader
  • 1
  • 1
  • 1

4 Answers4

0

Remove the semicolon from your for loop. Change

for(int i = n-1; i >= 0; i--);

to

for(int i = n-1; i >= 0; i--)
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

Remove semicolon from last from second last line of your code as below

for(int i = n-1; i >= 0; i--);    <---- This is causing you problem
        System.out.println(arr[i] ) ;

to

for(int i = n-1; i >= 0; i--)
        System.out.println(arr[i] ) ;
Parth Soni
  • 11,158
  • 4
  • 32
  • 54
0

These types of problems are very common when you don't follow coding standards. Make sure to format your code properly. Using for, while, if etc. without braces ( {} ) leads to these type of barely noticeable errors.

for(int i = n-1; i >= 0; i--); // **;** ends the scope of your loop
        System.out.println(arr[i] ) ; // so **i** is not visible here.

This is the correct format.

for(int i = n-1; i >= 0; i--){
        System.out.println(arr[i] ) ;
}

Don't hesitate since it take one additional line. but it will save a lot of time when debugging and even reading the code.

Praneeth Peiris
  • 2,008
  • 20
  • 40
0

I think by removing semicolon(;) from the second last line of the code will be enough to get rid of this problem.

Geek_To_Learn
  • 1,816
  • 5
  • 28
  • 48