Results 1 to 7 of 7
Thread: Problem in using Object datatype
- 05-04-2009, 05:19 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 27
- Rep Power
- 0
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.
- 05-04-2009, 05:58 PM #2
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
printing an object without a toString method prints its reference.
- 05-04-2009, 06:57 PM #3
I'm curious why you have a scanner but you aren't using it. I think you mean
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.Java Code:System.out.println("Enter ID please: "); int empID = sc.nextInt(); System.out.println("Your ID: "+empID);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.
- 05-04-2009, 07:24 PM #4
Member
- Join Date
- Apr 2009
- Posts
- 27
- Rep Power
- 0
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.
- 05-04-2009, 10:34 PM #5
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.
-
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.
- 05-05-2009, 11:51 PM #7
Member
- Join Date
- Apr 2009
- Posts
- 27
- Rep Power
- 0
Similar Threads
-
Java Array object problem
By kozik in forum New To JavaReplies: 13Last Post: 04-20-2009, 10:37 PM -
how to pass an arraylist from an object back to the parent object that it was created
By george_a in forum New To JavaReplies: 1Last Post: 03-04-2009, 06:14 PM -
Problem with Date Object
By R O C K Y in forum Advanced JavaReplies: 4Last Post: 02-15-2009, 04:37 PM -
Problems with a complex datatype in a webservice
By lichtbringer in forum Web FrameworksReplies: 2Last Post: 10-29-2008, 05:32 AM -
Parsing a superclass object to subclass object dynamicly
By Andrefs in forum Advanced JavaReplies: 1Last Post: 07-22-2008, 04:27 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks