aspose file tools*
The moose likes Java in General and the fly likes about toString method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "about toString method" Watch "about toString method" New topic
Author

about toString method

Kranthi Kondapaka
Ranch Hand

Joined: Sep 17, 2007
Posts: 31
How to make use of toString method to convert object to string,an object which is passed as an argument to the toString method.
For example:
String s=toString(e)
e is an employee class object.
Srikanth Basa
Ranch Hand

Joined: Jun 06, 2005
Posts: 241
I don't think I understand your question. Which toString method you are speaking about and What do you mean by "make use of toString method" ?
Kranthi Kondapaka
Ranch Hand

Joined: Sep 17, 2007
Posts: 31
package pack;
import java.util.*;
import java.io.*;
public class Samp {
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = 0;
try {
System.out.print("Enter the Number of records :");
n = Integer.parseInt(br.readLine());
ArrayList al = new ArrayList();
Employee e = null;
for (int i = 0; i < n; i++) {
System.out.print("Enter the Name:");
String name = br.readLine();
System.out.print("Enter the Age:");
int age = Integer.parseInt(br.readLine());
System.out.print("Enter the Designation:");
String desig = br.readLine();
e = new Employee(name, age, desig);
al.add(i, e);
}
// String s=al.toString(e);
// System.out.println(s);
Iterator i = al.iterator();
while (i.hasNext()) {
System.out.println((i.next()));
}
} catch (Exception e) {
System.out.println("Invalid Input");
}
}
}
class Employee
{
String name, desig;
int age;
public Employee()
{
}
public Employee(String name, int age, String desig)
{
this.name = name;
this.age = age;
this.desig = desig;
}
public String toString(Object e)
{
return e;
}
}
this is my prog and i want the output in string format(presently m getting as classname@memory address
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14111
    
  16

At the bottom of your program I see this:

This will not compile. The method must return a String but you are trying to return an Object - the compiler will give an error message about that.

So please post the correct code, and please use code tags when you post code, so that the forum can display your code properly.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Kranthi Kondapaka
Ranch Hand

Joined: Sep 17, 2007
Posts: 31
package pack;
import java.util.*;
import java.io.*;
public class Samp {
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = 0;
try {
System.out.print("Enter the Number of records :");
n = Integer.parseInt(br.readLine());
ArrayList al = new ArrayList();
Employee e = null;
for (int i = 0; i < n; i++) {
System.out.print("Enter the Name:");
String name = br.readLine();
System.out.print("Enter the Age:");
int age = Integer.parseInt(br.readLine());
System.out.print("Enter the Designation:");
String desig = br.readLine();
e = new Employee(name, age, desig);
al.add(i, e);
}
// String s=al.toString(e);
// System.out.println(s);
Iterator i = al.iterator();
while (i.hasNext()) {
System.out.println((i.next()));
}
} catch (Exception e) {
System.out.println("Invalid Input");
}
}
}
class Employee
{
String name, desig;
int age;
public Employee()
{
}
public Employee(String name, int age, String desig)
{
this.name = name;
this.age = age;
this.desig = desig;
}
}
this is the exact code i wrote and i want to know the exact implementation of toString method
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3446
    
  12
The exact implementation depends on what you want it to do. For example you could return just the name
or it could return the name with the age in brackets afterwards

Basically, it's completely up to you what it does.


Joanne
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
[Kranthi Kondapaka:]   exact implementation of toString method

That's the String class.

For any Java Object, if one does not provide a toString() method:



"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
 
Consider Paul's rocket mass heater.
 
subject: about toString method