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

Recommended Answers

All 5 Replies

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

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

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.

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.