BP Forums
Need some help plz! - Printable Version

+- BP Forums (https://bpforums.info)
+-- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=55)
+--- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=56)
+---- Forum: Java (https://bpforums.info/forumdisplay.php?fid=19)
+---- Thread: Need some help plz! (/showthread.php?tid=597)



Need some help plz! - bigbubblewrap - 08-09-2012

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 -->


Re: Need some help plz! - Vinwarez - 08-10-2012

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]


Re: Need some help plz! - bigbubblewrap - 08-10-2012

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 -->


Re: Need some help plz! - brandonio21 - 08-10-2012

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.


Re: Need some help plz! - Vinwarez - 08-10-2012

Thanks and No problem. <!-- sSmile --><img src="{SMILIES_PATH}/icon_e_smile.gif" alt="Smile" title="Smile" /><!-- sSmile -->