Passing Array to Method

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2008
Posts: 13
Reputation: sandawg is an unknown quantity at this point 
Solved Threads: 0
sandawg sandawg is offline Offline
Newbie Poster

Passing Array to Method

 
0
  #1
Nov 20th, 2008
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);
}
}
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: Passing Array to Method

 
0
  #2
Nov 21st, 2008
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.

  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class Inventory
  5. {
  6.  
  7. public static void main(String args[])//throws IOException
  8. // don't throw an Exception here, the main method is
  9. //the last place where it can be caught
  10.  
  11. {
  12.  
  13. String invArray[] = {"BDVD", "HDVD", "Music", "1220", "1367", "2903", "15", "25", "18", "19.95", "15.79", "13.99"};
  14.  
  15. // assign the value here, it makes it easier to read
  16. double totInventory1 = 0;
  17. double totInventory2 = 0;
  18. double totInventory3 = 0;
  19. double totalInventory = 0;
  20.  
  21. String productName1 = invArray[0];
  22.  
  23. int productNumber1 = Integer.parseInt(invArray[3]);
  24.  
  25. int inStock1 = Integer.parseInt(invArray[6]);
  26.  
  27. double unitPrice1 = Double.valueOf(invArray[9].trim()).doubleValue();
  28.  
  29. String productName2 = invArray[1];
  30.  
  31. int productNumber2 = Integer.parseInt(invArray[4]);
  32.  
  33. int inStock2 = Integer.parseInt(invArray[7]);
  34.  
  35. double unitPrice2 = Double.valueOf(invArray[10].trim()).doubleValue();
  36.  
  37. String productName3 = invArray[2];
  38.  
  39. int productNumber3 = Integer.parseInt(invArray[5]);
  40.  
  41. int inStock3 = Integer.parseInt(invArray[8]);
  42.  
  43. double unitPrice3 = Double.valueOf(invArray[11].trim()).doubleValue();
  44.  
  45. totInventory1 = (inStock1 * unitPrice1);
  46. totInventory2 = (inStock2 * unitPrice2);
  47. totInventory3 = (inStock3 * unitPrice3);
  48.  
  49. Product prod = new Product(totInventory1, totInventory2, totInventory3);
  50. totalInventory = prod.gettInventory();
  51.  
  52. //Method Sort Array
  53. modifyArray(invArray);
  54. // {
  55. // I have no idea what that is supposed to do here
  56. System.out.printf( "%2s%s%13s%16s%12s%12s%16s\n", " ", "Item Name",
  57. "Item Number", "Items on Hand", "Item Price", "Item Value", "Total Value");
  58.  
  59. int counter =0;
  60.  
  61. System.out.printf( "%d%8s%10d%16d%16.2f%12.2f\n", (counter +1), productName1,
  62. productNumber1, inStock1, unitPrice1, totInventory1);
  63.  
  64. System.out.printf( "%d%8s%10d%16d%16.2f%12.2f\n", (counter +2), productName2,
  65. productNumber2, inStock2, unitPrice2, totInventory2);
  66.  
  67. System.out.printf( "%d%8s%10d%16d%16.2f%12.2f\n", (counter +3), productName3,
  68. productNumber3, inStock3, unitPrice3, totInventory3);
  69.  
  70. System.out.println();
  71.  
  72. System.out.printf("%70s$%.2f\n", "Value of total inventory: ", totalInventory);
  73. }
  74. // most important: close your main method
  75. public static void modifyArray( String invArray2 [] )
  76. //void modifyArray( String b [] )
  77. {
  78. Arrays.sort(invArray2);
  79. }
  80. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 54
quuba quuba is offline Offline
Posting Whiz

Re: Passing Array to Method

 
0
  #3
Nov 21st, 2008
hello sandawg
  1. public class Inventory2 {
  2.  
  3. static class Group {
  4.  
  5. private int currentIndex = 0;
  6. private Element[] elements;
  7.  
  8. Group(int itemsTotal) {
  9. elements = new Element[itemsTotal];
  10. }
  11.  
  12. void add(Element el) {
  13. elements[currentIndex] = el;
  14. currentIndex++;
  15. }
  16.  
  17. double getValueOfTotalInventory() {
  18. double sum = 0;
  19. for (int i = 0; i < currentIndex; i++) {
  20. sum += elements[i].getItemValue();
  21. }
  22. return sum;
  23. }
  24.  
  25. void printHeader() {
  26. System.out.println(Element.getHeader());
  27. }
  28.  
  29. void printRecords() {
  30. for (int i = 0; i < currentIndex; i++) {
  31. System.out.println(elements[i].getData());
  32. }
  33. }
  34.  
  35. void printSum() {
  36. System.out.println("Value of total inventory: $" + getValueOfTotalInventory());
  37. }
  38.  
  39. void printAll() {
  40. printHeader();
  41. printRecords();
  42. printSum();
  43. }
  44. }
  45.  
  46. static class Element {
  47.  
  48. private String productNumber;
  49. private int inStock;
  50. private int items;
  51. private double unitPrice;
  52.  
  53. Element(String[] el) {
  54. productNumber = el[0];
  55. inStock = Integer.parseInt(el[1]);
  56. items = Integer.parseInt(el[2]);
  57. unitPrice = Double.parseDouble(el[3]);
  58. }
  59.  
  60. double getItemValue() {
  61. return items * unitPrice;
  62. }
  63.  
  64. public static String getHeader() {
  65. StringBuilder sb = new StringBuilder();
  66. sb.append("productNumber");
  67. sb.append("\t");
  68. sb.append("inStock");
  69. sb.append("\t");
  70. sb.append("items");
  71. sb.append("\t");
  72. sb.append("unitPrice");
  73. sb.append("\t");
  74. sb.append("itemValue");
  75. return sb.toString();
  76. }
  77.  
  78. public String getData() {
  79. StringBuilder sb = new StringBuilder();
  80. sb.append(productNumber);
  81. sb.append("\t");
  82. sb.append(inStock);
  83. sb.append("\t");
  84. sb.append(items);
  85. sb.append("\t");
  86. sb.append(unitPrice);
  87. sb.append("\t");
  88. sb.append(getItemValue());
  89. return sb.toString();
  90. }
  91. }
  92.  
  93. public static void main(String args[]) {
  94. //changed form of record
  95. String invArray[][] = {{"BDVD", "1220", "15", "19.95"},
  96. {"HDVD", "1367", "25", "15.79"},
  97. {"Music", "2903", "18", "13.99"}};
  98. int invArrayLength = invArray.length;
  99. Group group = new Group(invArray.length);
  100. //adding records
  101. for (int i = 0; i < invArrayLength; i++) {
  102. group.add(new Element(invArray[i]));
  103. }
  104. //report
  105. group.printAll();
  106. }
  107. }
Java is an abstract high-level language.Use this possibility. Decompose problem to logical classes.
quuba
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 13
Reputation: sandawg is an unknown quantity at this point 
Solved Threads: 0
sandawg sandawg is offline Offline
Newbie Poster

Re: Passing Array to Method

 
0
  #4
Nov 22nd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 13
Reputation: sandawg is an unknown quantity at this point 
Solved Threads: 0
sandawg sandawg is offline Offline
Newbie Poster

Re: Passing Array to Method

 
0
  #5
Nov 22nd, 2008
Thank you for your response and I did as suggested and it indeed got me going.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC