Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

passing arguments to constructor

 
Martyn Clark
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
can anybody point me in the right direction for the problem i am having.
if i have two arguments in a constuctor say:
Object(String empName, String empLocation) when i create new Object but want to get the input form JOptionPane, what do i put in as the arguments?
i Know i can put "my name", "UK" and this works but if it is comming from JOptionPane i can not seem to get it to work can anybody help please.
Thanks in advance.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, welcome to the ranch!

See if I read this right ... if you do

everything is ok, but if you do

you don't like the results? I guess you could have trouble in getting values from the GUI or passing them to the constructor. Sounds like the constructor itself is probably ok from the first "bob" test.

Maybe you could post some of the code that gets the values from the GUI and see if we spot anything. And describe "doesn't work" in a bit more detail. What happened? What was not as expected?
 
Martyn Clark
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi stan,
thanks for the reply, sorry i did not explain fully ill try again!
i have two private members in the class
private String name;
private String course;
the constructor i have is:
Student(String studentName, String StudentCourse)
{
name = studentName;
course = studentCourse;
}

then i have two method of the get type, return name, return course;

in the test class

i create new object
Student newStudent = new Student(//what agrument do i put in here);

if i hard code it in as "Martyn", "java" it retuns and prints out as expected but i really need to be using the JOptionPane to get the info.

and i am of the understanding because i have two arguments in the constuctor i must have some aguments when i create a new student object. how do i pass the input from the JOptionPane to this new object so it passes it to the Student class.
hope i have explained myself a bit better
thank you.
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just a guess: if your optionPane has two textFields, both @param equals
String empName = textField1.getText();
String empLocation = textField2.getText();

now looks easy enough:
MyClass abc = new MyClass(empName, empLocation);
[ June 07, 2005: Message edited by: miguel lisboa ]
 
Martyn Clark
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi miguel,
thank you for your reply, but i am just trying to create a class called Student, with 1 constructor and two methods to return the name and course.
i dont know how to create a new student object and get the arguments from the JOptionPane.showInputDialog passed to the student class then retrive them and print them out in the consol.
thank you
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pls post some code you'v got so far
 
Martyn Clark
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again,
here is the student class

//File called Student.java

class Student
{
//Members section
//Private members
private String name=" ";
private String course=" ";

//Constructor
Student(String studentName, String studentCourse)
{
name = studentName;
course = studentCourse;
}

//Method section
public String getInput(String input)
{
return input;

}
public String getName()
{
return name;
}

public void printAll()
{
System.out.println("Your name is: " + name);
}
}

and here is the StudentTest class

// File called StudentTest.java
// Main program for testing Student class
import javax.swing.*;
class StudentTest
{
public static void main(String[] args)
{
Student newStudent = new Student(//What goes here insted of "myName" , "Java");
//some how i need to use the JOptionPane in here somewhere to get the input
//then for it to printout.
newStudent.getName();
newStudent.getCourse();
newStudent.printAll();

}
}

i appriciate your help cheers!
 
Martyn Clark
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry the input method should not be there i was just trying something differnt to no avail i might add
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no need for swing stuff!


C:\javas>java StudentTest miguel Java
Your name is miguel and your course is Java
 
Martyn Clark
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey miguel,
you made that look easy!!

you will have to excuse me but i have only just started learning java, and this is leading up to an assignment i have to do and it states they would prefer me to use the JOptionPane to get the input. The end result will be 3 classes i.e. Student, Storage, and the main program, i created a programme with just main method but i created 2 single arrays one for name one for course used nested for loops so i could input as many names and courses that i specified in a variable maxNum and when i did it this way it all works great using the JOptionPane but i just cannot fathem out how to do it when i create two seperate files and it's really bugging me!!!
your code works great, but i do need to use the other option.
cheers again
 
Daniel .J.Hyslop
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hiya martin
JOptionPane initialises a string value to your variables try declaring your string variables and initialising them with JoptionPane before you create the Student object ie Student s = new Student(name,course);I think you will find that this sets the values in your constuctor
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[ June 07, 2005: Message edited by: miguel lisboa ]
 
Martyn Clark
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
i would like to thank you all for your replies you have been a great help.
cheers.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic