Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

A supermarket wants to reward its best customer of each day, showing the customer’s name on a screen in the supermarket. For that purpose, the customer’s purchase amount is stored in an ArrayList and the customer’s name is stored in a corresponding ArrayList. Implement a method public static String nameOfBestCustomer(ArrayList sales, ArrayList customers) that returns the name of the customer with the largest sale. Write a program that prompts the cashier to enter all prices and names, adds them to two array lists, calls the method that you implemented, and displays the result. Use a price of 0 as a sentinel.

So far I'm having a problem inputing the numbers/ names into the array by keyboard using a for loop. This is what i have so far;

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

public class TopCustomer {

    public static String nameOfBestCustomer(ArrayList<Double> sales, ArrayList<String> customers) {

}

public static void main (String [] args){
    ArrayList<Double> sales = new ArrayList<Double>();
    ArrayList<String> customer = new ArrayList<String>();
    Scanner in = new Scanner(System.in);        
    System.out.print("How many Customers are there?");
    int num = in.nextInt();

    for (int i = 1; num <= i; i++) {
        System.out.print("Enter name of customer " + i + ": \n");
        customer.add(i) = in.nextString();
        System.out.print("Enter how much customer " + i + ": \n");
        sales.add(i) = in.nextDouble(); 
    }
}

}

share|improve this question
1  
use customer.add(in.nextString()); same for sales. –  Braj Mar 13 '14 at 0:02
    
I'm still getting an error: cannot find symbol customer.add(in.nextString()); –  Sjanes227 Mar 13 '14 at 0:23

2 Answers 2

up vote 2 down vote accepted

you have 2 errors here i have corrected them below

for (int i = 0; i < num; i++){
    System.out.print("Enter name of customer " + (i+1) + ": \n");
    customer.add(in.next());
    System.out.print("Enter how much customer " + (i+1) + ": \n");
    sales.add(in.nextDouble()); 
}

first your for loop should be formatted the way i presented becuase otherwise it was always ending instantly unless there is only 1 customer and also the .add() method you were using automatically adds what is inside to the end of the array list.

share|improve this answer
    
Thank you for the help! I'm still getting an error: cannot find symbol customer.add(in.nextString()); –  Sjanes227 Mar 13 '14 at 0:41
    
sry, i updated that line, try using the update –  mig Mar 13 '14 at 0:50

Your error is here:

for (int i = 1; num <= i; i++) { 

If a user inputs a num greater than 1, this loop will terminate immediately. Want you want it is:

for (int i = 1; i <= num; i++) {

You are also adding in the data wrong. Instead of this:

customer.add(i) = in.nextString();

and

  sales.add(i) = in.nextDouble(); 

Do,

customer.add(in.nextString())

and

sales.add(in.nextDouble());
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.