• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

about toString method

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Not looking good. I think this might be the end. Wait! Is that a tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic