Omnimaga

General Discussion => Technology and Development => Computer Programming => Topic started by: Snake X on April 13, 2011, 04:38:47 pm

Title: [java help] concatenation?
Post by: Snake X on April 13, 2011, 04:38:47 pm
Again.. 3rd time going with java help because im just that special :P

Ok, this time I need to get help with the concat method to concatenate a string upon itself.
So say I have this code (please dont tell me my for loop wont initiate if i get it wrong because this is just an example >.>)

Code: [Select]
String pie = new String("I like pie! "); //pie is good btw :P
for (int i=1;i>=10;i++) {
  //concatenate the string Pie with itself thus adding 'I like pie!' to the string pie 10 times.
}
System.out.print(pie);

How would I make that code in the for loop? what would it look like?
Title: Re: [java help] concatenation?
Post by: merthsoft on April 13, 2011, 04:47:15 pm
According to the javadocs (http://download.oracle.com/javase/6/docs/api/java/lang/String.html) for string, you can just use the concat (http://download.oracle.com/javase/6/docs/api/java/lang/String.html#concat%28java.lang.String%29) method. So it'll look like:
Code: [Select]
String pie = new String("I like pie! "); //pie is good btw :P
for (int i=1;i<10;i++) { // changed >= to <
  pie.concat(pie);
}
System.out.print(pie);
Title: Re: [java help] concatenation?
Post by: Snake X on April 13, 2011, 04:55:45 pm
sorry but that didnt work :(

edit: I knew you could use that method, but i wasnt sure how for strings to be concatenated with themselves
Title: Re: [java help] concatenation?
Post by: merthsoft on April 13, 2011, 05:08:00 pm
What did it do?
Title: Re: [java help] concatenation?
Post by: Snake X on April 13, 2011, 05:09:00 pm
just displayed 'I like pie!'
Title: Re: [java help] concatenation?
Post by: merthsoft on April 13, 2011, 05:09:41 pm
Oh, yeah, do pie = pie.concat(pie);

Note, this'll actually concat it a lot more times than 10, since it concats it to itself over and over again. The "correct" thing to do would be to use a StringBuilder.
Title: Re: [java help] concatenation?
Post by: Snake X on April 13, 2011, 05:18:00 pm
yeah that worked, but not for my specific program really. Ok what my program does.. or what it should do is takes a number of lines, and makes a star pyramid out of it
example
5 lines is this:

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

here is my code:

Code: [Select]
import javax.swing.JOptionPane;

public class starz {

public static void main (String []args)
{
String numtemp = JOptionPane.showInputDialog("Enter a number of lines:");
int lines = Integer.parseInt(numtemp);
String star = new String("*");
for (int count=0;count<=lines;count++) {
for (int spaces=lines-1;spaces>0;spaces--) {
System.out.print(".");
}
for (int starcount=1;starcount<=lines;starcount++) {
System.out.print(star);
star = star.concat(star); //still needs to be fixed
star = star.concat(star); //still needs to be fixed
}
System.out.println();
}
}
}
I have a dot replacing a space to make sure its working, but what it does basically is that it prints 1 star, adds 2 more every time supposedly making the pyramid shape. However, the real end result is an infinite loop :s
Title: Re: [java help] concatenation?
Post by: nemo on April 13, 2011, 08:54:01 pm
here's how i would do it:
Code: [Select]
line | stars | spaces
0      1       4
1      3       3
2      5       2
3      7       1
4      9       0
make two linear equations from this:
stars = line * 2 + 1;
spaces = 4 - line;
and then use them in the inner for-loop headers.
Code: [Select]
for(int line = 0; line < 5; line++){
    for(int spaces = 0; spaces < 4 - line; spaces++)
        System.out.print(" ");
    for(int stars = 0; stars < line * 2 + 1; stars++)
        System.out.print("*");
    System.out.println();
}

to use concatenation like you're trying to do, just start a String star as "*", and every new line concatenate "**".
Code: [Select]
String stars = "*";
for(int line = 0; line < 5; line++){
    for(int spaces = 0; spaces < 4 - line; spaces++)
        System.out.print(" ");
    System.out.print(stars);
    stars += "**";
    System.out.println();
}

yes, i realize the for loop for spaces can be optimized. i was just showing how i used my linear equations in the code.
Title: Re: [java help] concatenation?
Post by: Snake X on April 13, 2011, 09:39:57 pm
I'll try this later. Thanks! For now i gtg
Title: Re: [java help] concatenation?
Post by: Madskillz on April 13, 2011, 11:25:18 pm
Ha ha I remember doing the star pyramid, excepting we had to make a pine tree for ours.