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

Thread: What's wrong with my code ?

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What's wrong with my code ?

    What's wrong with my code, it is not giving output properly.
    import java.util.*;
     
    public class Llist {
     
            public static void main(String[] args) {
     
                    String[] things = {"Apple", "Banana", "Orange", "Mango", "Grapes"};
     
                    List<String> list1 = new LinkedList<String>();
                    for(String x : things)
                            list1.add(x);
     
                    String[] things2 = {"Briyani", "Chicken", "Meat", "Fish"};
     
                    List<String> list2 = new LinkedList<String>();
                    for(String y : things2)
                            list2.add(y);
     
                    list1.addAll(list2);
                    list2 = null;
     
                    printMe(list1);
                    removeStuff(list1, 2, 3);
                    reverseMe(list1);
     
            }
     
            private static void printMe(List<String> l) {
                    for(String b : l)
                            System.out.printf("%s ", b);
                    System.out.println();
            }
     
            private static void removeStuff(List<String> l, int from, int to) {
                    l.subList(from, to).clear();
            }
     
            private static void reverseMe(List<String> l) {
                    ListIterator<String> bobby = l.listIterator(l.size());
                    while(bobby.hasPrevious())
                            System.out.printf("%s ", bobby.hasPrevious());
            }
    }
    Last edited by JavaPF; November 5th, 2010 at 08:04 AM. Reason: Please use highlight tags. See my signature.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: What's wrong with my code ?

    What should the proper output be?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    6
    Thanks
    0
    Thanked 6 Times in 3 Posts

    Default Re: What's wrong with my code ?

    Ok, well i'm not sure what output you are going for but i did notice a few things:

    1) Your first method printMe(list1); prints the following "Apple Banana Orange Mango Grapes Briyani Chicken Meat Fish"
    2) The removeStuff method looks fine
    3) There is a problem in the reverseMe method, in the last line of code
    while(bobby.hasPrevious())
    System.out.printf("%s ", bobby.hasPrevious());
    this is an loop that goes on forever, bobby.hasPrevious() is returning true every time.

    But if you tell us what output you are looking for we will be able to help you more.

    Dejan

Similar Threads

  1. Whats wrong with my code?
    By mlan in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 27th, 2010, 01:42 PM
  2. What is wrong with this code, please desperate
    By macnasty in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 6th, 2010, 10:26 AM
  3. [SOLVED] what is wrong for the java code
    By chuikingman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 11th, 2010, 02:00 AM
  4. What can go wrong if you replace && with & in the following code:
    By scott01 in forum Java Theory & Questions
    Replies: 4
    Last Post: February 12th, 2010, 07:47 AM
  5. Generation of Palindrome number in Java
    By tina.goyal in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 26th, 2009, 08:49 AM