6 Replies - 329 Views - Last Post: 02 February 2013 - 08:21 AM Rate Topic: -----

#1 JimmyT72  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 02-February 13

Return string from iterative loop (Not using System.out.println)

Posted 02 February 2013 - 01:02 AM

Hello,

I am trying to figure out how to return a string from a iterative loop.

I know how to code it using system.out.println, but I need to return the whole value as a string. Below is the code for the println version of the code.

for (int i = 0; i < 5; i++) {
	  for (int j = 0; j <= i; j++) {
	    	System.out.print("*");
	  }
	    	System.out.println();
}


It outputs this:

*
**
***
****
*****


However I need to return that whole output as a string, not using the println method. This is what I have so far, but it hits the return and breaks before it can continue to loop. (At least that's what I think).

//Size is a integer being passed in, for this example lets say 5 to match above.
	    public static String triangle(int size){
	    	
                String s = "";
	    	
	        for (int i = 0; i < size; i++) {
	        	   s = "";
	        	   for (int j = 0; j <= i; j++) {
	        	      s = "*" + "\n";
	        	   }	   
	        }
	    return s;
	    }



Thanks for the advice/guidance.

Is This A Good Question/Topic? 0
  • +

Replies To: Return string from iterative loop (Not using System.out.println)

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 8037
  • View blog
  • Posts: 13,754
  • Joined: 19-March 11

Re: Return string from iterative loop (Not using System.out.println)

Posted 02 February 2013 - 01:14 AM

//Size is a integer being passed in, for this example lets say 5 to match above.
	    public static String triangle(int size){
	    	
                String s = "";
	    	
	        for (int i = 0; i < size; i++) {
	        	   s = "";
	        	   for (int j = 0; j <= i; j++) {
	        	      s = "*" + "\n";
	        	   }	   
	        }
	    return s;
	    }




At the end of this loop, the only possible values for s are "" and "*\n". This is because you're not appending to s, you're assigning to it.


/obligatory admonition to use stringbuilder instead of string concatenation
Was This Post Helpful? 0
  • +
  • -

#3 burakaltr  Icon User is offline

  • D.I.C Regular

Reputation: 92
  • View blog
  • Posts: 276
  • Joined: 07-November 10

Re: Return string from iterative loop (Not using System.out.println)

Posted 02 February 2013 - 01:22 AM

I Presume the code you're asking is the one below :


import java.util.StringTokenizer;

public class Str {
	public static void main(String...args){
	String bfr=(build().toString());	
		
	StringTokenizer tkn=new StringTokenizer(bfr);
while(tkn.hasMoreTokens())System.out.print(tkn.nextToken()+"\n");
	}
	static StringBuffer build(){
	StringBuffer buf=new StringBuffer();
	for (int i = 0; i < 5; i++) {
		  for (int j = 0; j <= i; j++) {
		    //	System.out.print("*");
		    	buf.append('*');
		  }
		    	//System.out.println();
		    	buf.append(' ');
	}
	return buf;
	}
}

Was This Post Helpful? 0
  • +
  • -

#4 JimmyT72  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 02-February 13

Re: Return string from iterative loop (Not using System.out.println)

Posted 02 February 2013 - 01:33 AM

View Postburakaltr, on 02 February 2013 - 01:22 AM, said:

I Presume the code you're asking is the one below :


import java.util.StringTokenizer;

public class Str {
	public static void main(String...args){
	String bfr=(build().toString());	
		
	StringTokenizer tkn=new StringTokenizer(bfr);
while(tkn.hasMoreTokens())System.out.print(tkn.nextToken()+"\n");
	}
	static StringBuffer build(){
	StringBuffer buf=new StringBuffer();
	for (int i = 0; i < 5; i++) {
		  for (int j = 0; j <= i; j++) {
		    //	System.out.print("*");
		    	buf.append('*');
		  }
		    	//System.out.println();
		    	buf.append(' ');
	}
	return buf;
	}
}


Hmm. I think there is a way to do this without using StringTokenizer and I prefer to find that way as I am not experienced with that method. I think you understand what I am trying to accomplish thou as you are returning buf and not a println statement.

View Postjon.kiparsky, on 02 February 2013 - 01:14 AM, said:

At the end of this loop, the only possible values for s are "" and "*\n". This is because you're not appending to s, you're assigning to it.


/obligatory admonition to use stringbuilder instead of string concatenation


Sorry, I am not sure what you mean. When you say I am assigning to s, do you mean s, just keeps getting overwritten. Thanks for the help.
Was This Post Helpful? 0
  • +
  • -

#5 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Return string from iterative loop (Not using System.out.println)

Posted 02 February 2013 - 04:45 AM

I think you want the newline character at the end of each line, not after every "*", inserted between the i and j loops:
//Size is a integer being passed in, for this example lets say 5 to match above.
	    public static String triangle(int size
            {
                String s = "";
	    	
	        for (int i = 0; i < size; i++)
                {
                    // the next line empties s each time through the
                    // i loop and is incorrect
	            // s = "";
	            for (int j = 0; j <= i; j++)
                    {
                        // adds i + 1 asterisks to s
	                s += "*";
	            }
                    // adds the linefeed between loops
                    s += "\n"
	        }

	    return s;
	    }

I'm not able to test to verify, but the code above should be close to what you need. Let me know if you have any questions.
Was This Post Helpful? 2
  • +
  • -

#6 JimmyT72  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 02-February 13

Re: Return string from iterative loop (Not using System.out.println)

Posted 02 February 2013 - 05:12 AM

View PostGregBrannon, on 02 February 2013 - 04:45 AM, said:

I think you want the newline character at the end of each line, not after every "*", inserted between the i and j loops:
//Size is a integer being passed in, for this example lets say 5 to match above.
	    public static String triangle(int size
            {
                String s = "";
	    	
	        for (int i = 0; i < size; i++)
                {
                    // the next line empties s each time through the
                    // i loop and is incorrect
	            // s = "";
	            for (int j = 0; j <= i; j++)
                    {
                        // adds i + 1 asterisks to s
	                s += "*";
	            }
                    // adds the linefeed between loops
                    s += "\n"
	        }

	    return s;
	    }

I'm not able to test to verify, but the code above should be close to what you need. Let me know if you have any questions.



Yah, that works. I ended up playing around with it about a hour ago and got everything working, but still wasn't sure why the original code wasn't working, so thanks for explaining. I appreciate it.
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 8037
  • View blog
  • Posts: 13,754
  • Joined: 19-March 11

Re: Return string from iterative loop (Not using System.out.println)

Posted 02 February 2013 - 08:21 AM

View PostJimmyT72, on 02 February 2013 - 03:33 AM, said:

Sorry, I am not sure what you mean. When you say I am assigning to s, do you mean s, just keeps getting overwritten. Thanks for the help.


Yes, that's what I mean. There are three places where s gets a value, and in each of them the previous value of s is lost.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1