Author Topic: [java help] concatenation?  (Read 4519 times)

0 Members and 1 Guest are viewing this topic.

Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
[java help] concatenation?
« 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?
Loved this place, still the best producers of power metal, and sparked my dreams of coding.

Offline merthsoft

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 241
  • Rating: +63/-1
    • View Profile
Re: [java help] concatenation?
« Reply #1 on: April 13, 2011, 04:47:15 pm »
According to the javadocs for string, you can just use the concat 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);
« Last Edit: April 13, 2011, 04:48:05 pm by merthsoft »
Shaun

Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
Re: [java help] concatenation?
« Reply #2 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
« Last Edit: April 13, 2011, 05:07:48 pm by Snake X »
Loved this place, still the best producers of power metal, and sparked my dreams of coding.

Offline merthsoft

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 241
  • Rating: +63/-1
    • View Profile
Re: [java help] concatenation?
« Reply #3 on: April 13, 2011, 05:08:00 pm »
What did it do?
« Last Edit: April 13, 2011, 05:09:37 pm by merthsoft »
Shaun

Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
Re: [java help] concatenation?
« Reply #4 on: April 13, 2011, 05:09:00 pm »
just displayed 'I like pie!'
Loved this place, still the best producers of power metal, and sparked my dreams of coding.

Offline merthsoft

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 241
  • Rating: +63/-1
    • View Profile
Re: [java help] concatenation?
« Reply #5 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.
« Last Edit: April 13, 2011, 05:11:54 pm by merthsoft »
Shaun

Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
Re: [java help] concatenation?
« Reply #6 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
Loved this place, still the best producers of power metal, and sparked my dreams of coding.

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: [java help] concatenation?
« Reply #7 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.


Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
Re: [java help] concatenation?
« Reply #8 on: April 13, 2011, 09:39:57 pm »
I'll try this later. Thanks! For now i gtg
Loved this place, still the best producers of power metal, and sparked my dreams of coding.

Offline Madskillz

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 488
  • Rating: +32/-2
    • View Profile
Re: [java help] concatenation?
« Reply #9 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.