Results 1 to 8 of 8
  1. #1
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default Stack (collection) to int via String

    Java Code:
     public int getOldXIndex() {
       
            oldXIndex.pop();
            String s = oldXIndex.toString();
            int i= Integer.parseInt(s);
            return i;
        }
    this is the problem[], but how can i get this 9 to an int?

    Error: Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "[9]"

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default

    What is oldXIndex? What happens when you call its toString()? What are you doing with the value you pop from it?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default

    Hi,

    oldXIndex? its the Stack variable and it holds a [10].
    With the pop it becomes [9]

    When i call to string it seems to get "[9]" and thats when the numberformat exception comes, because of [].

    The question would be, how do i get a good int of an Item in a Stack collection.

    Maybe there are other ways.

    Kind regards,

    André

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default

    I'm pretty sure you don't want to be using the Stack's toString(). I'm still not clear what you're trying to do- are you trying to print the last item popped? The item at the top? The size? Something else?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default

    The item after a pop, so this is the last item.

  6. #6
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default

    Think away the pop. I want to convert the last item of a Stack into an int.

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default

    Are you looking for the peek() method?

    Recommended reading: Stack (Java Platform SE 6)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

Similar Threads

  1. Stack (push/pop/top)
    By sehudson in forum New To Java
    Replies: 1
    Last Post: 03-08-2011, 02:40 AM
  2. Stack
    By kayln in forum Eclipse
    Replies: 0
    Last Post: 03-04-2011, 08:14 PM
  3. How big is the stack??
    By mgeno216 in forum New To Java
    Replies: 6
    Last Post: 03-04-2011, 08:04 AM
  4. Counting vowles in a collection of String objects
    By sunde887 in forum New To Java
    Replies: 4
    Last Post: 01-18-2011, 07:22 AM
  5. Need help please – implementing a stack!
    By sfe23 in forum New To Java
    Replies: 0
    Last Post: 02-24-2009, 03:19 PM

Posting Permissions

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