Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about toString method

 
Kranthi Kondapaka
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15633
46
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kranthi Kondapaka
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Kranthi Kondapaka:]   exact implementation of toString method

That's the String class.

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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic