Results 1 to 6 of 6
  1. #1
    csanch11 is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Problem using the toString() method

    So I'm working on a project and noticed that my toString() method won't work. This is just an example of the type of code that I have in my real project. What am I doing wrong here?


    THIS IS MY MAIN CLASS

    XML Code:
    package trialanderror;
    
    import java.util.Scanner;
    
    /**
     * Author: <<Conrado Sanchez>> Date: Task:
     */
    public class TrialAndError {
    
        public static void main(String[] args) {
            Scanner keys = new Scanner(System.in);
            String name;
            String phonenumber; 
            
            System.out.print("What is your name? ");
            name = keys.nextLine();
            System.out.print("What is your phonenumber: ");
            phonenumber = keys.nextLine();
            Example printout = new Example(name, phonenumber);
            printout.toString();
        }
    }

    THIS IS MY EXAMPLE CLASS

    XML Code:
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package trialanderror;
    
    /**
     *
     * @author conrados
     */
    public class Example {
    
        private String name;
        private String phone;
    
        public Example(String name, String phone) {
            this.name = name;
            this.phone = phone;
        }
    
        public String toString() {
            String str = "NAME: " + this.name + "; PHONE: " + this.phone;
            return str;
        }
    
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Problem using the toString() method

    Here's a hint:

    Java Code:
    // other code
    
    // now call method printit.
    printit();
    
    // here's your method
    public int printit() {
        return 2;
    }
    Now what does this program do? Once you figure that out you will have your answer.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    csanch11 is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Re: Problem using the toString() method

    That code does nothing.... You either catch the return value of 2 with a variable inside the main method and print it from there, or use System.out.println(2); and change your return type into void....

    Well okay, I should probably post the real codes I'm working with. I have two problems here.

    Problem #1: I want the toString() method in my Contact class to print out the "ADDED [" + this.name + "; " + this.phone + "]" but It's not doing its job.
    Problem #2: I have two toString() methods. One in my main class (AddressBook) and another one in my Contact class. The toString() method in my AddressBook class has an error. The error says toString() in AddressBook cannot override toString() in Object overriding method is static.

    How do I fix these problems. I tried all sorts of things before posting this on here and I really don't know what's wrong with my code.

    *Note: I'm not done with my source code, I'm just testing out little bits of if to make sure they work and when I tried the toString() method in the Contact class, it did not print out the required information.



    THIS IS THE MAIN CLASS

    XML Code:
    package adressbook;
    
    import java.util.Scanner;
    import java.util.ArrayList;
    
    /**
     * Author: <<Conrado Sanchez>> Date: Task:
     */
    public class AddressBook {
    
        public static Scanner keys = new Scanner(System.in);   // create Scanner object
        public static ArrayList<Contact> addressBook = new ArrayList<>();  // create ArrayList object
    
        public static void main(String[] args) {
            String input;   // user input 
            String input_1; // will hold the user input in lowercase 
    
            do {
                // show menu and ask for user input 
                System.out.println("+---------------------+");
                System.out.println("ADD     Adds a contact");
                System.out.println("FIND    Finds contacts ");
                System.out.println("PRINT   Prints contacts ");
                System.out.println("QUIT    Quits");
                System.out.println("+---------------------+");
                System.out.println("\n");
                System.out.print("--> ");
                input = keys.nextLine();
    
                // space
                System.out.println();
    
                // make input lowercase 
                input_1 = input.toLowerCase();
    
                // Make user re-enter input if it's not add, find, print, or quit
                while (!input.matches("add|find|print|quit")) {
                    System.out.println("Invalid command. Please re-enter one of the options below.");
                    System.out.println("+---------------------+");
                    System.out.println("ADD     Adds a contact");
                    System.out.println("FIND    Finds contacts ");
                    System.out.println("PRINT   Prints contacts ");
                    System.out.println("QUIT    Quits");
                    System.out.println("+---------------------+");
                    System.out.println("\n");
                    System.out.print("--> ");
                    input = keys.nextLine();
    
                    // space
                    System.out.println();
                }
    
                // determine and perform what type of command the user wants to use
                if (input_1.equals("add")) {
                    String name;            // hold the name 
                    String phonenumber;     // hold phone number
    
                    // ask and get name
                    System.out.print("NAME ---> ");
                    name = keys.nextLine();
                    System.out.print("PHONE ---> ");
                    phonenumber = keys.nextLine();
                    // send information to the add method 
                    add(name, phonenumber);
                    // space
                    System.out.println();
                } else if (input_1.equals("find")) {
    
                } else if (input_1.equals("print")) {
    
                }
    
            } while (input_1.compareTo("quit") != 0);
    
        } // end of main 
    
        /**
         * The add method prompts the user for a name and a phone number. (NOT
         * FINISHED)
         */
        public static void add(String n, String p) {
            addressBook.add(new Contact(n, p));
            
        }
    
        /**
         * The find method prompts the user for texts it will use to search the
         * address book and it will display a lists of all contacts that contain
         * that text in either the contacts name or phone number. (NOT FINISHED)
         */
        public static void find() {
    
        }
    
        /**
         * The print method prints out a lists of all contacts in the address book
         * as well as the number of contacts it printed. (NOTFINISHED)
         */
        public static void toString() {
    
        }
    
    } // end of WorkBook class


    THIS IS MY CONTACT CLASS

    XML Code:
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package workbook;
    
    /**
     *
     * @author conrados
     */
    public class Contact {
    
        private String name;    // hold persons name 
        private String phone;   // hold persons phone number 
    
        // constructor 
        public Contact(String name, String phone) {
            this.name = name;
            this.phone = phone;
        }
    
        // empty constructor 
        public Contact() {
    
        }
    
        // toString method displays the name and phone number of the user
        @Override
        public String toString() {
            return "ADDED [" + this.name + "; " + this.phone + "]";
        }
    }
    Last edited by csanch11; 10-05-2014 at 12:20 AM.

  4. #4
    csanch11 is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Re: Problem using the toString() method

    Never mind i figured it out.... For the contact toString() method i just had to replace this with Contact... So instead of this.name i put Contact.name

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Problem using the toString() method

    You missed the point. In your first post, you had the following statement.
    Java Code:
    printout.toString();
    All that does is return a string. but you did nothing with it. Just like I did nothing with the return value of 2. If you want to print it out you should do
    Java Code:
    System.out.println(printout);
     //or
    System.out.println(printout.toString());
    Regards, Jim
    Last edited by jim829; 10-06-2014 at 03:45 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Problem using the toString() method

    And changing the toString to print out the value would be wrong, as that's clearly not the job of toString...

    Seen that one done too many times, so simply pre-empting.
    :)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. toString Method
    By hndlz in forum New To Java
    Replies: 9
    Last Post: 04-20-2012, 04:45 AM
  2. URGENT toString method help
    By johnnyjava in forum New To Java
    Replies: 17
    Last Post: 08-16-2011, 09:09 PM
  3. Why isn't my toString method working
    By arri3oi in forum New To Java
    Replies: 7
    Last Post: 12-13-2010, 06:34 AM
  4. toString() method
    By 01allenh in forum New To Java
    Replies: 2
    Last Post: 03-26-2009, 12:43 AM
  5. toString method
    By apfroggy0408 in forum New To Java
    Replies: 6
    Last Post: 01-31-2008, 05:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •