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]

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
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]
%
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]);
}
}
}