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: Array Lists, PLEASE HALP

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

    Exclamation Array Lists, PLEASE HALP

    Hello, I'm trying to do a program that asks the user to input double values and display the values, along with the sum, average, largest and smallest. The limit of inputs is 15. I have to clear the instance variables at the end. The notes in what I have so far should explain the rest. Any help would be appreciated. Please and thank you

     
    import java.util.Scanner;
     
    public class cheese_ArrayList
    {
            Scanner input = new Scanner(System.in);
            private double sum, average, large, small;
            //private double[] list;
     
            public void cheese_ArrayList() //tell array to hold up to ARRAY_MAX values
            {
                    final int MAX_ARRAY; //store max number of allowed inputs
                    double[] list = new double[ MAX_ARRAY ]; //store double values input by user
     
                    while(MAX_ARRAY < 1 || MAX_ARRAY > 15)
                    {
                            System.out.print("How many values do you want to input?");
                            MAX_ARRAY = input.nextInt();
     
                            if(MAX_ARRAY < 1 || MAX_ARRAY > 15)
                            System.out.println("Please enter a number between 1 and 15 inclusively");
                    }
                    getData();
                    processData();
                    displayData();
                    clearData();
            }
     
            public void getData() //repetition structure to read up to ARRAY_MAX values
            {
                    //number of inputs recorded
                    //limit user to a max of 15 inputs
                    //store actual number of input in count
                    for(int count = 0; count < list.length; count++)
                    {
                            System.out.print("Enter a double value: ");
                            double[] list = input.nextDouble();
                    //      System.out.print("Would you like to enter another value (Y or N)?");
                    //      char answer = input.nextChar();
                    //      if(answer == "n" || answer == "N")
                    }
     
            }
     
            public double processData() //calculate sum, average, large, small and store in list
            {
                    double sum, average, large, small;
     for(int count = 0; count < list.length; count++)
                            sum += list[ count ];
     
                    for(int count = 0; count < list.length; count++)
                            average = list[ count ] / count;
     
                    for(int count = 0; count < list.length; count++)
                            if(large < list[ count ])
                            {
                                    large = list[ count ];
                                    large = count;
                            }
     
                    for(int count = 0; count < list.length; count++)
                            if(small > list[ count ])
                            {
                                    small = list[ count ];
                                    small = count;
                            }
            }
     
            public void displayData() //repetition structure show values in list (array)
            {
                    System.out.printf("%s%d%s%.6d", "Value ", count, "= ", list[ count ]);
                    System.out.printf("\nSum of all values = %d", sum);
                    System.out.printf("\nAverage of all values = %d", average);
                    System.out.printf("\nLargest value = %d", large);
                    System.out.printf("\nSmallest value = %d", small);
            }
     
            public void clearData() //set all instance variable values back to zero
            {
                    list[ count ] = 0;
                    count = 0;
     
                    System.out.println("The list is now cleared.");
            }
    }


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Array Lists, PLEASE HALP

    Ok, so you are not using an ArrayList, but just an Array.
    Looking through your code, there are something things that I could possibly consider a red flag:
    public class cheese_ArrayList
    You are creating a class named cheese_ArrayList. Since there is no main method in this code, it is treated as an cheese_ArrayList Object with a default constructor of: cheese_ArrayList();. I assume you meant to make:
     public void cheese_ArrayList() //tell array to hold up to ARRAY_MAX values
    your main. Have a look at how to declare a main in JAVA. I'll give you a hint, they pretty much make the entire class static.

    final int MAX_ARRAY;
    You are declaring MAX_ARRAY a Constant by using the identifier final. This is fine, but you havent set its value. So, if I'm not mistaking, it will either default to 0 or leave you with a compiler error saying it was never initialized.

    Also, I'm going to make a wild guess and say you are a C Programmer. I get that from your unnecessary use of printf statements. In Java, you dont need place holders to create output. However the printf statement is still used sometimes, but from what I've found, only for formatting numbers and other output specifications. Here is the usual "JAVA" way of doing each output:
    //System.out.printf("%s%d%s%.6d", "Value ", count, "= ", list[ count ]);
    //This is an example of when using printf is useful. This is because you are formatting to 6 decimal places. Can someone check me on this printf below, dont really use it much.
    //You are actually trying to format as an integer by using d and then give it 6 decimal places. You should format as a double by using lg.
    System.out.printf("Value "+count+"= %.6lg",list[count]);
     
    //System.out.printf("\nSum of all values = %d", sum);
    System.out.println("Sum of all values = "+sum);
    //System.out.printf("\nAverage of all values = %d", average);
    System.out.println("Average of all values = "+average);
    //System.out.printf("\nLargest value = %d", large);
    System.out.println("Largest value = "+large);
    //System.out.printf("\nSmallest value = %d", small);
    System.out.println("Smallest value = "+small);
    There are two method of output that are commonly seen in JAVA: System.out.println and System.out.print.
    System.out.println tells the output to automatically advance to the next line after printing out (no need for \n) System.out.print tells the output to continue with the line after printing out. This is incase you want to continue adding to the output line or something.

    Notice how instead of place holders, you simple append the variables to the Strings using +. You can use + to add anything to a String in any situation. Using the String+Object will always call the Object's toString method. The toString method determines how the object will be displayed. For simple types, it is just their value. For Objects, by default it is their location in memory. System.out.printf works fine, but it is just a little bit of overkill for JAVA unless you are trying to do something like format a double to two decimals or something, since appending to text more closely resembles how we read English compared to place holding.



    There is lots of fixing to do with your code. I can guess you are a beginning and I'll be happy to step you through this provided you are willing to do some problem solving in the process and provided you are learning as we fix it.
    Last edited by aussiemcgr; September 4th, 2010 at 07:51 PM.

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

    Iamthecheese (September 7th, 2010)

  4. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Array Lists, PLEASE HALP

    Cross posted at Array Lists, please help - Java

    I see you followed some my advice from the other forum, but not all of it.
    Last edited by Norm; September 4th, 2010 at 08:11 PM.

  5. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Array Lists, PLEASE HALP

    Norm, have you found every Java forum on the face of the planet? You seem to always find the cross-posts.

  6. #5
    Junior Member
    Join Date
    Sep 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Array Lists, PLEASE HALP

    Thank you so much! I feel so stupid...that's what I get for not taking programming for 2 years :/

    I was wondering if fixing this debacle would fix my list[ count ] reference in the processData() method? If not how are arrays properly called and stored in this case? I don't remember anything about this

    Thanks.

  7. #6
    Junior Member
    Join Date
    Sep 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy Re: Array Lists, PLEASE HALP

    Okay, I now realize that I have a bigger problem. I know the UML Diagram goes as follows:

    cheese_ArrayList
    ------------------------------------------------------
    - ARRAY_MAX : Integer (FINAL) = 15
    - list : Double Array
    - count : Integer
    - sum : Double
    - average : Double
    - large : Double
    - small : Double
    ------------------------------------------------------
    <<constructor>> cheese_ArrayList ( )
    + getData ()
    + processData ()
    + displayData ()
    + clearData()

    ...ok, so since there is no main, how do I go about allowing user input in the methods for the array?
    Otherwise I would just use import.java.util.Scanner...but I'm now getting errors like:

    ARRAY_MAX = input.nextInt();
    ^ cannot find symbol

    As I said earlier (I think) the ARRAY_MAX (I was dyslexics) can be defined by the user in the program...which means it doesn't have to be a final...right? I mean, how other way am I going to allow this with user input? The max space for the array is 15 so if the user only wants to enter 14 doubles it shouldn't be final.

    Please help with this I/O fiasco

  8. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Array Lists, PLEASE HALP

    ARRAY_MAX = input.nextInt();
    ^ cannot find symbol
    Which variable is the ^ under? The compiler can't find its definition.

  9. #8
    Junior Member
    Join Date
    Sep 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Array Lists, PLEASE HALP

    oh, sorry, it's under input. Assuming it's because of the no scanner thing. But how do I globalize the scanner to accept user input without a main? should I just put it in getData()?

  10. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Array Lists, PLEASE HALP

    The variable input needs to be in scope at the location you are trying to use it. Without seeing any code, that's the best I can do.

  11. #10
    Junior Member
    Join Date
    Sep 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Array Lists, PLEASE HALP

    import java.util.Scanner;
     
    public class cheese_ArrayList
    {
            double[] list;// = new double[]; //store double values input by user
            private double sum, average, large, small;
            private int ARRAY_MAX, count;
     
            public void getData()
            {
                    Scanner input = new Scanner( System.in );
                    //set input max
                    //limit max to 15
                    while(ARRAY_MAX < 1 || ARRAY_MAX > 15)
                    {
                            System.out.print("How many values do you want to input?");                                    ARRAY_MAX = input.nextInt();
     
                            if(ARRAY_MAX < 1 || ARRAY_MAX > 15)
                            System.out.println("Please enter a number between 1 and 15 inclusively");
                    }
                    //number of inputs recorded
                    //store actual number of input in count
                    for(int count = 0; count < list.length; count++)
                    {
                            System.out.print("Enter a double value: ");
                            //in = input.newDouble();
                            list[ count ] = input.newDouble();
                    }
     
            }
     
            //calculate doubles
            public double processData()
            {
                    for( count = 0; count < list.length; count++)
                    {
                            sum += list[ count ];
                    }
     
                    for( count = 0; count < list.length; count++)
                    {
                            average = list[ count ] / count;
                    }
                    for( count = 0; count < list.length; count++)
                    {
                            if(large < list[ count ])
                            {
     large = list[ count ];
                                    large = count;
                            }
                    }
     
                    for( count = 0; count < list.length; count++)
                    {
                            if(small > list[ count ])
                            {
                                    small = list[ count ];
                                    small = count;
                            }
                    }
            }
     
            //display data in list
            public void displayData()
            {
                    System.out.printf("%s%d%s%.6d", "Value ", count, "= ", list[ count ]);
                    System.out.printf("\nSum of all values = %d", sum);
                    System.out.printf("\nAverage of all values = %d", average);
                    System.out.printf("\nLargest value = %d", large);
                    System.out.printf("\nSmallest value = %d", small);
            }
     
            //reset instance variables
            public void clearData()
            {
                    list[ count ] = 0;
                    count = 0;
     
                    System.out.println("The list is now cleared.");
            }
    }

    this is what i have so far. i just popped the scanner in the getData()

  12. #11
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Array Lists, PLEASE HALP

    Question for you:

    Where do you initialize list?

Similar Threads

  1. Problems in linked lists
    By Hotzero in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 5th, 2010, 09:25 AM
  2. iterators & linked lists in java question
    By somewhat_confused in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 4th, 2010, 10:59 AM
  3. Lists of Sets and Sets of Lists
    By Newoor in forum Collections and Generics
    Replies: 2
    Last Post: December 8th, 2009, 08:13 PM
  4. Constructors, Hash Tables, & Linked Lists
    By illusion887 in forum Collections and Generics
    Replies: 2
    Last Post: December 3rd, 2009, 03:46 AM
  5. Storing an array into an array
    By vluong in forum Collections and Generics
    Replies: 4
    Last Post: September 22nd, 2009, 02:14 PM

Tags for this Thread