Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Why cant I use toString() with vector?

  1. #1
    Member
    Join Date
    Sep 2011
    Posts
    63
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Why cant I use toString() with vector?

    I have declared a new vector called v of type String with an initial capacity of 10 and increasing by 5 each tiem using the following:
    Vector<String> v = new Vector<String>(10, 5);

    I am then trying to use the add method to add a randome string as follows:

    v.add(toString(random.nextInt(5000)));

    I am getting the following error;

    error: method toString in class Object cannot be applied to given types;

    Why can't i do this? I would have thought it would just pass a random number as a String!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Why cant I use toString() with vector?

    See the API for Object - the toString method contains no parameters and returns a String - you are attempting to call a toString(int val) method, which does not exist. Call toString on an object, not passing the object to the method:
    Integer i = new Integer(random.nextInt(5000));//without autoboxing for clarity
    String stringValue = i.toString();

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Why cant I use toString() with vector?

    That is not how toString works. You have to call the method on an object and you do not pass it a parameter.

    class Foo {
        String name;
        String dob;
     
        Foo(String n, String d) {
            name = n;
            dob = d;
        }
     
        public String toString() {
            return name + " was born on " + dob;
        }
     
        public static vodi main(String[] args) {
            Foo f = new Foo ("Bob", "1/4/1950");
            System.out.println(f.toString());
            // or
            System.out.println(f); //toString method is automagically called
        }
    }
    Improving the world one idiot at a time!

  4. The Following User Says Thank You to Junky For This Useful Post:

    tarkal (November 9th, 2011)

  5. #4
    Member
    Join Date
    Sep 2011
    Posts
    63
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Why cant I use toString() with vector?

    Fantastic, that will be why it wasn't working then!

    Thanks

  6. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    7
    My Mood
    Busy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why cant I use toString() with vector?

    Your program has some error i.e

    nextInt() is not a static method.we cant call it directly with class name .
    Try It.
    Random r=new Random();
    Vector<String> v = new Vector<String>(10, 5);
    v.add(Integer.toString(r.nextInt(5000)));

Similar Threads

  1. toString method
    By feldmanb700 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 17th, 2011, 09:20 PM
  2. [SOLVED] How to choose a different toString() Besides the default one?
    By Hallowed in forum Java Theory & Questions
    Replies: 12
    Last Post: April 8th, 2011, 03:05 PM
  3. Replies: 2
    Last Post: December 22nd, 2010, 09:21 AM
  4. Valid toString method?
    By dcshoecousa in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 4th, 2010, 11:55 AM
  5. [SOLVED] toString() method
    By chronoz13 in forum Object Oriented Programming
    Replies: 12
    Last Post: January 19th, 2010, 06:44 AM