Results 1 to 6 of 6
  1. #1
    trakke is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default Java combining arrays

    What I'm trying to do is ask the user for a list of items, and create two arrays - one for the itemName and one for the itemPrice. My program right now deals only with the itemPrice and there's no indication of how I can combine two arrays in one to output a list of both arrays combined, like this:

    Bread - 1.20
    Milk - 2.00

    Here is what I have so far, two arrays, but the name array really isn't included in anything. Thanks!

    Java Code:
    public class TaxClass
        {
        private Input newList;
        /**
         * Constructor for objects of class Tax
         * Enter the number of items
         */
        public TaxClass(int anyAmount)
        {
            newList = new Input(anyAmount);
        }
        /**
         * Mutator method to add items and their cost
         * Enter the sales tax percentage
         */
        public void addItems(double anyTax){
            double salesTax = anyTax;
            newList.setArray(salesTax);
        }
        }
    
        public class Input
        {
        private Scanner keybd;
        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(double anyValue){
            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]);
            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 * anyValue);
               total = total + cost;
            }
            System.out.println("Total tax: " + totalTax);
            System.out.println("Total cost pre-tax: " + total);
            System.out.println("Total cost including tax: " + (total+totalTax));
        }
        }

  2. #2
    FlipPoker@gmail.com is offline Senior Member
    Join Date
    Mar 2011
    Posts
    103
    Rep Power
    0

    Default

    You don't need to combine the arrays to get the output you desire. Just access each array and concatenate them to a single line:
    Java Code:
    ...
    System.out.println(itemArray[index] + " - " + costArray[index]);
    ...

  3. #3
    trakke is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    When I do that it comes up like this:

    Milk-null
    Bread-null
    Cookies-null
    Chocolate-null
    Cereal-null
    Total tax: 0.52
    Total cost pre-tax: 6.5
    Total cost including tax: 7.02

  4. #4
    FlipPoker@gmail.com is offline Senior Member
    Join Date
    Mar 2011
    Posts
    103
    Rep Power
    0

    Default

    You never store the cost in your costArray[]. Look in your for loop, you retrieve the item cost, but you never actually store it in the costArray.

  5. #5
    trakke is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    so how do I fix it?

  6. #6
    FlipPoker@gmail.com is offline Senior Member
    Join Date
    Mar 2011
    Posts
    103
    Rep Power
    0

    Default

    You set the costArray the same way you set your itemArray. Does the cost variable hold the value that you need? If so, you would set it like this:
    Java Code:
    ...
    double cost = Double.valueOf(keybd.next()).doubleValue();
    costArray[indexc] = cost;
    ...

Similar Threads

  1. Problem with combining charset and enctype
    By shihad_s in forum New To Java
    Replies: 0
    Last Post: 02-22-2011, 12:14 PM
  2. problem with combining RGB binary images
    By meth in forum New To Java
    Replies: 0
    Last Post: 01-20-2011, 12:35 PM
  3. Combining these 2 projects
    By fresh83 in forum New To Java
    Replies: 14
    Last Post: 12-28-2009, 08:52 AM
  4. combining two separate codes into one
    By jaiminparikh in forum Advanced Java
    Replies: 22
    Last Post: 02-18-2009, 05:35 PM
  5. Help combining loops into 1 program.
    By kewlgeye in forum New To Java
    Replies: 5
    Last Post: 04-22-2008, 09:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •