1,105,625 Community Members

Passing Array to Method

Member Avatar
sandawg
Newbie Poster
13 posts since Aug 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

First I apologizeif the answer to my question is somewhere in the hundreds of posts listed. I am new to java and basically clueless. I am suppose to pass an array to a method and then sort the array. I get an "illegal start of expression error when attempting to compile the code. Can/will someone please help? Here is my code:

import java.util.*;
import java.io.*;

public class Inventory
{

 public static void main(String args[])throws IOException

 {

String invArray[] = {"BDVD", "HDVD", "Music", "1220", "1367", "2903", "15", "25", "18", "19.95", "15.79", "13.99"};

double totInventory1;
double totInventory2;
double totInventory3;
double totalInventory;

totInventory1 = 0;
totInventory2 = 0;
totInventory3 = 0;
totalInventory = 0;

String productName1 = invArray[0];

int productNumber1 = Integer.parseInt(invArray[3]);

int inStock1 = Integer.parseInt(invArray[6]);

double unitPrice1 = Double.valueOf(invArray[9].trim()).doubleValue();

String productName2 = invArray[1];

int productNumber2 = Integer.parseInt(invArray[4]);

int inStock2 = Integer.parseInt(invArray[7]);

double unitPrice2 = Double.valueOf(invArray[10].trim()).doubleValue();

String productName3 = invArray[2];

int productNumber3 = Integer.parseInt(invArray[5]);

int inStock3 = Integer.parseInt(invArray[8]);

double unitPrice3 = Double.valueOf(invArray[11].trim()).doubleValue();

totInventory1 = (inStock1 * unitPrice1);
totInventory2 = (inStock2 * unitPrice2);
totInventory3 = (inStock3 * unitPrice3);

Product prod = new Product(totInventory1, totInventory2, totInventory3);
totalInventory = prod.gettInventory();

//Method Sort Array
modifyArray(invArray);
{

System.out.printf( "%2s%s%13s%16s%12s%12s%16s\n", " ", "Item Name",
"Item Number", "Items on Hand", "Item Price", "Item Value", "Total Value");

int counter =0;

System.out.printf( "%d%8s%10d%16d%16.2f%12.2f\n", (counter +1), productName1,
productNumber1, inStock1, unitPrice1, totInventory1);

System.out.printf( "%d%8s%10d%16d%16.2f%12.2f\n", (counter +2), productName2,
productNumber2, inStock2, unitPrice2, totInventory2);

System.out.printf( "%d%8s%10d%16d%16.2f%12.2f\n", (counter +3), productName3,
productNumber3, inStock3, unitPrice3, totInventory3);

System.out.println();

  System.out.printf("%70s$%.2f\n", "Value of total inventory:  ", totalInventory);

public static void modifyArray( String invArray2 [] )
//void modifyArray( String b [] )
{
    Arrays.sort(invArray2);
}
}
Member Avatar
stultuske
Posting Expert
5,326 posts since Jan 2007
Reputation Points: 938 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

first, use code tags next time, it's easier to read
second, paste the entire error, it includes the line where the error occurs. also, if you want us to test anything, provide everything needed, like the product class.

I'll paste the improved code here, so you can check for yourself, the problem was, you're not closing your main method.

import java.util.*;
import java.io.*;

public class Inventory
{

public static void main(String args[])//throws IOException
// don't throw an Exception here, the main method is
//the last place where it can be caught

{

String invArray[] = {"BDVD", "HDVD", "Music", "1220", "1367", "2903", "15", "25", "18", "19.95", "15.79", "13.99"};

// assign the value here, it makes it easier to read
double totInventory1 = 0;
double totInventory2 = 0;
double totInventory3 = 0;
double totalInventory = 0;

String productName1 = invArray[0];

int productNumber1 = Integer.parseInt(invArray[3]);

int inStock1 = Integer.parseInt(invArray[6]);

double unitPrice1 = Double.valueOf(invArray[9].trim()).doubleValue();

String productName2 = invArray[1];

int productNumber2 = Integer.parseInt(invArray[4]);

int inStock2 = Integer.parseInt(invArray[7]);

double unitPrice2 = Double.valueOf(invArray[10].trim()).doubleValue();

String productName3 = invArray[2];

int productNumber3 = Integer.parseInt(invArray[5]);

int inStock3 = Integer.parseInt(invArray[8]);

double unitPrice3 = Double.valueOf(invArray[11].trim()).doubleValue();

totInventory1 = (inStock1 * unitPrice1);
totInventory2 = (inStock2 * unitPrice2);
totInventory3 = (inStock3 * unitPrice3);

Product prod = new Product(totInventory1, totInventory2, totInventory3);
totalInventory = prod.gettInventory();

//Method Sort Array
modifyArray(invArray);
// {
// I have no idea what that is supposed to do here
System.out.printf( "%2s%s%13s%16s%12s%12s%16s\n", " ", "Item Name",
"Item Number", "Items on Hand", "Item Price", "Item Value", "Total Value");

int counter =0;

System.out.printf( "%d%8s%10d%16d%16.2f%12.2f\n", (counter +1), productName1,
productNumber1, inStock1, unitPrice1, totInventory1);

System.out.printf( "%d%8s%10d%16d%16.2f%12.2f\n", (counter +2), productName2,
productNumber2, inStock2, unitPrice2, totInventory2);

System.out.printf( "%d%8s%10d%16d%16.2f%12.2f\n", (counter +3), productName3,
productNumber3, inStock3, unitPrice3, totInventory3);

System.out.println();

System.out.printf("%70s$%.2f\n", "Value of total inventory: ", totalInventory);
}
// most important: close your main method
public static void modifyArray( String invArray2 [] )
//void modifyArray( String b [] )
{
Arrays.sort(invArray2);
}
}
Member Avatar
quuba
Posting Pro
573 posts since Nov 2008
Reputation Points: 81 [?]
Q&As Helped to Solve: 107 [?]
Skill Endorsements: 0 [?]
 
0
 

hello sandawg

public class Inventory2 {

    static class Group {

        private int currentIndex = 0;
        private Element[] elements;

        Group(int itemsTotal) {
            elements = new Element[itemsTotal];
        }

        void add(Element el) {
            elements[currentIndex] = el;
            currentIndex++;
        }

        double getValueOfTotalInventory() {
            double sum = 0;
            for (int i = 0; i < currentIndex; i++) {
                sum += elements[i].getItemValue();
            }
            return sum;
        }

        void printHeader() {
            System.out.println(Element.getHeader());
        }

        void printRecords() {
            for (int i = 0; i < currentIndex; i++) {
                System.out.println(elements[i].getData());
            }
        }

        void printSum() {
            System.out.println("Value of total inventory: $" + getValueOfTotalInventory());
        }

        void printAll() {
            printHeader();
            printRecords();
            printSum();
        }
    }

    static class Element {

        private String productNumber;
        private int inStock;
        private int items;
        private double unitPrice;

        Element(String[] el) {
            productNumber = el[0];
            inStock = Integer.parseInt(el[1]);
            items = Integer.parseInt(el[2]);
            unitPrice = Double.parseDouble(el[3]);
        }

        double getItemValue() {
            return items * unitPrice;
        }

        public static String getHeader() {
            StringBuilder sb = new StringBuilder();
            sb.append("productNumber");
            sb.append("\t");
            sb.append("inStock");
            sb.append("\t");
            sb.append("items");
            sb.append("\t");
            sb.append("unitPrice");
            sb.append("\t");
            sb.append("itemValue");
            return sb.toString();
        }

        public String getData() {
            StringBuilder sb = new StringBuilder();
            sb.append(productNumber);
            sb.append("\t");
            sb.append(inStock);
            sb.append("\t");
            sb.append(items);
            sb.append("\t");
            sb.append(unitPrice);
            sb.append("\t");
            sb.append(getItemValue());
            return sb.toString();
        }
    }

    public static void main(String args[]) {
        //changed form of record
        String invArray[][] = {{"BDVD", "1220", "15", "19.95"},
            {"HDVD", "1367", "25", "15.79"},
            {"Music", "2903", "18", "13.99"}};
        int invArrayLength = invArray.length;
        Group group = new Group(invArray.length);
        //adding records
        for (int i = 0; i < invArrayLength; i++) {
            group.add(new Element(invArray[i]));
        }
        //report
        group.printAll();
    }
}

Java is an abstract high-level language.Use this possibility. Decompose problem to logical classes.
quuba

Member Avatar
sandawg
Newbie Poster
13 posts since Aug 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks for your help. I will have to carefully study your code to be able to fully understand it as much of it is well beyond where my knowledge is at this particular time.

Member Avatar
sandawg
Newbie Poster
13 posts since Aug 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thank you for your response and I did as suggested and it indeed got me going.

Question Answered as of 5 Years Ago by stultuske and quuba
Member Avatar
Ashok Agarwal
Newbie Poster
1 post since Sep 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Can any one plz help me How a arraylist can be passed in a constructor?

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article