Results 1 to 6 of 6
  1. #1
    b.netana is offline Member
    Join Date
    Oct 2011
    Posts
    3
    Rep Power
    0

    Default why am I getting these errors- Array

    Java Code:
    // Inventory.java part 2
    // this program is to calculate the value of the inventory of the Electronics Department's cameras
    
    import java.util.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.io.*;
    
    public class Inventory2
    {
       public static void main(String[] args)
       {
           // create Scanner to obtain input from the command window
    	   Scanner input = new Scanner (System.in);
    	   
    	   Cam[] array = new Cam[3];
              int nbCam = 0;
    	   
    	   boolean toContinue = true;
    	   String name = "";
    	   int itemNumber = 0; // first number to multiply
    	   int itemStock = 0; // second number to multiply
    	   double itemPrice = 0; //
    	   double totalValue; // product of number1 and number2
    
    	   
            while(toContinue){
                input = JOptionPane.showInputDialog("1) Add an item\n2) Modify an existing item\n3) Display inventory\n4) Exit");
                menuItem = Integer.parseInt(input);
                switch(menuItem){
                    case 1: //add an item
    
                        String name = JOptionPane.showInputDialog("Enter the name of the item");
                        int quantity = Integer.parseInt(JOptionPane.showInputDialog("Enter the quantity of the item"));
                        double price = Double.parseDouble(JOptionPane.showInputDialog("Enter the price"));
                        inventory[counter++] = new Item(name, quantity, price);    
                        break;
                         
                    case 2: //modify an existing item
                        int index = Integer.parseInt(JOptionPane.showInputDialog("Which item from 0-9 do you wish to modify?"));
                         
                        //notice how we just call the toString() method instead of having a line to set up the String display
                        int category = Integer.parseInt(JOptionPane.showInputDialog(inventory[index].toString() + "\nModify:\n1) Name\n2) Quantity\n3) Price"));
                        String value = JOptionPane.showInputDialog("Enter the new value");
                         
                        //notice how we use the update() method from the given Item and let it update its attributes internally
                        //rather than messing around with determining what parallel array to update and how to parse the values
                        inventory[index].update(category, value);
                        break;
    	                 
                    case 3: //display items
                        //notice how many fewer lines this takes up than iterating through each parallel array
                        //and appending the information to the display String
                        //Here, the toString() method comes in handy, plus having one array makes the task easier
                        //So we have about 4 lines of code for this part total
                        String display = "Name\tQuantity\tPrice\n";
                        for(Item i: inventory){
                                                if(i != null)
                                                      display += i.toString() + "\n";
                                            }
                        JOptionPane.showMessageDialog(null, display);
                        break;
    	                     
                    case 4: //exit
                        System.exit(0);
                }//end switch
    	         
            }//end while	    
    		}
    }
    
    class Cam{
    
       private String name;
       private int itemNumber;
       private int itemStock;
       private double itemPrice;
       private String deptName;
       
       public Cam(String name, int itemNumber, int itemStock, double itemPrice) {
    	  this.name = name;
    	  this.itemNumber = itemNumber;
    	  this.itemStock = itemStock;
    	  this.itemPrice = itemPrice;
       }
    
       public name(){
          this("New Name",0,0,0);
    	  }
       
       
    	  
       public void setName(String name){this.name = name;}{
       public String getName(){return name;}
    
       public void setItemNumber(int itemNumber){this.itemNumber = itemNumber;}{
       public int getItemNumber(){return itemNumber;}
       
       public void setItemStock(int itemStock){this.itemStock = itemStock;}{
       public int getItemStock(){return itemStock;}
       
       public void setItemPrice(double itemPrice){this.itemPrice = itemPrice;}{
       public double getItemPrice(){return itemPrice;}
    }
    
       public void update(int category, String value){
          switch(category){
    	     case 1:
    		    name = value;
    			return;
    		 case 2:
    		    //value converts to int
    		    itemNumber = Integer.parseInt(value);
    		    itemStock = Integer.parseInt(value);
    		    return;
    		 case 3:
    		    //value converts to double
    			itemPrice = Double.parseDouble(value);
    			return;
    	   }
    	}
    
    	public String toString(){
    	   return name + "\t" + itemNumber + "\t" + itemStock + "\t" + itemPrice;
    	   
       }
    	
    }
    C:\Java>javac Inventory2.java
    Inventory2.java:96: error: invalid method declaration; return type required
    public name(){
    ^
    Inventory2.java:103: error: illegal start of expression
    public String getName(){return name;}
    ^
    Inventory2.java:103: error: ';' expected
    public String getName(){return name;}
    ^
    Inventory2.java:105: error: illegal start of expression
    public void setItemNumber(int itemNumber){this.itemNumber = itemNumber;}{
    ^
    Inventory2.java:105: error: illegal start of expression
    public void setItemNumber(int itemNumber){this.itemNumber = itemNumber;}{
    ^
    Inventory2.java:105: error: ';' expected
    public void setItemNumber(int itemNumber){this.itemNumber = itemNumber;}{
    ^
    Inventory2.java:105: error: ';' expected
    public void setItemNumber(int itemNumber){this.itemNumber = itemNumber;}{
    ^
    Inventory2.java:106: error: illegal start of expression
    public int getItemNumber(){return itemNumber;}
    ^
    Inventory2.java:106: error: ';' expected
    public int getItemNumber(){return itemNumber;}
    ^
    Inventory2.java:108: error: illegal start of expression
    public void setItemStock(int itemStock){this.itemStock = itemStock;}{
    ^
    Inventory2.java:108: error: illegal start of expression
    public void setItemStock(int itemStock){this.itemStock = itemStock;}{
    ^
    Inventory2.java:108: error: ';' expected
    public void setItemStock(int itemStock){this.itemStock = itemStock;}{
    ^
    Inventory2.java:108: error: ';' expected
    public void setItemStock(int itemStock){this.itemStock = itemStock;}{
    ^
    Inventory2.java:109: error: illegal start of expression
    public int getItemStock(){return itemStock;}
    ^
    Inventory2.java:109: error: ';' expected
    public int getItemStock(){return itemStock;}
    ^
    Inventory2.java:111: error: illegal start of expression
    public void setItemPrice(double itemPrice){this.itemPrice = itemPrice;}{
    ^
    Inventory2.java:111: error: illegal start of expression
    public void setItemPrice(double itemPrice){this.itemPrice = itemPrice;}{
    ^
    Inventory2.java:111: error: ';' expected
    public void setItemPrice(double itemPrice){this.itemPrice = itemPrice;}{
    ^
    Inventory2.java:111: error: ';' expected
    public void setItemPrice(double itemPrice){this.itemPrice = itemPrice;}{
    ^
    Inventory2.java:112: error: illegal start of expression
    public double getItemPrice(){return itemPrice;}
    ^
    Inventory2.java:112: error: ';' expected
    public double getItemPrice(){return itemPrice;}
    ^
    Inventory2.java:115: error: illegal start of expression
    public void update(int category, String value){
    ^
    Inventory2.java:115: error: illegal start of expression
    public void update(int category, String value){
    ^
    Inventory2.java:115: error: ';' expected
    public void update(int category, String value){
    ^
    Inventory2.java:115: error: ';' expected
    public void update(int category, String value){
    ^
    Inventory2.java:115: error: not a statement
    public void update(int category, String value){
    ^
    Inventory2.java:115: error: ';' expected
    public void update(int category, String value){
    ^
    Inventory2.java:132: error: illegal start of expression
    public String toString(){
    ^
    Inventory2.java:132: error: ';' expected
    public String toString(){
    ^
    Inventory2.java:137: error: reached end of file while parsing
    }
    ^
    30 errors

    I don't understand why I am getting these errors, can someone guide me to the answer?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    28

    Default Re: why am I getting these errors- Array

    Check your curly braces because you have too many and in the wrong spot. Remember that methods cannot have their bodies within other methods.

  3. #3
    b.netana is offline Member
    Join Date
    Oct 2011
    Posts
    3
    Rep Power
    0

    Default Re: why am I getting these errors- Array

    C:\Java>javac Inventory2.java
    Inventory2.java:87: error: invalid method declaration; return type required
    public name(){
    ^
    Inventory2.java:94: error: illegal start of expression
    public String getName(){return name;}}
    ^
    Inventory2.java:94: error: ';' expected
    public String getName(){return name;}}
    ^
    Inventory2.java:97: error: illegal start of expression
    public int getItemNumber(){return itemNumber;}}
    ^
    Inventory2.java:97: error: ';' expected
    public int getItemNumber(){return itemNumber;}}
    ^
    Inventory2.java:100: error: illegal start of expression
    public int getItemStock(){return itemStock;}}
    ^
    Inventory2.java:100: error: ';' expected
    public int getItemStock(){return itemStock;}}
    ^
    Inventory2.java:103: error: illegal start of expression
    public double getItemPrice(){return itemPrice;}}
    ^
    Inventory2.java:103: error: ';' expected
    public double getItemPrice(){return itemPrice;}}
    ^
    9 errors

    okay I've fixed that, but why do these methods require a semicolon?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,271
    Blog Entries
    7
    Rep Power
    24

    Default Re: why am I getting these errors- Array

    I see one left curly bracket for two right curly brackets all over the place; that confuses the compiler; pay special attention to its first error message and read it carefully; programming can't be done in a sloppy way.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  5. #5
    b.netana is offline Member
    Join Date
    Oct 2011
    Posts
    3
    Rep Power
    0

    Default Re: why am I getting these errors- Array

    Java Code:
    // Inventory.java part 2
    // this program is to calculate the value of the inventory of the Electronics Department's cameras
    
    import java.util.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.io.*;
    
    public class Inventory2
    {
       public static void main(String[] args)
       {
           // create Scanner to obtain input from the command window
    	   Scanner input = new Scanner (System.in);
    	   
    	   Cam[] array = new Cam[3];
              int nbCam = 0;
    	   
    	   boolean toContinue = true;
    	   String name = "";
    	   int itemNumber = 0; // first number to multiply
    	   int itemStock = 0; // second number to multiply
    	   double itemPrice = 0; //
    	   double totalValue; // product of number1 and number2
    
    	   
            while(toContinue){
                input = JOptionPane.showInputDialog("1) Add an item\n2) Modify an existing item\n3) Display inventory\n4) Exit");
                menuItem = Integer.parseInt(input);
                switch(menuItem){
                    case 1: //add an item
    
                        String name = JOptionPane.showInputDialog("Enter the name of the item");
                        int quantity = Integer.parseInt(JOptionPane.showInputDialog("Enter the quantity of the item"));
                        double price = Double.parseDouble(JOptionPane.showInputDialog("Enter the price"));
                        inventory[counter++] = new Item(name, quantity, price);    
                        break;
                         
                    case 2: //modify an existing item
                        int index = Integer.parseInt(JOptionPane.showInputDialog("Which item from 0-9 do you wish to modify?"));
                         
                        //notice how we just call the toString() method instead of having a line to set up the String display
                        int category = Integer.parseInt(JOptionPane.showInputDialog(inventory[index].toString() + "\nModify:\n1) Name\n2) Quantity\n3) Price"));
                        String value = JOptionPane.showInputDialog("Enter the new value");
                         
                        //notice how we use the update() method from the given Item and let it update its attributes internally
                        //rather than messing around with determining what parallel array to update and how to parse the values
                        inventory[index].update(category, value);
                        break;
    	                 
                    case 3: //display items
                        //notice how many fewer lines this takes up than iterating through each parallel array
                        //and appending the information to the display String
                        //Here, the toString() method comes in handy, plus having one array makes the task easier
                        //So we have about 4 lines of code for this part total
                        String display = "Name\tQuantity\tPrice\n";
                        for(Item i: inventory){
                                                if(i != null)
                                                      display += i.toString() + "\n";
                                            }
                        JOptionPane.showMessageDialog(null, display);
                        break;
    	                     
                    case 4: //exit
                        System.exit(0);
                }//end switch
    	         
            }//end while	    
    		}
    }
    
    class Cam{
    
       private String name;
       private int itemNumber;
       private int itemStock;
       private double itemPrice;
       private String deptName;
       
       public Cam(String name, int itemNumber, int itemStock, double itemPrice) {
    	  this.name = name;
    	  this.itemNumber = itemNumber;
    	  this.itemStock = itemStock;
    	  this.itemPrice = itemPrice;
       }
    
       public name(){
          this("New Name",0,0,0);
    	  }
       
       
    	  
       public void setName(String name){this.name = name;}{
       public String getName(){return name;}}
    
       public void setItemNumber(int itemNumber){this.itemNumber = itemNumber;}{
       public int getItemNumber(){return itemNumber;}}
       
       public void setItemStock(int itemStock){this.itemStock = itemStock;}{
       public int getItemStock(){return itemStock;}}
       
       public void setItemPrice(double itemPrice){this.itemPrice = itemPrice;}{
       public double getItemPrice(){return itemPrice;}}
    
    
       public void update(int category, String value){
          switch(category){
    	     case 1:
    		    name = value;
    			return;
    		 case 2:
    		    //value converts to int
    		    itemNumber = Integer.parseInt(value);
    		    return;
    		 case 3:
    		    itemStock = Integer.parseInt(value);
    		    return;
    		 case 4:
    		 //value converts to double
    			itemPrice = Double.parseDouble(value);
    			return;
    	   }
    	}
    
    	public String toString(){
    	   return name + "\t" + itemNumber + "\t" + itemStock + "\t" + itemPrice;
    	   
       }
    	
    }
    Inventory2.java:87: error: invalid method declaration; return type required
    public name(){
    ^
    Inventory2.java:94: error: illegal start of expression
    public String getName(){return name;}}
    ^
    Inventory2.java:94: error: ';' expected
    public String getName(){return name;}}
    ^
    Inventory2.java:97: error: illegal start of expression
    public int getItemNumber(){return itemNumber;}}
    ^
    Inventory2.java:97: error: ';' expected
    public int getItemNumber(){return itemNumber;}}
    ^
    Inventory2.java:100: error: illegal start of expression
    public int getItemStock(){return itemStock;}}
    ^
    Inventory2.java:100: error: ';' expected
    public int getItemStock(){return itemStock;}}
    ^
    Inventory2.java:103: error: illegal start of expression
    public double getItemPrice(){return itemPrice;}}
    ^
    Inventory2.java:103: error: ';' expected
    public double getItemPrice(){return itemPrice;}}
    ^
    9 errors

    I understand what you are saying, but perhaps the misunderstanding is coming from not having the errors to match the updated code.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,271
    Blog Entries
    7
    Rep Power
    24

    Default Re: why am I getting these errors- Array

    Things like this:

    Java Code:
    public void setName(String name){this.name = name;}{   
    public String getName(){return name;}}
    ... really don't make any sense at all (syntactically); I see those things all over the place; fix them.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Gui with errors
    By scooter24 in forum New To Java
    Replies: 2
    Last Post: 07-04-2011, 05:29 PM
  2. Replies: 4
    Last Post: 01-04-2011, 01:36 AM
  3. Errors,errors and errors
    By xpatiencex in forum New To Java
    Replies: 6
    Last Post: 04-25-2009, 04:43 AM
  4. Replies: 3
    Last Post: 03-08-2009, 02:51 AM
  5. Replies: 22
    Last Post: 01-05-2009, 02:03 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •