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

Thread: Help, Building List & finding min and max

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

    Default Help, Building List & finding min and max

    I need to build a list and then return the Min and Max number, The code I have just keeps returning 0s. Could any one help? Thnx

    private SimpleList<Integer> buildList(String line) {
    //Build a list of integers from line
    //Assume the line is syntactically correct
    SimpleList <Integer> list;
    list = new SimpleList <Integer>();

    int i = 0;

    while (i<line.length()){

    list.add(i);
    i++;
    }
    return list;
    }

    private int findMax(SimpleList<Integer> list) {
    //Return the maximum integer in list
    list.reset();

    int Max = list.next();

    while (list.hasNext()){
    int t = list.next();
    if(t > (Max));
    t = Max;
    }
    return Max;

    }

    private int findMin(SimpleList<Integer> list) {
    //Return the minimum integer in list
    list.reset();

    int min = list.next();

    while (list.hasNext()){
    int x = list.next();
    if(x < (min));
    x = min;
    }
    return min;
    }


  2. #2
    Junior Member
    Join Date
    Nov 2011
    Location
    Budapest, Hungary
    Posts
    10
    My Mood
    Mellow
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Help, Building List & finding min and max

    Your logic here is a bit tangled.

    For instance, to find the maximum value, you need to do something like this:

    max = 0

    repeat for each list element:

    x = next list element
    if x > max then max = x

    finally, return max
    Cave of Programming -- programming info and help, exercises, tutorials and other stuff.

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 168 Times in 159 Posts

    Default Re: Help, Building List & finding min and max

    What does the reset method do? Do you have to write the findMax and findMin methods? It would much more efficient to find the max and min values as you are building the list. Which is rather an odd method. If you pass the String "frog" to the buildList meethod then the List will contain 0,1,2 & 3., if you pass "help" you get the same List and if you pass "apple" you get 0,1,2,3 & 4. Is that what the method is really supposed to do?
    Improving the world one idiot at a time!

  4. #4
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help, Building List & finding min and max

    Well the the buildlist is suposed to build a list from the string entered. Then the FindMax & Min are suposed to figure out what the min and maxium values are are display them.

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

    Default Re: Help, Building List & finding min and max

    Yeah I'm fairly sure we are all aware of what your requirements are. Perhaps you could provide an example of what the input and output is and what you think the output should be. As well as ask a specific question. Is there anything about my previous reply you did not understand?
    Improving the world one idiot at a time!

Similar Threads

  1. JSF: Dynamically building SelectItems
    By Tsarin in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: December 4th, 2011, 04:58 PM
  2. Need help finishing code. List Building...
    By Margaret_Girl87 in forum Object Oriented Programming
    Replies: 1
    Last Post: November 30th, 2011, 05:09 PM
  3. Linked list Schminked list help with Nodes Please
    By Bially in forum Collections and Generics
    Replies: 1
    Last Post: September 29th, 2011, 03:20 PM
  4. Finding min in linked list
    By lieles in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 7th, 2011, 01:06 AM
  5. Building a Menu List
    By websey in forum AWT / Java Swing
    Replies: 1
    Last Post: November 15th, 2009, 09:34 AM