Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

int cannot be dereferenced (please help me!!!!)

Status
Not open for further replies.

ilovelinux2006

Programmer
Jun 6, 2006
32
US
Hey everyone,

I am creating a simple Java program. Thats suppose to output:

Enter integer of up to 4 digits in size: 368
Thousands place: 0
Hundreds place: 3
Tens place: 6
Ones place: 8

So im using the int:

Scanner keyinput = new Scanner(System.in);
System.out.print("Enter integer of up to 4 digits in size: ");
int digit = keyinput.nextInt();
int v1;
v1 = digit.substring(3,4);
System.out.println("Thousands place: " + v1);


But I keep getting this error when i compile the program:

int cannot be dereferenced
v2 = digit.substring(3,4);
^

I realize I cannot use substring with my variable digit when digit is an int. How can I enter only a 3 digit number, and receive a thousands place? Please help me fix!
 
You're better off only working with integers:

Code:
ones = digit % 10 ;
tens = digit / 10 % 10 ;
hundreds = digit / 100 % 100 ;
thousands = digit / 1000 ;
 
If you must use strings, digit.toString() can then be manipulated with substring and other String methods.
 
To repeat Stefan's statement :-

int is a primitive and has no methods. You cannot call a toString method on it.

If you want a string version of a primitive integer, use the static method of the Integer wrapper.

Code:
String strDigits = Integer.toString(digit);

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top