Results 1 to 5 of 5
  1. #1
    burrish is offline Member
    Join Date
    Sep 2011
    Location
    Texas
    Posts
    17
    Rep Power
    0

    Default Extract an Array from a Class

    I've tried several things, and I really need help getting the data listed in the Input3 class out into main. How would I write an array in main to grab it from "getNextString"? The loops I've tried all throw errors, and it's just not happening.

    Java Code:
    public class ClassPractice {
    
    	public static void main(String[] args) {
    	
    		
    		
    		
    				
    				
    		
    	
    	
    	}
    
    
    
    }
    public class Input3 
    {
    	private String[] input = new String[6];
    	private static int StringNum = -1;
    	
    	public Input3() 
    	{
    		input[0] = "Shane Mosley";
    		input[1] = "Ike Quartey";
    		input[2] = "Felix Trinidad";
    		input[3] = "Roy Jones";	
    		input[4] = "Kelly Pavlik";
    		input[5] = "Manny Pacquiao";
    
    	}
    
    	
    	public String getNextString()
    	{
    		StringNum++;
    		return input[StringNum];
    		
    	}
    
    	
    }
    Last edited by burrish; 10-20-2011 at 08:20 AM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Extract an Array from a Class

    Create an array
    Create an instance of Input3
    Use a loop to call getNextString

    A problem I see is how big do you make the array?

  3. #3
    sasi040 is offline Member
    Join Date
    Mar 2011
    Location
    Hyderabad,Andhra Pradesh, India
    Posts
    2
    Rep Power
    0

    Default Re: Extract an Array from a Class

    Hi burrish, in main you can place the following code
    Input3 input = new Input3();
    String [] arrayFromClass = input.getNextString();
    for(String s:arrayFromClass){
    System.out.println(s);
    }
    #############
    you can modify the getNextString method to return an array of strings instead each string once.
    public String [] getNextString() {
    return input;
    }
    ..
    I hope this is what you are looking hope this will be a solution.

  4. #4
    burrish is offline Member
    Join Date
    Sep 2011
    Location
    Texas
    Posts
    17
    Rep Power
    0

    Default Re: Extract an Array from a Class

    Quote Originally Posted by Junky View Post
    Create an array
    Create an instance of Input3
    Use a loop to call getNextString

    A problem I see is how big do you make the array?
    In this case, I would assume it to be six string elements long. (input string modified)

  5. #5
    burrish is offline Member
    Join Date
    Sep 2011
    Location
    Texas
    Posts
    17
    Rep Power
    0

    Default Re: Extract an Array from a Class

    I used everyone's information and finally came up with a loop that worked. Thanks to all of you!!

Similar Threads

  1. use array from one class in another class.
    By lemontree45 in forum New To Java
    Replies: 3
    Last Post: 08-26-2011, 06:11 PM
  2. array of class objects
    By ravi1912 in forum New To Java
    Replies: 1
    Last Post: 08-08-2011, 10:58 AM
  3. Replies: 0
    Last Post: 05-12-2011, 08:04 PM
  4. extract strings from an array
    By plasticfood in forum New To Java
    Replies: 18
    Last Post: 10-02-2010, 02:15 PM
  5. Replies: 8
    Last Post: 03-21-2009, 10:34 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
  •