| | |
Passing Array to Method
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Aug 2008
Posts: 13
Reputation:
Solved Threads: 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);
}
}
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);
}
}
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.
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.
Java Syntax (Toggle Plain Text)
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); } }
•
•
Join Date: Nov 2008
Posts: 332
Reputation:
Solved Threads: 54
hello sandawg
Java is an abstract high-level language.Use this possibility. Decompose problem to logical classes.
quuba
java Syntax (Toggle Plain Text)
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(); } }
quuba
![]() |
Similar Threads
- Reflection Invoke Method w/ Params (C#)
- passing array into a function (C#)
- Generic method parser / invoker (C#)
- Passing variables to and from PHP (JavaScript / DHTML / AJAX)
- help with parrallel arrays (C++)
- passing values from array to array (C)
- Passing a Function, function pointer (C++)
- passing arrays to classes? (Java)
- passing arrays in visual basic (Visual Basic 4 / 5 / 6)
Other Threads in the Java Forum
- Previous Thread: Merge sort problem.
- Next Thread: Using Scanner - I real need help!
| Thread Tools | Search this Thread |
Tag cloud for Java
affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth businessintelligence chat class classes client code component database desktop draw ebook eclipse encode equation error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer intersect j2me java javaexcel javaprojects jmf jni jpanel julia linked linux list loop mac main map method methods mobile netbeans newbie number object open-source oracle parameter print problem program programming project properties recursion reference replaysolutions rotatetext scanner score screen scrollbar server set size sms socket sort sql string superclass swing template test threads time tree windows working xstream





