Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Program not working

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Program not working

    import java.util.*;
    public class Arraylistcollection
    {
    ArrayList<Integer> list = new ArrayList<Integer>();
    Public static void main (String args[])
    {
    //Numberinglist(list);
    int total = 0;
    Iterator<Integer> iter = list.iterator();
    while (iter.hasNext())
    {
    Integer val = iter.next();
    total = total + val;
    }
    System.out.println("The total Amount is" +total);
    }
    private static void Numberinglist(ArrayList<Integer> list)
    {
    list.add(new Integer(1));
    list.add(new Integer(2));
    }


    }

    --- Update ---

    Sorry
    Numberinglist(list); is not commented.
    it is still throwuing an error on the opening braces to Public static void main(String args[]);

    --- Update ---

    Error is:

    Compilation Errors Detected

    Line: 6
    <identifier> expected


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Program not working

    Java is case sensitive.
    Improving the world one idiot at a time!

  3. The Following User Says Thank You to Junky For This Useful Post:

    KSR (September 19th, 2013)

  4. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Program not working

    Hi..
    Thankyou for the reply,, but where exactly am i using the wrong case, im not able to figure out.

    Thanks for helping.

  5. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Program not working

    Read the error message.
    Improving the world one idiot at a time!

  6. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Program not working

    H,,
    i got it.. public should be all low case.

    ta,
    KSR

  7. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Program not working

    Cheers!
    Improving the world one idiot at a time!

  8. The Following User Says Thank You to Junky For This Useful Post:

    KSR (September 19th, 2013)

  9. #7
    Junior Member
    Join Date
    Sep 2013
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Program not working

    Hi Junky,
    Im really viewing this from C++ perspective, since i am not @ alll fluent with java..

    On solving my previous error, i got an error saying:
    " Line: 8
    non-static variable list cannot be referenced from a static context

    Line: 10
    non-static variable list cannot be referenced from a static context "

    So i removed main function - just to test - i know this would be not good since there wud be no starting point for compiler, but just to test.

    On removal of main, it says:
    Line: 8
    invalid method declaration; return type required

    ---> What im not getting is that, on line 8:" Numberinglist(list);" - here im only trying to call the function with a parameter.. then why exactly is this error popping up..

  10. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Program not working

    You need to understand the difference between static and non static. Basically static means methods and variables belong to the class and not an instance of the class. This means that if you have a static variable in a Foo class, every Foo object you create has access to the same variable. If you have a non-static variable it belongs to an instance of the class, so each object you create has its own version of the variable and each one can have a different value.

    Let's imagine you have a Person class with a name variable. Then in your main method (which is static) you create 5 Person objects each with a different name. Then you say print(name). There are 5 Person objects, which name should it print? If you want to access the non-static variable list then you need to have an instance (object) of the Arraylistcollection class.
    Improving the world one idiot at a time!

  11. The Following User Says Thank You to Junky For This Useful Post:

    KSR (September 19th, 2013)

  12. #9
    Junior Member
    Join Date
    Sep 2013
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Program not working

    Hi Junky,

    yes you are right... And thankyou so much to elaborate on the concept.. So i cannot use an instance in a static void main.. So i removed my void main (since my compiler would not take a non-static main, which is how it should be - otherwise main would get instantiated, martyring basic principles). But when i remove main, it throws other error on line 8:
    Line: 8
    invalid method declaration; return type required
    --> What im not understanding is - this is not procedural - then y is my compiler, on removal of main, is not understaing on line 8 that im only calling the function - and not declaring/defining it.

    --- Update ---

    Line 8:
    Numberinglist(list);

    --- Update ---

    Compilation Errors Detected

    Line: 8
    invalid method declaration; return type required

    Line: 8
    <identifier> expected

  13. #10
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Program not working

    You cannot remove the main method. This is the entry point to your program. How will you start it without a main method?

    There are 3 solutions:

    Declare list to be static. Most n00bs just make everything static because they do not fully understand what static means and it makes all errors go away. However this is the least desirable. Java is an object oriented language. Static means not using objects so why bother using Java?

    You can move the declaration of list inside main.

    Best option is to use OOP and create an object of the Arraylistcollection class and call its non-static methods.

    BTW it should be ArrayListCollection.

    --- Update ---

    Just to give you an example:
    public class Foo {
        private int value;
        private String colour;
     
        Foo(int v, String c) {
            int value = v;
            colour = c;
        }
     
        public void doStuff() {
            System.out.println("Value: " + value);
            System.out.println("Colour: " + colour);
       }
     
        public static void main(String[] args) {
            Foo f = new Foo(42, "red");
            f.doStuff();
        }
    }
    The only thing static is the main method which has to be static.
    Improving the world one idiot at a time!

  14. The Following User Says Thank You to Junky For This Useful Post:

    KSR (September 19th, 2013)

  15. #11
    Junior Member
    Join Date
    Sep 2013
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Program not working

    So are we creating an instance for a class domestically? - Becaus main is defined in the same class..

    Based on what you taught, this is best and simplest i could come up with:
    import java.util.*;

    public class ArrayL
    {
    public void Numberinglist(ArrayListCollection<Integer> list)
    {
    list.add(1);
    list.add(2);
    }

    public static void main(String[] args)
    {
    ArrayListCollection<Integer> list = new ArrayListCollection<Integer>();
    ArrayL l = new ArrayL();
    l.Numberinglist(list);
    }
    }

    Still error:
    cannot find symbol
    symbol: class ArrayListCollection
    location: class ArrayL

    Something is really wrong.
    Apparently i dont work in java, so i dont have a system available @ office.. im practicing online on this:
    Java Online Compiler & Runner

    -> Do u think something wrong with this cloud app?


    Regards and thanks,
    KSR

    --- Update ---

    anybody??

    --- Update ---



    --- Update ---

    Correct code:

    import java.util.*;

    public class ArrayL
    {
    public void Numberinglist(ArrayList<Integer> list)
    {
    list.add(1);
    list.add(2);
    }

    public static void main(String[] args)
    {
    ArrayList<Integer> list = new ArrayList<Integer>();
    ArrayL l = new ArrayL();
    l.Numberinglist(list);
    int total = 0;
    Iterator<Integer> iter = list.iterator();
    while (iter.hasNext())
    {
    Integer val = iter.next();
    total = total + val;
    }
    System.out.println("The total amount is " +total);
    }
    }

    Thankyou so much for all help!

    Regards,
    KS

Similar Threads

  1. Simple Address Book Program Not Working....
    By programmer101 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 7th, 2013, 01:21 AM
  2. Simple Calcular Program, But not working!!!
    By achugr in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 1st, 2012, 02:53 PM
  3. Cannot get buttons working in GUI program
    By Yeshibo in forum AWT / Java Swing
    Replies: 3
    Last Post: April 23rd, 2012, 12:59 PM
  4. Client Server Program Not Working
    By ROHIT C. in forum Java Networking
    Replies: 3
    Last Post: September 3rd, 2010, 02:30 PM
  5. Substring program, not working
    By Newoor in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 18th, 2009, 12:46 PM

Tags for this Thread