double array won't print

87 Views Asked by At

I did some more work on this program, but now I am stuck because the string array prints, but i cant for the life of me get the double array to print. Any help would be appreciated!

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

public class inventoryTracker
   {
      private static Scanner sc;
    private static double itemCost;

    public static void main(String[] args)
      {
     System.out.println("Welcome to the Inventory tracker"
            + "\nThis program will accept the names and costs for 10 stocked items."
            + "\nThe program will then output a table with the names, costs and,"
            + "\nprices of the items."
            + "\nPrices are calculated with a 30 percent markup on cost.");

    sc = new Scanner(System.in);

   String[] product = new String[10];
   Double[] itemCost = new Double[10];


   for (int i = 0; i < itemCost.length; i++ ){
         System.out.print("Enter the item cost :");
         itemCost [i]= sc.nextDouble();

   }

    for (int i = 0; i < product.length; i++){
            System.out.print("Enter the product name :");
            product[i] = sc.next();
    }  

    System.out.println(Arrays.toString(product));





        }


    }
3

There are 3 best solutions below

0
On

That's because you are assigning a string to a string array in your two for loops.

product= sc.toString();

should be

product[i] = sc.toString();

and the same goes for itemCost.

0
On

That's because you are assigning a string to a string array in your two for loops. Also you are doing sc.toString() that is not correct.

product= sc.toString();

and

itemCost= sc.nextDouble();

should be changed to

product[i]  = sc.nextLine();

and

itemCost[i] = sc.nextDouble();

and the same goes for itemCost.

3
On

You need to use index to set a value like:

product[index] = value;

Also you are using sc.toString() to get string from user. It wont work you need to use next() method to get string from user.

Loop should be like:

 for (int i = 0; i < product.length; i++){
    System.out.print("Enter the product name :");
    product[i] = sc.next();
 }
 for (int i = 0; i < itemCost.length; i++ ){
     System.out.print("Enter the item cost :");
     itemCost [i]= sc.nextDouble();
 }