array and method help


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: array and method help

Hybrid View

  1. #1
    Join Date
    Mar 2005
    Posts
    3

    array and method help

    I am totally new to Java...I compiled my menu driven program and I received the following error message:java:58: ';' expected
    int add_items(String []number,String []name, double []cost, int []level, int count1)
    ^
    Any ideas on what is wrong and how to fix it would be greatly appreciated.




    import javax.swing.JOptionPane;
    public class method4
    {
    public static void main(String[] args)
    {
    String[] employee_id = new String[100];
    String[] employee_name = new String[100];
    double[] employee_salary = new double[100];
    int[] salary_level = new int[100];
    int[] employee_pay = new int[100];
    menu();


    }

    public static void menu()
    {// open menu
    String svalue;
    String output= " how much for you" + "\n" +
    "1. Modify Employee Information" + "\n" +
    "2. Add/Modify Employee Information" + "\n" +
    "3. Report Section" + "\n" +
    "4. Exit the System" + "\n" + "\n" +
    "Please make your selection> ";
    svalue =JOptionPane.showInputDialog(null,
    output," ",JOptionPane.QUESTION_MESSAGE);

    }//end menu method

    public static String modify_inventory()

    {//open modify
    String svalue;
    int i;
    String output= " Modify Employee" + "\n" +
    "1. Add employee" + "\n" +
    "2. Modify employee" + "\n" +
    "3. Delete employee" + "\n" +
    "4. Exit Modify Employee" + "\n" + "\n" +
    "Please make your selection> ";
    svalue =JOptionPane.showInputDialog(null,
    output," ",JOptionPane.QUESTION_MESSAGE);


    // Convert string to intger

    int b=Integer.parseInt(svalue);


    //menu options

    if(b == 1)


    {//opening if

    //add items adds items to db
    int add_items(String []id,String []name, double []salary, int []level, int count1)
    {// open add items
    int contn;
    String scontn, svalue;
    //items will be added until told to stop
    String output = "Enter 1 to continue and 0 to stop";
    scont = JOptionPane.showInputDialog(null, output, " ",JOptionPane.QUESTION_MESSAGE);
    contn = Integer.parseInt(scont);
    while (contn != 0)
    {//items added
    //items are added to the arrays
    count1 = count1 + 1;
    output = "Enter the Employee id Number";
    number[count1] = JOptionPane.showInputDialog(null, output, " ",JOptionPane.QUESTION_MESSAGE);
    output = "Enter the employee Name";
    name[count1] = JOptionPane.showInputDialog(null, output, " ",JOptionPane.QUESTION_MESSAGE);
    output = "Enter the salary";
    svalue = JOptionPane.showInputDialog(null, output, " ",JOptionPane.QUESTION_MESSAGE);
    cost[count1] = Double.parseDouble(svalue);
    output = "Enter the salary Level";
    svalue = JOptionPane.showInputDialog(null, output, " ",JOptionPane.QUESTION_MESSAGE);
    level[count1] = Integer.parseInt(svalue);
    output = "Enter 1 to continue and 0 to stop";
    scont = JOptionPane.showInputDialog(null, output, " ",JOptionPane.QUESTION_MESSAGE);
    cont = Integer.parseInt(scont);
    }//close items added
    return count1;
    }//close add employee

    }// close b=1
    else if (b == 2){

    String modifyString =JOptionPane.showInputDialog(null,

    "enter employee number","employee data",JOptionPane.QUESTION_MESSAGE);

    }else if (b == 3){


    String deleteString =JOptionPane.showInputDialog(null,

    "enter employee number","inventory data",JOptionPane.QUESTION_MESSAGE);
    }

    return svalue;



    }// end modify employee data



    public static void modify_employee()
    {
    String output;
    System.out.println(" employee has been added/modified");
    }

    public static void report()
    {
    String output;
    System.out.println(" Your reort has been printed");
    System.exit(0);}
    }

  2. #2
    Join Date
    Feb 2004
    Posts
    541
    The problem is that there seems to have been some misunderstanding about Java syntax when you wrote that code. The error message it gave you means what it says, it expected to find a semi-colon at the end of line 58 but it didn't find one. It therefore doesn't know what the code means because it isn't legal Java...

    Line 58 reads...
    Code:
    int add_items(String []number,String []name, double []cost, int []level, int count1)
    That line is a method definition, but without a method body. The problem with syntax that I mentioned is that this line is contained within the method modify_inventory(). In Java you can't define one method inside another, so your line 58 is wrong. Your comment above suggests that you were attempting to add a record to a database, so you must have been trying to call a method, not define one.

    I'm not sure how much Java you know, so I don't know if you'll understand what I wrote or not. Modifying a database certainly isn't a basic exercise in programming though. I wouldn't have thought it would have been given to someone totally new to Java.

  3. #3
    Join Date
    Mar 2005
    Posts
    3
    Thank you mike for responding....could you please give me an example of what it should look. I am not exactly sure what you are talking about? I'm not writing to a db I just put that in there as a comment, sorry about that. I probably threw you off!

  4. #4
    Join Date
    Feb 2004
    Posts
    541
    Ok, this is the method that is defined in the wrong place...

    Code:
    int add_items(String []id,String []name, double []salary, int []level, int count1)
    {// open add items
        int contn;
        String scontn, svalue;
        //items will be added until told to stop
        String output = "Enter 1 to continue and 0 to stop";
        scont = JOptionPane.showInputDialog(null, output, " ",JOptionPane.QUESTION_MESSAGE);
        contn = Integer.parseInt(scont);
        while (contn != 0)
        {//items added
            //items are added to the arrays 
            count1 = count1 + 1;
            output = "Enter the Employee id Number";
            number[count1] = JOptionPane.showInputDialog(null, output, " ",JOptionPane.QUESTION_MESSAGE);
            output = "Enter the employee Name";
            name[count1] = JOptionPane.showInputDialog(null, output, " ",JOptionPane.QUESTION_MESSAGE);
            output = "Enter the salary";
            svalue = JOptionPane.showInputDialog(null, output, " ",JOptionPane.QUESTION_MESSAGE);
            cost[count1] = Double.parseDouble(svalue);
            output = "Enter the salary Level";
            svalue = JOptionPane.showInputDialog(null, output, " ",JOptionPane.QUESTION_MESSAGE);
            level[count1] = Integer.parseInt(svalue);
            output = "Enter 1 to continue and 0 to stop";
            scont = JOptionPane.showInputDialog(null, output, " ",JOptionPane.QUESTION_MESSAGE);
            cont = Integer.parseInt(scont);
        }//close items added
        return count1;
    }//close add employee
    This needs to be moved to class level like all the other methods. By class level I mean that the only curly brackets enclosing it are the ones that define the class (the opening bracket and the last closing bracket).

    Inside the method modify_inventory() you need to call the method add_items() instead of defining it....so it'll look something like this.

    Code:
    import javax.swing.JOptionPane;
    public class method4
    {
    public static void main(String[] args)
    {
    String[] employee_id = new String[100];
    String[] employee_name = new String[100];
    double[] employee_salary = new double[100];
    int[] salary_level = new int[100];
    int[] employee_pay = new int[100];
    menu();
    
    
    }
    
    int add_items(String []id,String []name, double []salary, int []level, int count1)
    {// open add items
    int contn;
    String scontn, svalue;
    //items will be added until told to stop
    String output = "Enter 1 to continue and 0 to stop";
    scont = JOptionPane.showInputDialog(null, output, " ",JOptionPane.QUESTION_MESSAGE);
    contn = Integer.parseInt(scont);
    while (contn != 0)
    {//items added
    //items are added to the arrays 
    count1 = count1 + 1;
    output = "Enter the Employee id Number";
    number[count1] = JOptionPane.showInputDialog(null, output, " ",JOptionPane.QUESTION_MESSAGE);
    output = "Enter the employee Name";
    name[count1] = JOptionPane.showInputDialog(null, output, " ",JOptionPane.QUESTION_MESSAGE);
    output = "Enter the salary";
    svalue = JOptionPane.showInputDialog(null, output, " ",JOptionPane.QUESTION_MESSAGE);
    cost[count1] = Double.parseDouble(svalue);
    output = "Enter the salary Level";
    svalue = JOptionPane.showInputDialog(null, output, " ",JOptionPane.QUESTION_MESSAGE);
    level[count1] = Integer.parseInt(svalue);
    output = "Enter 1 to continue and 0 to stop";
    scont = JOptionPane.showInputDialog(null, output, " ",JOptionPane.QUESTION_MESSAGE);
    cont = Integer.parseInt(scont);
    }//close items added
    return count1;
    }//close add employee
    
    public static void menu()
    {// open menu
    String svalue;
    String output= " how much for you" + "\n" +
    "1. Modify Employee Information" + "\n" +
    "2. Add/Modify Employee Information" + "\n" +
    "3. Report Section" + "\n" +
    "4. Exit the System" + "\n" + "\n" +
    "Please make your selection> ";
    svalue =JOptionPane.showInputDialog(null,
    output," ",JOptionPane.QUESTION_MESSAGE);
    
    }//end menu method
    
    public static String modify_inventory()
    
    {//open modify
    String svalue;
    int i;
    String output= " Modify Employee" + "\n" +
    "1. Add employee" + "\n" +
    "2. Modify employee" + "\n" +
    "3. Delete employee" + "\n" +
    "4. Exit Modify Employee" + "\n" + "\n" +
    "Please make your selection> ";
    svalue =JOptionPane.showInputDialog(null,
    output," ",JOptionPane.QUESTION_MESSAGE);
    
    
    // Convert string to intger
    
    int b=Integer.parseInt(svalue);
    
    
    //menu options
    
    if(b == 1)
    
    
    {//opening if
    
    //add items adds items to db
    
    add_items(YOU NEED TO PUT THE RIGHT PARAMETERS IN HERE);
    
    }// close b=1
    else if (b == 2){
    
    String modifyString =JOptionPane.showInputDialog(null,
    
    "enter employee number","employee data",JOptionPane.QUESTION_MESSAGE);
    
    }else if (b == 3){
    
    
    String deleteString =JOptionPane.showInputDialog(null,
    
    "enter employee number","inventory data",JOptionPane.QUESTION_MESSAGE);
    }
    
    return svalue;
    
    
    
    }// end modify employee data
    
    
    
    public static void modify_employee()
    {
    String output;
    System.out.println(" employee has been added/modified");
    }
    
    public static void report()
    {
    String output;
    System.out.println(" Your reort has been printed");
    System.exit(0);}
    }
    What i've posted above definately won't compile because I didn't put the parameters in the method call. I wrote it in capitals so you can see where it is. The parameters you need are the ones that you defined in the method definition.

    I didn't check the rest of the code so I don't know if there are any other problems in it. It may still not compile even when you put the right paramters in the method. The error messages you get will point you to exactly what is wrong though.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center