Results 1 to 3 of 3

Thread: Object to Int

  1. #1
    Slaughterman is offline Member
    Join Date
    Sep 2009
    Posts
    11
    Rep Power
    0

    Default Object to Int

    Hey guys,

    I have a little problem with one of my program. I have a server/client scheme and I'm receiving a int value from my client into my server.
    The thing is my server see it as an Object, and I need to change it back to an Int because later on my client I have a function which permit to lower the int number on my server. Don't really know if my explanation is very clear, but anyway here is the part of the code (summarized) which cause me trouble:

    public String doLesson(){
    int places = 0;
    places = Integer.parseInt(receive()); // Solution not working i found on google
    Lesson i = new
    Lesson(lessonNo,description,places.intValue(),loca tion);
    server.put(lessonNo, i);
    return "Lesson added";
    }
    It's strange because for a double value for example I have no problem at all:

    public String doLesson(){
    Double places = 0.00;
    places = (Double) receive(); // If only it was this simple with int, just adding (Int) receive(); - but not working :(
    Lesson i = new
    Lesson(lessonNo,description,places.doubleValue(),l ocation);
    server.put(lessonNo, i);
    return "Lesson added";
    }
    Any ideas ?

    Cheers,

    Clement

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    When you System.out.println(recieve()); what does it look like? Because the Integer.parseInt(...) is trying to parse that exact string; my guess is that it doesn't look like an integer.
    My Hobby Project: LegacyClone

  3. #3
    Slaughterman is offline Member
    Join Date
    Sep 2009
    Posts
    11
    Rep Power
    0

    Default

    System.out.print("Enter number of places\t");
    String places = getString();
    send(new Integer(places));
    I found the solution, here is the updated code:

    public String doLesson(){

    int places = 0;

    places = (Integer) receive();

    Lesson i = new
    Lesson(lessonNo,description,places,location);
    server.put(lessonNo, i);
    return "Lesson added";
    }
    Sorry for the trouble :)

Similar Threads

  1. Replies: 2
    Last Post: 11-26-2010, 02:12 AM
  2. Replies: 1
    Last Post: 03-04-2009, 06:14 PM
  3. Replies: 1
    Last Post: 07-22-2008, 04:27 PM
  4. Replies: 1
    Last Post: 04-17-2008, 10:10 PM
  5. Creating object of Type Object class
    By venkatv in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 03:33 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
  •