Results 1 to 7 of 7
  1. #1
    mfaizan24 is offline Member
    Join Date
    Apr 2009
    Posts
    27
    Rep Power
    0

    Cool Problem in using Object datatype

    I got problem in using Object Datatype its accepting value but the output coming in int format...
    I'm using this codes.

    import java.io.*;
    import java.util.*;
    import java.util.Scanner.*;

    class Employee{
    public static void main(String args[]){
    Scanner sc = new Scanner(System.in);

    try{
    Object empID;
    System.out.print("enter ID please : " );
    empID = (Object) System.in.read();
    System.out.print("Your ID : " + empID);
    }
    catch(IOException e){
    System.out.print(e.getMessage());}
    }
    }

    it will be highly appreciated if anyone tell me how to use object datatype.

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    printing an object without a toString method prints its reference.

  3. #3
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    I'm curious why you have a scanner but you aren't using it. I think you mean
    Java Code:
    System.out.println("Enter ID please: ");
    int empID = sc.nextInt();
    System.out.println("Your ID: "+empID);
    There's no need to do whatever you're doing. If you know it's going to input an int then make the variable an int and use your scanner to read in an int.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  4. #4
    mfaizan24 is offline Member
    Join Date
    Apr 2009
    Posts
    27
    Rep Power
    0

    Default

    I was trying to read value through nextObject() but i realize that we dont have such function in java nextObject().. then i tried (Object) System...
    and its gone through but stuck in wrong output.
    Actually i want to use this object data type to accept unique ID which contains int and string together.

  5. #5
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    you can read it in as a String and use .equalsIgnoreCase() to determine if it's unique. Or just add empID.toString() to your print statement. You could alternately typecast empID to a String. trying to print out empID in its object form will just print the object reference or call the inherited toString method, which for Object prints the reference I think
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Agrees with Mr. X...X. Read the data in as a String using your Scanner object. You can always manipulate the String later if you need to extract the int part or the char parts.

  7. #7
    mfaizan24 is offline Member
    Join Date
    Apr 2009
    Posts
    27
    Rep Power
    0

Similar Threads

  1. Java Array object problem
    By kozik in forum New To Java
    Replies: 13
    Last Post: 04-20-2009, 10:37 PM
  2. Replies: 1
    Last Post: 03-04-2009, 06:14 PM
  3. Problem with Date Object
    By R O C K Y in forum Advanced Java
    Replies: 4
    Last Post: 02-15-2009, 04:37 PM
  4. Problems with a complex datatype in a webservice
    By lichtbringer in forum Web Frameworks
    Replies: 2
    Last Post: 10-29-2008, 05:32 AM
  5. Replies: 1
    Last Post: 07-22-2008, 04:27 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
  •