Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: how to reuse method returned string ??

  1. #1
    Junior Member
    Join Date
    May 2009
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question how to reuse method returned string ??

    Hello friends,

    I am writing a small program in which i have 3 methods. All are working fine. One of the method getstring(char_aarray, string, int_array) is taking 3 arguments First arguments is char[ ] and returning a character array char[ ]. I want that when the program start and there is a call to getstring(char_rarray, string, int_array) method, it takes a predefined char[ ] as arguemnt but second time if there is call to that method it uses its own returned char[ ].

    Thanks in advance.

    BR,
    zeeshan


  2. #2
    Junior Member
    Join Date
    Aug 2009
    Posts
    8
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: how to reuse method returned string ??

    Show me your program once.....
    Regards,
    guptapr

  3. #3
    Junior Member
    Join Date
    May 2009
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how to reuse method returned string ??

    I am trying to develop a hangman game. the code is right here.


    import java.util.Vector;
     
    public class SearchChar {
     
    	public static void main(String args[]){
     
    		String main= new String("zeeshannabeel");
    		String search= new String("e");
     
    		String s= printdash(main);
    		char[] prestring= s.toCharArray();
    		Vector<Integer> vectorarray= getindex(main, search);
     
    		char[] searcharray= search.toCharArray();
    		char[] strings= guess(prestring,searcharray,vectorarray);
    		System.out.println(strings);
     
    	}
     
    	/* --- To print dash first time --- */
    	public static String printdash(String main){
    		String dash= new String();
    		for(int z=0; z<main.length(); z++){
    			dash= dash.concat("-");
    			}
    		System.out.println(dash);
    		return dash;
     
    		}
     
    	/* --- To find the IndexOf search string in Main string --- */
    	public static Vector<Integer> getindex(String main, String search){
    		Vector<Integer> varray= new Vector<Integer>();
     
    		for(int i=0; i<=main.lastIndexOf(search); i++){
    			varray.addElement(main.indexOf(search, i));
    			i= main.indexOf(search, i);
    		}
    		/* --- To print indexOf Search string in Main string --- */ 
    		for(int a=0; a<varray.size(); a++){
    			System.out.println(varray.get(a));	
    		}
    		return varray;
    	}
     
    	/*------ To insert Search character in String --- */
    	public static char[] guess(char[]pre_string, char[]guess_string, Vector<Integer> vectorarray){
    		int j=0;
    		while(j<vectorarray.size()){
    			int k=vectorarray.elementAt(j);
    			for(int i=0; i<guess_string.length; i++){
    				pre_string[k]= guess_string[i];				
    				k++;
    			}
    			j++;
     
    		}
    			return pre_string;
    		}
    }
    Last edited by Freaky Chris; September 4th, 2009 at 05:03 AM. Reason: Added code tags

  4. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: how to reuse method returned string ??

    Just store the array it returns and pass it back into it the second time.

    Chris
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  5. #5
    Junior Member
    Join Date
    May 2009
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how to reuse method returned string ??

    ofcourse i know that i have to store the array and pass it back to the second time ... but how? this is my question. I have stored that arrayin char[] strings as u can see in my code. but how can i reuse it second third fourth untill char[] strings matches to char[] main. But remember call to the method depends on if the search string changes.

    BR,
    Zeeshan

  6. #6
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: how to reuse method returned string ??

    I cannot see your getstrig method :/

    Chris
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  7. #7
    Junior Member
    Join Date
    May 2009
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Thumbs up Re: how to reuse method returned string ??

    there is no getstring method. I just wrote a fake name. The name of that method in code is guess method with 3 arguments. anyways thats for ur help i have solved the problem

  8. #8
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: how to reuse method returned string ??

    Next time just use the name of the method, it avoids confusion.

    Glad you have sorted it, mark as solved if that is everythin.

    Thanks,
    Chris

    The Moderation Team...

    Always wanted to say that lmao!
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  9. The Following User Says Thank You to Freaky Chris For This Useful Post:

    zeeshanmirza (September 4th, 2009)

Similar Threads

  1. [SOLVED] Method declaration in Java
    By mohsendeveloper in forum Object Oriented Programming
    Replies: 4
    Last Post: June 11th, 2009, 03:18 AM
  2. How to display the contents of my queue?
    By rocafella5007 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 30th, 2009, 11:46 AM
  3. problem with Scanner nextLine() method
    By mia_tech in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 10th, 2009, 04:14 AM
  4. Replies: 2
    Last Post: March 4th, 2009, 05:32 AM
  5. Replies: 1
    Last Post: December 30th, 2008, 06:30 AM