Whats wrong with this.

So... This program I'm going to make is suppose to ask a numer for unidentified user and print out the number as stars. I cant figure out why does this program not enter the given value of stars into array. For example in this program I want to give stars to 5 people. Here is the code:

publicclass tahtiTaulukko{

/** Creates a new instance of tahtiTaulukko */

publicstaticvoid main(String args[]){

String myArray [];

myArray =new String[5];

int points = 0;

String mLine =new String();

char star ='*';

for (int i = 0; i < myArray.length; i++){

System.out.println("Give me the Stars of " + (i+1) +" person");

points = Oma.lueInt();

for (int j = 0; j < points; j++){

mLine += star;

myArray[j] = mLine;

}

}

System.out.println(myArray);

}

}

I think the problem is here "myArray[j] = mLine;" it does not add the mLine valua to myArray but why?

[1730 byte] By [Orccya] at [2007-10-3 7:58:30]
# 1

I think you could modify the code as follows:

public class tahtiTaulukko {

/** Creates a new instance of tahtiTaulukko */

public static void main(String args[]) {

String myArray [];

myArray = new String[5];

int points = 0;

String mLine = new String();

char star = '*';

for (int i = 0; i < myArray.length; i++) {

System.out.println("Give me the Stars of " + (i+1) + " person");

points = Oma.lueInt();

for (int j = 0; j < points; j++) {

mLine += star;

}

myArray[i] = mLine;

}

System.out.println(myArray);

}

Pravin

PMJaina at 2007-7-15 3:01:31 > top of Java-index,Java Essentials,New To Java...
# 2

I'll bet this is more like what you want:

[code]

package cruft;

import java.util.Scanner;

public class TahtiTaulukko

{

/**

* Creates a new instance of TahtiTaulukko - no it doesn't. this is

* just a main program. there's no constructor here, except for the one

* that the compiler writes for you.

*/

public static void main(String args[])

{

String myArray[] = new String[5];

int points = 0;

char star = '*';

Scanner scanner = new Scanner(System.in);

for (int i = 0; i < myArray.length; i++)

{

System.out.println("Give me the Stars of " + (i + 1) + " person");

points = scanner.nextInt();

String mLine = "";

for (int j = 0; j < points; j++)

{

mLine += star;

}

myArray[i] = mLine;

}

for (int i = 0; i < myArray.length; ++i)

{

System.out.println(myArray[i]);

}

}

}

[code]

%

duffymoa at 2007-7-15 3:01:31 > top of Java-index,Java Essentials,New To Java...
# 3

Try again:

package cruft;

import java.util.Scanner;

public class TahtiTaulukko

{

/**

* Creates a new instance of TahtiTaulukko - no it doesn't. this is

* just a main program. there's no constructor here, except for the one

* that the compiler writes for you.

*/

public static void main(String args[])

{

String myArray[] = new String[5];

int points = 0;

char star = '*';

Scanner scanner = new Scanner(System.in);

for (int i = 0; i < myArray.length; i++)

{

System.out.println("Give me the Stars of " + (i + 1) + " person");

points = scanner.nextInt();

String mLine = "";

for (int j = 0; j < points; j++)

{

mLine += star;

}

myArray[i] = mLine;

}

for (int i = 0; i < myArray.length; ++i)

{

System.out.println(myArray[i]);

}

}

}

duffymoa at 2007-7-15 3:01:31 > top of Java-index,Java Essentials,New To Java...
# 4
Two additional changes: (1) You need to move the declaration of the String that contains the stars inside the first loop.(2) You can't just print the array that way. You have to loop through and print each individual String.%
duffymoa at 2007-7-15 3:01:31 > top of Java-index,Java Essentials,New To Java...