Results 1 to 2 of 2
  1. #1
    rednessc is offline Member
    Join Date
    Dec 2007
    Posts
    1
    Rep Power
    0

    Unhappy problems with asigning elements of an array to a constructor

    I have created an array which holds data that is entered by a user using JOptionPane.

    I would now like to create an object of a class called Module that was previously created. But the program says that the constructor module() cannot be found. Can somebody help because I need that object to be declared so that I can display the data that is entered by the user with the use of System.out.print(). Below is the code for the Module class:

    public class Module

    {
    int level, finalMark ;
    String moduleCode ;

    Module(String mc, int l, int fm)
    {
    moduleCode = mc;
    level = l;
    finalMark = fm;
    }

    public String toString()
    {
    return level + " " + moduleCode + " " + finalMark ;
    }

    } // end class Module

    Now this is the start of the code for class ModuleTest, where I am stuck. Like explained before, all I need is for the details entered by the user to be stored in an object in order to then display them:

    import javax.swing.* ;

    public class ModuleTest
    {

    public static void main ( String [] args )
    {
    int size ;
    size = 3 ;
    int level=0 ;
    int finalMark ;

    String moduleCode ;
    String levelStr ;
    String finalMarkStr ;

    Module [] moduleInfo;
    moduleInfo = new Module[size] ;

    System.out.printf( "%s %8s %8s\n", "Level" , "ModuleCode", "FinalMark" ) ;
    System.out.println( "------------------------------" ) ;

    for (int i = 0 ; i < (moduleInfo.length - 1) ; i++ )
    {
    levelStr = JOptionPane.showInputDialog( "Please enter level: " ) ;

    moduleCode = JOptionPane.showInputDialog( "Please enter module code: " ) ;

    finalMarkStr = JOptionPane.showInputDialog( "Please enter final mark: " ) ;

    // the string values that are entered have to be converted into integer
    level = Integer.parseInt(levelStr) ;
    finalMark = Integer.parseInt(finalMarkStr) ;

    // create new object
    Module m1 = new Module() ;
    }

    }

  2. #2
    revathi17 is offline Member
    Join Date
    Aug 2007
    Posts
    26
    Rep Power
    0

    Default

    In your Module class, you have defined the constructor as
    Java Code:
    Module(String mc, int l, int fm)
    {
    moduleCode = mc;
    level = l;
    finalMark = fm;
    }
    and in Module test you are calling Module(), which does not exist
    you have to pass the parameters and call the constructor in the module class like this:
    Java Code:
    ......
    
    Module m1 = new Module(moduleCode, level, finalMark) ;

    -R

Similar Threads

  1. Replies: 0
    Last Post: 12-19-2007, 10:10 AM
  2. Replies: 0
    Last Post: 12-19-2007, 10:01 AM
  3. reference to elements in array
    By Igor in forum New To Java
    Replies: 1
    Last Post: 12-14-2007, 12:56 PM
  4. problems with array index
    By mary in forum New To Java
    Replies: 2
    Last Post: 08-01-2007, 05:30 PM
  5. Help with array of elements
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 06:33 PM

Posting Permissions

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