Parallel Arrays

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

38 Replies - 2351 Views - Last Post: 09 November 2015 - 06:42 AM Rate Topic: -----

#1 dthax   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 15-October 15

Parallel Arrays

Posted 08 November 2015 - 02:25 PM

Hello there!!

I am working on an assignment this week using parallel arrays. I haven't been able to find any examples online or using by books that allows for user input.

My teacher suggest that the array be initialized in under the declarations but I keep receiving an error asking for the "10" to be deleted.

I know I have some other issues a little further down in my code, creating a table and my loop counter possibly, but I just want my array to work and I can hopefully work from there.

import java.util.Scanner;

public class InventoryPriceCalculator {

public static void main (String[]args) {
  
  // Declarations 
   // array for the cost of 10 items
  Double itemCost[10];
  String itemName[10];
  int counter = 1; 
  Double priceCounter; 
 // End Declarations

    System.out.print("Welcome to Inventory Price Calculator"); 
    System.out.print("This program will accept input for ten inventory items");
    System.out.print("After items are enters, the program will output each item's name, cost and selling price.");
       
    Scanner input = new Scanner (System.in);
    
    {
      
    System.out.print("Enter the first item's Name:");
       itemName[0]=input.nextLine();
    System.out.print("Enter the first item's price:");
        itemCost[0] =input.nextDouble();   
                     
        priceCounter = priceCounter + (itemCost[0] * 1.3);  }
     {
  while(counter<9)
       System.out.print("Enter next item's name:");
          itemName[counter]= input.nextLine();
       System.out.print("Enter next item's price:");
          itemCost[counter]= input.nextDouble();
      
       priceCounter = priceCounter + (itemCost[0] * 1.3);  
         
        }
     for (int i=0;i<itemCost.length;i++) {
       System.out.print("Inventory Summary:");
       System.out.println("#\tItem Name\tItem Price\tItemCost\n");
       System.out.println("-\t-----\t-----\t-----\n");
       System.out.println ("listName[i] + “          “ + listCost[i] + “       “ = (listCost[i]*1.3");           
}
                     
     System.out.println("“The total income if everything is sold will be: $" + priceCounter); 
     System.out.print ("Thank you for using Inventory Calculator. Goodbye");
}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Parallel Arrays

#2 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11071
  • View blog
  • Posts: 18,909
  • Joined: 19-March 11

Re: Parallel Arrays

Posted 08 November 2015 - 02:41 PM

The array declaration syntax is incorrect.

Try

itemCost = new double[10];


(unless you really want the wrapper class - which you proably don't)


Also, while I understand that this is for an assignment, you should be aware that parallel arrays are not a recommended technique, and you're going to forget all about them as soon as you get the hang of objects.
Was This Post Helpful? 0
  • +
  • -

#3 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Parallel Arrays

Posted 08 November 2015 - 02:41 PM

To the OP, not Jon: That's not how you declare and initialize arrays. I recommend you review that section of your textbook, notes, or the Oracle tutorials.

This post has been edited by GregBrannon: 08 November 2015 - 02:42 PM

Was This Post Helpful? 0
  • +
  • -

#4 dthax   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 15-October 15

Re: Parallel Arrays

Posted 08 November 2015 - 02:55 PM

Thank you to both of you.

My professor wants it in the declaration as I put it. Or his example was "type myArray[x];" which is what is throwing me off. I wrote my pseudocode differently but he told me to use this method.

Jon - I will try rewriting it here in a. Minute. Also I think we are moving into objects next week. Some examples I have seen used objects which makes way more sense to me.

I have tried

 

itemCost=new double [10];




And it is giving multiple error that item Cost cannot be resolved to a variable.

This post has been edited by dthax: 08 November 2015 - 02:56 PM

Was This Post Helpful? 0
  • +
  • -

#5 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 693
  • View blog
  • Posts: 5,303
  • Joined: 25-December 13

Re: Parallel Arrays

Posted 08 November 2015 - 03:24 PM

Have you seen the tutorial: http://docs.oracle.c...lts/arrays.html
Was This Post Helpful? 0
  • +
  • -

#6 dthax   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 15-October 15

Re: Parallel Arrays

Posted 08 November 2015 - 03:26 PM

Hello there,
I sure have. Wasn't helpful in comparison to what my teacher is specifically asking.
Was This Post Helpful? 0
  • +
  • -

#7 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 693
  • View blog
  • Posts: 5,303
  • Joined: 25-December 13

Re: Parallel Arrays

Posted 08 November 2015 - 03:31 PM

Quote

what my teacher is specifically asking.

Can you explain?
Was This Post Helpful? 0
  • +
  • -

#8 dthax   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 15-October 15

Re: Parallel Arrays

Posted 08 November 2015 - 03:43 PM

Absolutely!

So in his example pseudocode he has

Declaration
Double listCost[10];
String listName[10];
... Other stuff..


In my pseudocode he said that my array has to be initialized under my declartion as "type myArray[x];" That arrays have very distinctive format but I have seen a few different types through the Web and my book and none have matched what he is asking.

When I write my array as he did above, Dr Java tells me to delete the 10. When I put itemCost=new double[10]; it say that itemCost cannot be resolved to a variable.

PS. I will not be using Dr Java after this class. It's the one he wanted.
Was This Post Helpful? 0
  • +
  • -

#9 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 693
  • View blog
  • Posts: 5,303
  • Joined: 25-December 13

Re: Parallel Arrays

Posted 08 November 2015 - 03:49 PM

Quote

array has to be initialized under my declartion as "type myArray[x];"

The tutorial says:
  // this form is discouraged
  float anArrayOfFloats[];


It recommends this way:
     // declares an array of integers
        int[] anArray;

In the above the type is int[].

        // allocates memory for 10 integers
        anArray = new int[10];

The above gives the the variable: anArray a value; a 10 element in array.


It also shows how to initialize an array:
    // initialize first element
        anArray[0] = 100;

And also this way which declares the variable and assigns it a value in one statement:
  char[] copyFrom = { 'd', 'e', 'c', 'a', 'f', 'f', 'e',
			    'i', 'n', 'a', 't', 'e', 'd' };

This post has been edited by NormR: 08 November 2015 - 03:56 PM

Was This Post Helpful? 0
  • +
  • -

#10 dthax   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 15-October 15

Re: Parallel Arrays

Posted 08 November 2015 - 03:54 PM

So if I change it to

 

ItemCost[0] =10;



It still gives the error that "itemCost cannot be resolved to a variable"

This post has been edited by dthax: 08 November 2015 - 03:56 PM

Was This Post Helpful? 0
  • +
  • -

#11 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 693
  • View blog
  • Posts: 5,303
  • Joined: 25-December 13

Re: Parallel Arrays

Posted 08 November 2015 - 03:59 PM

Quote

itemCost cannot be resolved to a variable"

Where is ItemCost declared(defined)? It needs to be defined as an array and give a value before its first element can be given a value.

This post has been edited by NormR: 08 November 2015 - 04:00 PM

Was This Post Helpful? 0
  • +
  • -

#12 dthax   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 15-October 15

Re: Parallel Arrays

Posted 08 November 2015 - 04:04 PM

View PostNormR, on 08 November 2015 - 03:59 PM, said:

Quote

itemCost cannot be resolved to a variable"

Where is ItemCost declared(defined)? It needs to be defined as an array and give a value before its first element can be given a value.



Can you explain a little more or give me an example?
Was This Post Helpful? 0
  • +
  • -

#13 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 693
  • View blog
  • Posts: 5,303
  • Joined: 25-December 13

Re: Parallel Arrays

Posted 08 November 2015 - 04:10 PM

I've been copying from the tutorial. It should be there.
Read the tutorial again. Copy any code from there that you don't understand and paste it here with your questions about it.

Your code in post#10 is the same as the 4th example in post#9.

This post has been edited by NormR: 08 November 2015 - 04:12 PM

Was This Post Helpful? 0
  • +
  • -

#14 dthax   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 15-October 15

Re: Parallel Arrays

Posted 08 November 2015 - 04:26 PM

I understand the tutorial and every one that I have seen hut again they do not match what my teacher has set above or they way he wants me to do it.
Was This Post Helpful? 0
  • +
  • -

#15 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3550
  • View blog
  • Posts: 16,190
  • Joined: 20-September 08

Re: Parallel Arrays

Posted 08 November 2015 - 04:30 PM

Quote

So in his example pseudocode he has

Declaration
Double listCost[10];
String listName[10];


So, either you have misunderstood him or he doesn't understand Java, since that's not valid array declaration code
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3