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
posted
0
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
posted
0
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
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: 3011
9
posted
0
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
posted
0
[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."