-2

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));





        }


    }
  • 1
    Can you please post the exact wording of the assignment? You might be misinterpreting it. – RealSkeptic Nov 12 '14 at 18:03
  • There is no such thing, literally speaking, as "parallel arrays". As @RealSkeptic says, put the full text of the assignment on display. – fge Nov 12 '14 at 18:12

3 Answers3

0

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.

Jannek
  • 94
  • 1
  • 5
0

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.

StackFlowed
  • 6,664
  • 1
  • 29
  • 45
0

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();
 }
SMA
  • 36,381
  • 8
  • 49
  • 73
  • Thank you! That did help a bit, now it is accepting the product names but not the costs. The error is Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextDouble(Unknown Source) at inventoryTracker.main(inventoryTracker.java:29) – JavaStudent Nov 12 '14 at 18:40
  • line 29 is itemCost [i]= sc.nextDouble(); – JavaStudent Nov 12 '14 at 18:40
  • I do not see the mismatch, everything is declared as a double – JavaStudent Nov 12 '14 at 18:40