codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  help ! toString method  happygirl at 00:47 on Monday, March 27, 2006
 

i am creating a program that have the following output

Name Number of children
John 5


<Added>

Name Number of children
john 5
lisa 3
wendy 6

Now i want use the toString method format the output become
Name Number of children
john *****
lisa ***
wendy ******


can anyone show me how to do it ?? thanks !

  Re: help ! toString method  crwood at 07:17 on Monday, March 27, 2006
 


public class PersonTest
{
public static void main(String[] args)
{
String[] names = { "john", "lisa", "wendy" };
int[] ages = { 5, 3, 6 };
Person randy = new Person("Randy Johnson", names, ages);
System.out.println(randy);
}
}

class Person
{
String name;
String[] children;
int[] ages;

Person(String name, String[] kids, int[] ages)
{
this.name = name;
children = kids;
this.ages = ages;
}

public String toString()
{
String s = "Name\tNumber of Children\n";
for(int j = 0; j < children.length; j++)
{
s += children[j] + "\t";
for(int k = 0; k < ages[j]; k++)
s += "*";
s += "\n";
}
return s;
}
}









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
//








Recent Forum Threads
•  Re: Security - Code verify
•  Job @ EarlySail
•  Job @ EarlySail (perl)
•  IPC problem
•  Re: import contacts of msn/yahoo
•  Cookies and Threads C++
•  right justify a background in a table?
•  Help with Loop (C++/MFC)
•  Help with Loop (C++/MFC)


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2007