2

I am trying to write a simple program which has 2 classes, one with set methods, and the other with the main, executable method.

First Class:

import java.util.ArrayList;

public class Practice
{
    private String firstName;
    private String lastName;
    ArrayList<Double> sales = new ArrayList<Double>();

    public Practice(String firstName, String lastName)
    {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public void setFirstName(String firstName)
    {
        this.firstName = firstName;
    }

    public String getFirstName()
    {
        return firstName;
    }

    public void setLastName(String lastName)
    {
        this.lastName = lastName;
    }

    public String getLastName()
    {
        return lastName;
    }

    public void addSales(double sale)
    {
        sales.add(sale);
    }

    public ArrayList<Double> returnSales()
    {
        return sales;
    }
}

Main Class:

import java.util.Scanner;

public class PracticeExecutable {

    public static void main(String[] args)
    {
        double sale;
        String firstName;
        String lastName;
        Scanner input = new Scanner(System.in);

        System.out.println("Please Enter first name: ");
        firstName = input.nextLine();

        System.out.println("Please Enter last name: ");
        lastName = input.nextLine();

        Practice employee = new Practice(firstName, lastName);

        do
        {
            System.out.println("Please enter sale amount or -1 to quit: ");
            sale = input.nextDouble();
            if(sale == -1)
                break;
        }while(sale != -1);

        employee.addSales(sale);
        input.close();


        System.out.println("Name: " + employee.getFirstName() + " " + 
            employee.getLastName());
        System.out.println("Sales: " + employee.returnSales());
    }

}

I am trying to pass the user input into the ArrayList sales, then print the sales that were input, but it seems it is only recognizing '-1' as a sale. I can't seem to locate the issue though.

1
  • What you typing when program ask you for sale amount? Commented Dec 12, 2017 at 17:52

2 Answers 2

1

It's because in loop you only check if sale is invalid and do nothing with valid sale values. When you exit from loop (sale input is -1) you add that wrong value to list. For change this simple move employe.addSales(sale) to loop like this:

       do {
            System.out.println("Please enter sale amount or -1 to quit: ");
            sale = input.nextDouble();
            if(sale == -1) break;
            employee.addSales(sale);

        } while(sale != -1);
Sign up to request clarification or add additional context in comments.

1 Comment

Wow. I have no idea how I overlooked that. Thanks!
1

Carefully observe this line:

employee.addSales(sale);

This is the line that adds the sales to the ArrayList. This line only runs once, and it always has the value of -1. Afterall that's when the loop breaks when sales is -1. So it simply adds -1 to the ArrayList, nothing else.
What you need to do is move this line inside the do while block. Just make sure that the sale is added to the ArrayList when the value of sales is not equal to -1. Rest will be fine.

1 Comment

I see that now. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.