Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need some help plz!
#1
Hey i was watching Learning java part 14 on YouTube and i followed through with the tutorial line for line, however , eclipse kept telling me i did something wrong.

Here's the error:


Exception in thread "main" java.lang.Error: Unresolved compilation problem:

i cannot be resolved to a variable

at AL.PrintArray(AL.java:43)
at AL.main(AL.java:20)


and here's my code:
Code:
import java.util.ArrayList;
import java.util.Scanner;

public class AL {

        static Scanner reader = new Scanner(System.in);
        static ArrayList<Integer> array = new ArrayList<Integer>();
        
        public static void main(String[] args)
        {    
            System.out.println("Please enter an array of numbers, type in 0 when finished!");
            int in = reader.nextInt();
            while (in != 0)
            {
                array.add(in);
                in = reader.nextInt();
            }
            
            //The user typed 0
            PrintArray();
            
            System.out.println("What number would you like to delete?");
            int del = reader.nextInt();
            for (int i = 0; i < array.size(); i++)
            {
            
            if (array.get(i) == del){
                array.remove(i);
                break;
            
                }
            }
            //Break will put us right here
            PrintArray();
        }
        
        public static void PrintArray()
        {

System.out.println("-------------------------");
        
        {
        System.out.println(i);    
        }
    }
        
        
}

If you can find the problem please do tell i still have not figured it out <!-- sSad --><img src="{SMILIES_PATH}/icon_e_sad.gif" alt="Sad" title="Sad" /><!-- sSad --> <!-- s:?: --><img src="{SMILIES_PATH}/icon_question.gif" alt=":?:" title="Question" /><!-- s:?: --> <!-- s:?: --><img src="{SMILIES_PATH}/icon_question.gif" alt=":?:" title="Question" /><!-- s:?: --> <!-- s:!: --><img src="{SMILIES_PATH}/icon_exclaim.gif" alt=":!:" title="Exclamation" /><!-- s:!: --> . -Thanks & your tutorials are great keep it up! <!-- sBig Grin --><img src="{SMILIES_PATH}/icon_e_biggrin.gif" alt="Big Grin" title="Very Happy" /><!-- sBig Grin -->
#2
Hello, bigbubblewrap.

The error is in the following code snippet:
[code2=java]public static void PrintArray(){
System.out.println("-------------------------");
{
System.out.println(i);
}
}[/code2]
You need to add an enhanced FOR loop there. So it should look like this:
[code2=java]public static void PrintArray(){
System.out.println("-------------------------");

for (int i : array){
System.out.println(i);
}
}[/code2]
Also known as Rocketalypse.
System.out.println("I prefer Java.");
#3
Thx! it works fine now! <!-- sBig Grin --><img src="{SMILIES_PATH}/icon_e_biggrin.gif" alt="Big Grin" title="Very Happy" /><!-- sBig Grin --> <!-- sBig Grin --><img src="{SMILIES_PATH}/icon_e_biggrin.gif" alt="Big Grin" title="Very Happy" /><!-- sBig Grin -->
#4
Yeah, great spot Vinwarez!

[code2=java]public static void PrintArray(){
System.out.println("-------------------------");
{
System.out.println(i);
}
}[/code2]

Here, when you use the { and }, you are opening and closing a new body... but that is not legal without a body operator such as for, while, if, switch, etc.
My Blog | My Setup | My Videos | Have a wonderful day.
#5
Thanks and No problem. <!-- sSmile --><img src="{SMILIES_PATH}/icon_e_smile.gif" alt="Smile" title="Smile" /><!-- sSmile -->
Also known as Rocketalypse.
System.out.println("I prefer Java.");


Forum Jump:


Users browsing this thread: 1 Guest(s)