Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Return an array

Status
Not open for further replies.

shagymoe

Programmer
Apr 20, 2001
72
0
0
US
I'm a newbie to Java and I'm trying to write a class which will parse a search string, put each word from the string into an array and then return the array, but it won't work no matter what I do. Please help.

Code:
import java.util.regex.*;
import java.util.*;

public class SearchStringArray
{
	String searchString;
	String boundaryCharacter = " ";	

	public String[] SearchStringArray(String s)
	{
		String[] searchArray;
		searchString = s;
		Pattern space = Pattern.compile(boundaryCharacter);
		searchArray = space.split(searchString);
		return searchArray;
	}		

	
}

The class to test it is here:
Code:
import java.util.*;

public class Testssa 
{
	public static void main(String[] args) 
	{
		String search1 = "xdv hs5";
		SearchStringArray[] newone = new SearchStringArray(search1);
		
		for (int i=0; i<newone.length; i++ )
		{
			System.out.println(newone.length);
		}
		
		
	}
}


 
Ok well, I know that a constructor shouldn't return anything so how about this: (It doesn't work either)

Code:
import java.util.*;
import java.util.regex.*;

public class Testssa 
{
	public static void main(String[] args) 
	{
		String search1 = &quot;xdv hs5&quot;;
		SearchStringArray[] newone = new SearchStringArray(search1);
		
		for (int i=0; i<newone.length; i++ )
		{
			System.out.println(newone.length);
		}

	}

	public static String[] SearchStringArray(String s)
	{
		String searchString;
		String boundaryCharacter = &quot; &quot;;	
		String[] searchArray;
		searchString = s;
		Pattern space = Pattern.compile(boundaryCharacter);
		searchArray = space.split(searchString);
		return searchArray;
		
	}		
		
		
	
}

 
Well, THIS works, but it isn't how I wanted to do it. I wanted the SearchStringArray to be a class.

Code:
import java.util.*;
import java.util.regex.*;

public class Testssa 
{
	public static void main(String[] args) 
	{
		String search1 = &quot;xdv hs5&quot;;
		String[] newone = SearchStringArray(search1);
	    
		for (int i=0; i<newone.length; i++ )
		{
			System.out.println(newone[i]);
		}

	}

	public static String[] SearchStringArray(String s)
	{
		
		String searchString;
		String boundaryCharacter = &quot; &quot;;	
		String[] searchArray;
		searchString = s;
		Pattern space = Pattern.compile(boundaryCharacter);
		searchArray = space.split(searchString);
		return searchArray;		
		
	}		
	
}
 
When a class is created its constructor is called, a constructor does not return anything, it is a special kind of method that is always void.

So while what you are doing looks neat, it will never work.

You best bet is to create the
Code:
SearchStringArray
class as a class with no constuctor, and then provide a method within that class that returns a
Code:
String[]
and contains the search code (basically what you initial constructor does, but as a method not as a constructor).

The advantage there is you only need one class and you can call the StringSearch method as many times as you want, your initial way of doing it meant that if you wanted to search 2 strings you would have to create 2 identical classes, which is a major waste of time and resources. ----------------------------------------
There are no onions, only magic
----------------------------------------
 
Thanks, I'll try that. I have it working as you can see in the third iteration, but it is all in one class.
 
My suggestion for this class to be reusable would be to create the StringArray class and implementing a searching method and maybe a sorting method for it as well. The constructor would initialize the array and the search method would search it of course.
 
Hello,

The method with the same name of the class here cannot be used as constructor of the class, but rather as a normal method of the class.
So, you can call the method on the reference of SearchStringArray class.

Hope it helps.

Thanks in advance.

Soumen Ghosh
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top