Results 1 to 2 of 2
- 12-13-2007, 06:08 PM #1
Member
- Join Date
- Dec 2007
- Posts
- 1
- Rep Power
- 0
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() ;
}
}
- 12-14-2007, 07:25 AM #2
Member
- Join Date
- Aug 2007
- Posts
- 26
- Rep Power
- 0
In your Module class, you have defined the constructor as
and in Module test you are calling Module(), which does not existJava Code:Module(String mc, int l, int fm) { moduleCode = mc; level = l; finalMark = fm; }
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
-
Calling constructor of parent class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:10 AM -
Calling constructor of same class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:01 AM -
reference to elements in array
By Igor in forum New To JavaReplies: 1Last Post: 12-14-2007, 11:56 AM -
problems with array index
By mary in forum New To JavaReplies: 2Last Post: 08-01-2007, 04:30 PM -
Help with array of elements
By zoe in forum New To JavaReplies: 1Last Post: 07-24-2007, 05:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks