Novel N-terminal a
Q: How can I add
--- author: - 'Urs
The world of Star
Cochlear implants:
If this is your fi
It's a mystery to
Q: In my Rails ap
Mission A Mission
The authors confir

Q: How does a com
LONDON—The biggest
Q: What is the pu
Q: How to use Htm
The most iconic im
It was after the l
A randomized contr
The invention rela
Q: How to get tex
A. Field of the In
Q: Why do I get this java error: error: ';' expected on the last statement of the for loop in this method? I'm getting this error: error: ';' expected on the last statement of the for loop in the function run(). Why am I getting this error and how do I solve it? public void run(){ int N; N = 2; Scanner scan = new Scanner(System.in); System.out.print("Please enter a positive integer: "); N = scan.nextInt(); while(N < 0){ N = scan.nextInt(); } int[] a = new int[N]; for(int i = 0; i < N; i++){ a[i] = scan.nextInt(); } for(int i = 0; i < N; i++){ System.out.println("The " + i + "th term of sequence is: " + a[i]); } } A: It seems that you are copying some code, which was originally not intended to work like this (i.e. this was not written to work like this). In that case, go back to the code's author and ask them to fix it for you. If you really want to fix it, you should start with finding what's wrong with this line: int[] a = new int[N]; You cannot write int[] a = new int[N]; if you never initialize the length of the array. You should replace the new int[] with a new int[N], as in the code below. int[] a = new int[N]; for(int i = 0; i < N; i++){ a[i] = scan.nextInt(); } But then, we will still have the other problem: The problem of how you use your array in the for loop. If you think you understand what I mean when I say 'the problem of how you use your array', then I suggest you open up another question, ask the question, and post the code as an answer. This answer will likely be a link rot, so use common sense before asking a new question. Also, please consider rephrasing your title a little. "Error - ';' expected" is hard to understand. "Error - I'm getting an error because I'm using the array in a for loop". That is a clearer title and will help people understand your problem a lot better. Good luck. A: Change: int[] a = new int[N]; To int[] a = new int[N]; As: System.out.println("The " + i + "th term of sequence is: " + a[i]); Should look like: System.out.println("The " + i + "th term of sequence is: " + a[i-1]); That is because you create the array and then enter the for loop which starts from i=0 until i=1. To loop from i=1 until i=N, you should change: for(int i = 0; i < N; i++){ System.out.println("The " + i + "th term of sequence is: " + a[i]); } to: for(int i = 1; i <= N; i++){ System.out.println("The " + i + "th term of sequence is: " + a[i-1]); } To be more accurate you should add: scan.nextLine(); after scan.nextInt() to consume the new line after your number, but it will only work if you enter a number followed by a new line. A: change it to: for(int i = 1; i <= N; i++){ System.out.println("The " + i + "th term of sequence is: " + a[i-1]); } The loop is only running once because you only initialised it with N amount of size You also should add an int N = scan.nextInt(); right before the for loop or before the while loop And don't mix the int[] array a with normal int variables, you will get a ArrayIndexOutOfBoundsException when you try to access an array element with a normal int variable. Try to stick to java standard for example do not use == to compare two objects or integers. Use the equals or equalsIgnoreCase functions. Always make sure to define an upper-bound for your array because it will work until you exceed that. A good way to do this is to use a boolean. It doesn't need to be in a variable but instead put the following code in the loop: boolean isFull = false; if (a.length == N) { System.out.println("The array is full"); System.exit(0); } if (N == 0) { System.out.println("No elements have been entered"); System.exit(0); } if (N == 1) { System.out.println("The array is full"); System.exit(0); } if (isFull) { System.out.println("The array is full"); System.exit(0); } else { isFull = true; }