Results 1 to 10 of 10
  1. #1
    mwimpelberg is offline Member
    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Default void input method

    I need to create an input method that is void and call it from the main method.

    Java Code:
    	public  void  input()
    	{
    		
    		age=Integer.parseInt(JOptionPane.showInputDialog("Enter your age"));
    		name=JOptionPane.showInputDialog("Enter your name");	
    		setAge(age);
    		setName(name);
    		
    	}
    Then I'd like to instantiate an object in a loop such as

    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);
    		}

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: void input method

    Quote Originally Posted by mwimpelberg View Post
    I need to create an input method that is void and call it from the main method....
    OK, so what have you tried so far, and where are you stuck?

  3. #3
    mwimpelberg is offline Member
    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Default Re: void input method

    Quote Originally Posted by Fubarable View Post
    OK, so what have you tried so far, and where are you stuck?
    I tried the input method which just prints out the dialog box. From there, in my floor loop I'd like to have a new object Listing(name,age). So in my input method, I'd like to use my setAge() and setName() which return an int and String respectively

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  5. #5
    mwimpelberg is offline Member
    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Default 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());
    		}

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  7. #7
    mwimpelberg is offline Member
    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Default Re: void input method

    Quote Originally Posted by mwimpelberg View Post
    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());
    		}


    Cannot make a static reference to the non-static method input() from the type Listing

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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!
    }

  9. #9
    mwimpelberg is offline Member
    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Default Re: void input method

    I now have in the listing class

    Java Code:
    	public  void  input()
    	{
    		
    		age=Integer.parseInt(JOptionPane.showInputDialog("Enter your age"));
    		name=JOptionPane.showInputDialog("Enter your name");	
    			
    	}
    then in the main method

    Java Code:
    			
    		for (int i=0; i<l.length; i++) //loops through the array
    		{
    		l[i].input();
    		}
    Quote Originally Posted by Fubarable View Post
    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!
    }

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. looking for recursive help using Void method
    By tripline in forum New To Java
    Replies: 10
    Last Post: 10-27-2011, 06:48 PM
  2. accessing variables in a method that returns a void
    By mochajava in forum New To Java
    Replies: 2
    Last Post: 02-12-2011, 08:00 PM
  3. How to test a void method in Junit?
    By jRookie in forum New To Java
    Replies: 7
    Last Post: 09-01-2010, 07:47 AM
  4. calling a public void method from a class button
    By supa_kali_frajilistik in forum AWT / Swing
    Replies: 1
    Last Post: 05-21-2008, 05:40 AM

Posting Permissions

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