开发者

Adding a list of user-inputted doubles to an Array

开发者 https://www.devze.com 2023-03-01 03:57 出处:网络
So my program, when instantiated asks the user how man开发者_开发知识库y \"items\" they want.My program then creates two arrays, one for the \"item name\" and one for the \"item price\".I use a loop t

So my program, when instantiated asks the user how man开发者_开发知识库y "items" they want. My program then creates two arrays, one for the "item name" and one for the "item price". I use a loop to let the user input each item name and item price in their respective arrays, however I'm lost with the item price array. To use my loop, I need to utilize the "itemprice.length" element but I can't do that when I'm not working with Strings.

After the user inputs the "prices" of each item, I need to apply a multiplier to each array item and output it. So I want to, for example, have 3 items in the array: 1.20, 1.30, 1.40, and then I want the program to ask me for the "sales tax" of which I can enter 0.08 and it will then multiply 0.08 to each item and output a total.

Is there a way that I can make my program work so it allows the user to enter, let's say, 5 items and their prices and am I going about it the right way? Any way of doing this easier? Thanks!

public class Input
{
private Scanner keybd;
private String item;
private double cost;
private String[] costArray;
private String[] itemArray;

/**
 * Constructor for objects of class Scanner
 */
public Input(int anyAmountofItems)
{
    keybd = new Scanner(System.in);
    costArray = new String[anyAmountofItems];
    itemArray = new String[anyAmountofItems];
}
 /**
 * Mutator method to set the item names and costs
 */
public void setArray(){
    for(int index=0; index < itemArray.length; index++){ 
    System.out.println("Enter the item name: ");
    itemArray[index] = keybd.next();}
    for(int indexa=0; indexa < itemArray.length; indexa++){
        System.out.println(itemArray[indexa]);
    }
    for(int indexb=0; indexb < costArray.length; indexb++){ 
    System.out.println("Enter the item cost: ");
    costArray[indexb] = keybd.next();}
    for(int indexc=0; indexc < costArray.length; indexc++){
        System.out.println(costArray[indexc]);
    }
}
    /**
     * Accessor method to return the items cost with tax
     */
    public double getTax(){
        return costArray.length;
    }


Use a Float[] and use Float.parseFloat(String str) to convert from a string to a float.

As an aside, when dealing with money, floating point is a bad idea, since there are always issues with precision. It is best to use ints/longs with the appropriate lowest currency unit (i.e cents in the US etc.)


you can try as:

System.out.println("Enter the sales tax: ");
double salesTax = keybd.next();

double totalTax =0.0;
double total = 0.0;

for(int indexc=0; indexc < costArray.length; indexc++){
System.out.println("Enter the item cost: ");
double cost = Double.valueOf(keybd.next()).doubleValue();
totalTax = totalTax + (cost * salesTax);
total = total + cost;
}

System.out.println("Total: " + (total-totalTax));

EDIT: TO calculate the total during inserting the cost.


It's not clear to me what your question is. Are you having problems with the item cost data? You are just reading in a String. You should read in an array of doubles instead.

costArray = new double[anyAmountOfItems];
// Then when reading use the appropriate Scanner method
costArray[indexb] = keybd.nextDouble();

A couple style notes:

  1. You might want to consider using Lists instead of arrays. That way you don't have to worry about fixing the size of the arrays.
  2. There is no need to use new variable names in each of the for loops. It just adds confusion. Instead of indexa, indexb, etc just use i.
  3. Better yet, use the enhanced for loop for cases where you don't really need the index:

    for( String item : itemArray ) { System.out.println(item); }

0

精彩评论

暂无评论...
验证码 换一张
取 消