Reputation: 31
I need to write a Java program that prompts the user to enter an integer consisting of exactly 2 digits; then displays on the screen the sum of its individual digits.
I am stuck here. What am I doing wrong?
import java.util.Scanner ;
public class ss {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int x;
System.out.println("Please Enter a number consists of 2 digits only : ");
x = input.nextInt();
x.length() == 2;
}
}
and the last line contains an error!
Upvotes: 1
Views: 23879
Reputation: 35
You cannot call length on integer just write
if(x>=10 && x<=99)
{
//write your code here
}
Upvotes: 0
Reputation: 127
You can't find the length of an int
by calling a method on it, but you can find the length of a String
.
Try converting the int
to a String
and finding the length of that:
boolean isTwoDigits = x.toString().length() == 2;
Upvotes: 0
Reputation: 25327
Your error comes because x
is a primitive, not an object. Only objects have methods like length()
. A quick an easy way to determine the length of an integer is by using Math.log()
.
public int length(int n){
if (n == 0) return 1; // because Math.log(0) is undefined
if (n < 0) n = -n; // because Math.log() doesn't work for negative numbers
return (int)(Math.log10(n)) + 1; //+1 because Math.log10 returns one less
//than wanted. Math.log10(10) == 1.
}
This method uses the fact that the base b logarithm of an integer a is related to the length of the integer a.
Or, if you don't know how to use methods, you could do this (assuming n is the integer to check):
int length = (n == 0)? 1: ((n > 0)? (int) (Math.log(n)) + 1: (int) (Math.log(-n)) + 1);
Or, if you don't use the ternary operator, you could expand it:
int length = -1; //placeholder; might not need it.
if (n == 0) length = 1;
else if (n > 0) length = (int) (Math.log(n)) + 1;
else length = (int) (Math.log(-n)) + 1;
Upvotes: 0
Reputation: 70919
In a programming langauge, there are things called L-values and R-values. In an assignment operation, a L-value can accept a R-value as input. This comes from the typical layout which has L-values on the left of the assignment operator and R-values on the right side of the assignment operator.
x = 5;
x is the L-value and 5 is the R-value. It is possible to assign five to x.
However, a function returns a R-value. Therefore, it is possible to do this
x = a.length();
but is is not possible to do
a.length() = x;
because you can not assign a value to the return of a function.
Fundamentally, L-values are names which represent a value, but R-values are values or items which when analyzed result in the return of values.
Now, if you used the equals comparison operator, both values must be R-values, because no assignment is being performed
a.length == x
is just fine, because it is not the assignment operator =
but rather one of the comparison operators ==
.
Upvotes: 0
Reputation: 178263
The variable x
is of type int
, so you can't call a method on it. You need to either read the input as a String
or convert the int
to a String
then call length()
, or just test that the int
is between 10
and 99
, inclusive.
Upvotes: 1
Reputation: 236034
Assuming that x
is positive, a simple way to check if it has exactly two digits would be:
if (x >= 10 && x <= 99) {
// x contains exactly two digits
}
Upvotes: 9