0

I'm new to Java, obviously, and am working on homework where I'm given an array and then have to mentally manipulate it with various for loops several times. I've completed my work however I, being new to and excited about computer science, figured I could write a basic program to check my work.

This is the code I've written and my compiler keeps yelling at me that it "cannot find symbol - variable a" towards the bottom there. My ignorant thinking tells me that I created "a" when I named the array "a". Sadly I haven't been able to find an example code similar to this. Can you guys tell me what I'm doing wrong?

import java.util.Scanner;

public class ArrayTest
{
public static void main(String[] args)
{
int[] a = { 1, 2, 3, 4, 5, 4, 3, 2, 1, 0 };// the array I'm working on

}
{
for (int i = 1; i < 10; i++) { a[i] = a[i - 1]; } //the manipulation given
}
{   
System.out.println(a[i]);
}
}

Thank you!

3
  • 2
    Your code is all over the place...You have 2 initializer blocks. The scope of your variable is restricted to your main method. Why do you think putting brackets everywhere is the way to go? Commented Nov 2, 2013 at 22:40
  • And note how the code in Keppil's answer is well-formatted, compared to yours. You can immediately tell what is inside the method and the loop thanks to consistent indentation, which your code lacks severely. Commented Nov 2, 2013 at 22:44
  • pls indent the code before you post Commented Nov 2, 2013 at 22:55

1 Answer 1

4

Your a array is declared as a local member of your main method.

The next blocks after your main method are called instance blocks, because they relate to an instance of your Main class rather than to the body of its main, static, executable method.

Because of that, your for loop references a variable whose scope cannot be accessed.

Move your for loop and the printout to the main method by removing the curly brackets surrounding them in order for your code to compile.

edit just as in Keppil's answer.

As requested, a bare copy-paste of Keppil's code.

public static void main(final String[] args) {
    int[] a = { 1, 2, 3, 4, 5, 4, 3, 2, 1, 0 };// the array I'm working on
    for (int i = 1; i < 10; i++) {
        a[i] = a[i - 1]; // the manipulation given
        System.out.println(a[i]);
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you very much. My teacher claims that by the end of the semester syntax errors will be a thing of the past. I hope he's right. Again thank you for the help.
@phantasms you're welcome! Syntax errors happen more seldom if you use an IDE, which I strongly suggest. eclipse is my favorite Java IDE. I invite you to download and take a look. It's open source, free, and incredibly comfier than, say, vi.
@Mena Thank you. I had downloaded it but stuck to BlueJ and sublime out of comfort. I'll take the time to learn Eclipse so I waste less of your time!
@phantasms do that - it's worth it. Eclipse is packed with features and can seem a bit confusing at first, but I guarantee you'll end up falling in love eventually.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.