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

Thread: BlueJ program !

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default BlueJ program !

    I need help !! I have to write a program. The program first takes inputs (i.e., numeric values) supplied by users using a letter as
    sentinel value, and places them into an arraylist. It then displays all the elements of the arraylist,
    and marks the largest and the smallest. I tried doing it but it wont compile. Does anyone have a solution to this please ?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: BlueJ program !

    Post what you tried with the errors. Please read the Announcement topic at the top of the sub-forum to learn how to post code in code or highlight tags and other useful info for newcomers.

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: BlueJ program !

    import java.util.ArrayList;
    import java.util.Scanner;

    public class FindLargestValueInArrayList
    {
    public static void main(String[] args)
    {
    // Make sure you use Double instead of double to specify the data type
    // of the elements in the array
    ArrayList<Double> values = new ArrayList<
    Double>();

    // In fact, for the following code, as long as you put a non-double input,
    // it will stop adding data to the arraylist.
    System.out.println("Please enter value, Q to quit:");
    Scanner in = new Scanner(System.in);
    while (in.hasNextDouble())
    {
    values.add(in.nextDouble()); //We use the add method to add an element to the values arraylist.
    }

    // Find the largest value and store it in a variable called largest.
    // Find the smallest value and store it in a variable called smallest.
    double largest = values.get(0);
    // We go over every element in the arraylist, starting from the second element.
    for(int i = 1; i < values.size(); i++)
    {
    if (values.get(i) > largest)
    {
    largest = values.get(i);
    }
    }

    double smallest = values.get(0);
    // We go over every element in the arraylist, starting from the second element.
    for(int i = 1; i < values.size(); i++)
    {
    if (values.get(i) > smallest)
    {
    smallest = values.get(i);
    }
    }


    // Print all values, marking the largest
    // Print all values, marking the smallest
    // Now let us go over every element in the values arraylist
    for (double element : values)
    {
    System.out.print(element);
    // Evaluate whether or not the element is the largest value.
    // If yes, we print "==> largest value" that follows the element.
    // Evavluate whether or not the element is the smallest value.
    // If yes, we print "==> smallest value" that follows the element.
    if (element == largest)
    {
    System.out.print(element);
    // Evaluate wheteher or not the element is the largest value.
    // If yes, we print "==> largest value" that follows the element.
    if (element == largest)
    {
    System.out.print(" <== largest value");
    }
    System.out.println();

    if (element == smallest)
    {
    System.out.print(element);
    // Evaluate wheteher or not the element is the smallest value.
    // If yes, we print "==> smallest value" that follows the element.
    if (element == smallest)
    {
    System.out.print(" <== smallest value");
    }
    System.out.println();
    }
    }
    }
    }

    }

    --- Update ---

    This code is for a different problem. Im trying to find the largest value and smallest value using the arraylist. It does compile but the largest value and the smallest value are the same thing.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: BlueJ program !

    I guess you didn't bother to read the link I directed you to. You can still do that and then edit your posted code to be more readable.

  5. #5
    Junior Member
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: BlueJ program !

    This code is for a different problem. Have to find the largest value and the smallest value. But when I compile the program the largest and the samllest are the same value. Something is wrong but I cant figure it out. Please help.

    --- Update ---

    I did read the link. I don't understand where Im supposed to post it at.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: BlueJ program !

    Reread the link, item number 1. Post any part of that item you don't understand and need explained further. If you figure it out on a second read, then apply it.

    Think about this statement:

    if ( values.get( i ) > smallest )

    Does that make sense?

Similar Threads

  1. BlueJ Object input?
    By javacloud in forum Java IDEs
    Replies: 2
    Last Post: October 26th, 2013, 11:50 PM
  2. BLUEJ Program Help
    By threlot in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 5th, 2013, 09:42 PM
  3. Bluej help
    By kiara4 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 13th, 2012, 05:29 PM
  4. Bluej issue
    By BeardedAxeWound in forum Object Oriented Programming
    Replies: 16
    Last Post: August 26th, 2012, 03:07 AM
  5. BlueJ trouble or program trouble (Combining Arraylists)
    By star12345645 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 11th, 2012, 12:15 PM