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: Errors with LispList

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Errors with LispList

    Write static method which performs the following operation:
    length takes a list and returns the number of integers in it. For example, with [7,3,8,12,9,14]
    it would return 6.
    import java.util.Scanner;
     
    class Lab4Q2
    {
     
     public static void main(String[] args)
     {
      Scanner in = new Scanner(System.in);
      System.out.print("Enter a list (of integers): ");
      String str = in.nextLine();
      LispList<Integer> b = parseIntLispList(str);
      System.out.print("The list you entered is: ");
      System.out.println(b);
      int l = length(b);
      System.out.println("The number of integers is: "+l);
     
     }
     
     public static int length(LispList<Integer> a){
       int l = a.size();
       return l;
     }
     
    }
    Last edited by helloworld922; October 25th, 2009 at 09:30 AM.


  2. #2
    Member
    Join Date
    Oct 2009
    Posts
    52
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: Errors with LispList

    What is your question?

  3. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Errors with LispList

    ahhh missing method
    public static LispList<Integer> parseIntLispList(String str)
     {
      String line = str.trim();
      String contents = line.substring(1,line.length()-1).trim();
      if(contents.length()==0)
         return LispList.empty();
      String[] nums = contents.split(",");
      LispList<Integer> list = LispList.empty();
      for(int i=nums.length-1; i>=0; i--)
          {
           String num = nums[i].trim();
           list = list.cons(Integer.parseInt(num));
          }
      return list;
     }
    Last edited by helloworld922; October 25th, 2009 at 09:31 AM.

  4. #4
    Member
    Join Date
    Oct 2009
    Posts
    52
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: Errors with LispList

    Still, what is your question?

  5. #5
    Junior Member
    Join Date
    Oct 2009
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Errors with LispList

    ok i sorted that problme out,
    so why doesn't the size method work for LispLists (they work for Array Lists)

  6. #6
    Member
    Join Date
    Oct 2009
    Posts
    52
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: Errors with LispList

    What is the error your are getting? What is the API for LispList?

  7. #7
    Junior Member
    Join Date
    Oct 2009
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Errors with LispList

    symbol : method size()
    location: class LispList<java.lang.Integer>
    int l = a.size();

    this is the error im getting, however reading my notes I think u can only use certain methods with LispList
    such as

    For LispList<E>
    E head()
    LispList<E> tail()
    LispList<E> cons(E item)
    boolean isEmpty()
    static LispList<E> empty()

    There are no other methods, and no constructors (tail, cons and empty are factory methods)

  8. #8
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Errors with LispList

    ... LispList isn't a standard Java API class. The size method probably doesn't work because who-ever wrote it didn't implement the size method.

    Out of curiosity, what is a LispList? As far as I can tell, it doesn't differ from the two implementation methods of lists: linked lists and array lists (based off of how lists operate in the Lisp language).

  9. #9
    Member
    Join Date
    Oct 2009
    Posts
    52
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: Errors with LispList

    To further what helloworld922 said, you'll have to implement your length method by iterating through the LispList and counting the number of elements.

  10. #10
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Errors with LispList

    Not true, that's a very inefficient way to keep track of the size (it's O(n)). Instead, I'd keep track of every time you add/remove something, and then update a variable containing the size of the list accordingly. The memory usage over-all is actually less (don't have to keep track of all the counting variables), but more importantly your running time becomes O(1), a huge difference on large lists that have their length used many times.

  11. #11
    Member
    Join Date
    Oct 2009
    Posts
    52
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: Errors with LispList

    You are right, but this a lisp list we're talking about ;-)

Similar Threads

  1. [SOLVED] Error of "cannot find symbol"
    By big_c in forum File I/O & Other I/O Streams
    Replies: 31
    Last Post: April 9th, 2009, 11:20 AM
  2. Ambiguity and non-static variable reference error in java
    By jenseits in forum AWT / Java Swing
    Replies: 5
    Last Post: December 8th, 2008, 07:04 PM