Results 1 to 10 of 10
Thread: void input method
- 02-03-2013, 02:45 PM #1
Member
- Join Date
- Feb 2013
- Posts
- 6
- Rep Power
- 0
void input method
I need to create an input method that is void and call it from the main method.
Then I'd like to instantiate an object in a loop such asJava Code:public void input() { age=Integer.parseInt(JOptionPane.showInputDialog("Enter your age")); name=JOptionPane.showInputDialog("Enter your name"); setAge(age); setName(name); }
Java Code:for (int i=0; i<l.length; i++) //loops through the array { l[i].input(); name=l[i].getName(); age=l[i].getAge(); l[i]=new Listing(name,age); }
-
Re: void input method
- 02-03-2013, 04:03 PM #3
Member
- Join Date
- Feb 2013
- Posts
- 6
- Rep Power
- 0
Re: void input method
-
Re: void input method
OK, I think I may be partly understanding what you're trying to do. Please clarify or correct if I'm wrong:
- Create a Listing class.
- Create an input method in this class that fills the fields of the current object of this class.
- In the main method, create an array of objects of this class.
- In the main method loop through this array using a for loop, calling the input method on each instance held in the array.
If I'm right, your for loop will not need to call l[i].getName(), or any other get methods. All it needs to do is instantiate objects of this class if not yet done so by calling new Listing(), call the input method on this instance created in the loop.
- 02-03-2013, 04:34 PM #5
Member
- Join Date
- Feb 2013
- Posts
- 6
- Rep Power
- 0
Re: void input method
You are correct and this is what I tried
Java Code:for (int i=0; i<l.length; i++) //loops through the array { l[i].input(); name=l[i].getName(); age=l[i].getAge(); l[i]=new Listing(Listing.input()); }
-
Re: void input method
And what happens when you try this?
- 02-03-2013, 05:56 PM #7
Member
- Join Date
- Feb 2013
- Posts
- 6
- Rep Power
- 0
-
Re: void input method
Exactly, you're calling the input() method on the Listing *class* which doesn't make sense and is completely unnecessary. Also, as I stated earlier, getName() and getAge() has no place inside your for loop. What purpose does it serve? It does nothing useful. Instead and again, do as I suggest early. In pseudocode:
Java Code:for (int i = 0; i < .length; i++) { // create a new Listing object and assign it to l[i] // call the input method on this object (not on the class) // and that's it! }
- 02-03-2013, 08:55 PM #9
Member
- Join Date
- Feb 2013
- Posts
- 6
- Rep Power
- 0
Re: void input method
I now have in the listing class
then in the main methodJava Code:public void input() { age=Integer.parseInt(JOptionPane.showInputDialog("Enter your age")); name=JOptionPane.showInputDialog("Enter your name"); }
Java Code:for (int i=0; i<l.length; i++) //loops through the array { l[i].input(); }
-
Re: void input method
And everything works? If so, congrats!
Similar Threads
-
looking for recursive help using Void method
By tripline in forum New To JavaReplies: 10Last Post: 10-27-2011, 06:48 PM -
accessing variables in a method that returns a void
By mochajava in forum New To JavaReplies: 2Last Post: 02-12-2011, 08:00 PM -
How to test a void method in Junit?
By jRookie in forum New To JavaReplies: 7Last Post: 09-01-2010, 07:47 AM -
calling a public void method from a class button
By supa_kali_frajilistik in forum AWT / SwingReplies: 1Last Post: 05-21-2008, 05:40 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks