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 2 of 2

Thread: Help with Casting Vectors and String.valueOf()

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help with Casting Vectors and String.valueOf()

    Hi guys,

    I'm new to this forum but if you guys could help me out real quick, I would appreciate it.

    So I made this vector which is full of integers that I parsed previously from Strings. So the code looked something like:
    size = Integer.parseInt(sizeString);
    showSize.addElement(size);
    size is an integer, sizeString is a String, showSize is a vector.

    This part works flawlessly but when I add this part of the code:
    showString = String.valueOf(((int)showSize.get(0)));
    I get an error saying "inconvertible types found: java.lang.Object required: int"

    I thoguht that the casting of the int of showSize will make the parameters an int but I guess not. Any suggestions on how to fix this? Thank you in advance!


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Help with Casting Vectors and String.valueOf()

    Autoboxing is taking place, converting your int to an Integer.
    Use: Vector<Integer> vector = new Vector<Integer>(); where the Integer is specified rather than assumed
    or: Vector<Integer> vector = new Vector<>();
    but not: Vector vector = new Vector();
    When you get your value back out it is in Integer form, and should auto-unbox itself if you just remove the cast.

Similar Threads

  1. Casting error using String with generics
    By DOLZero in forum What's Wrong With My Code?
    Replies: 12
    Last Post: March 16th, 2013, 08:13 PM
  2. Double.valueof wont work for formated string
    By sstavrou in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 8th, 2013, 12:35 PM
  3. Casting java.lang.String to a custom class
    By Tyluur in forum What's Wrong With My Code?
    Replies: 14
    Last Post: July 29th, 2012, 08:51 PM
  4. [SOLVED] why casting int to String is not possible through brackets method
    By voltaire in forum Java Theory & Questions
    Replies: 2
    Last Post: May 2nd, 2010, 04:00 PM
  5. reading string input then casting it to an int?
    By etidd in forum Java Theory & Questions
    Replies: 2
    Last Post: March 27th, 2010, 11:49 PM