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

Thread: Array List and Method

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Array List and Method

    For my homework, I had to write a program that prompts the cashier to enter all prices and names, adds them to two arrays lists, calls the method that I implemented, and displays the result and use 0 as the sentinel value. I am having difficulty coming up with a for loop in the method, I believe I have the condition right but I am having trouble with the statements. I now know that String does not have the get property, but I have only done examples with integers and I am not very good with for loops and wouldn't know how to fix it. Any help would be greatly appreciated! Thanks.

    public static void main(String[] args) {
       Scanner in = new Scanner(System.in);
       ArrayList<Double> sales = new ArrayList<Double>();
       ArrayList<String> names = new ArrayList<String>();
       System.out.print("Enter Number of Customers");
       double salesAmount;
       System.out.print("Enter Sales for First Customers");
       salesAmount = in.nextDouble();
       while(salesAmount != 0)
       {
           sales.add(salesAmount);
           System.out.println("Enter Name of Customer: ");
           names.add(in.next());
           System.out.println("Enter the next sales amount, 0 to exit: ");
           salesAmount = in.nextDouble();
       }
     
       String bestCustomer = nameOfBestCustomer(sales, names);
     
    }
     
    public static String nameOfBestCustomer(ArrayList<Double> sales, ArrayList<String> customers)
    {
        String name = "";
        double maxSales;
     
        for (int i = 0; i < sales.size(); i++)
        {
            sales.size(name.get); <== it keeps saying "cannot find the symbol variable get"
        }
     return name;
    }
    Last edited by DragonAge99; March 28th, 2014 at 05:28 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Array List and Method

    I am having difficulty coming up with a for loop in the method,
    What is the for loop supposed to do? Normally for loops are used when the number of times to loop is known.
    cannot find the symbol variable get
    If get is a method, then you need ()s after it, otherwise it is considered a field.

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Array List and Method

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

  4. #4
    Junior Member
    Join Date
    Mar 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array List and Method

    I have to find the name of the customer who has bought the most at the store and the method is supposed to see which name has the highest "sale" and print out that name

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Array List and Method

    Ok, what have you tried? What steps must the code take to solve that problem?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Mar 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array List and Method

    Norm - I don't know exactly what you are asking, the code I have is what I tried and I just tried to follow the homework which is listed below

    Well the customer's purchase amount is stored in ArrayList<Double>, the customer's name is stored in ArrayList<String>, I have to implement a method called public static String nameOfBestCustomer(ArrayList<Double> sales, ArrayList<String> customers), and return the name of the customer with the largest sale. Then we have to return the name of the customer with the largest sale. It has to prompt the cashier to enter all prices and names, adds them to two array lists, calls the method that you implemented, and display the result. And we have to use 0 as a sentinel value.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Array List and Method

    return the name of the customer with the largest sale. Then we have to return the name of the customer with the largest sale
    Is that a repeat?

    What are the steps the code must take as it goes through the items in the ArrayList to find the desired value?

    It sounds like there are separate parts to the problem.
    1) get the data
    2) find the desired value
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. method array list
    By nepperso in forum What's Wrong With My Code?
    Replies: 19
    Last Post: December 9th, 2013, 11:08 AM
  2. Return a List from a method
    By iHank in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 30th, 2013, 09:14 AM
  3. Converting Two dimensional array into an Array list
    By NewbieJavaProgrammer in forum Object Oriented Programming
    Replies: 11
    Last Post: September 29th, 2012, 04:23 PM
  4. help with delete method of linked list
    By sonicjr in forum Collections and Generics
    Replies: 1
    Last Post: February 18th, 2012, 09:21 PM
  5. Array List of Array Lists working for first item but not for second.
    By javapenguin in forum Collections and Generics
    Replies: 6
    Last Post: February 15th, 2012, 05:12 PM