Reputation: 199
public class ConsecutiveChecker
{
public static void main( String[] args )
{
java.util.Scanner keyboardReader = new java.util.Scanner(System.in);
int number = keyboardReader.nextInt();
int w;
int x;
int y;
int z;
// w= fourth digit, x= third digit, y= second digit, z= first digit
w = (number - (number % 1000)) / 1000;
x = ((number - (number % 100)) % 1000) / 10;
y = ((number - (number % 10)) % 100) * 10;
z = (1000)*(number % 10);
boolean isOneDigitConsecutive;
isOneDigitConsecutive = (number <= 9);
boolean isTwoDigitConsecutive;
isTwoDigitConsecutive = (w = 0) && (x = 0) && (y <= 9) && (y - z ==1);
boolean isConsecutive = (isOneDigitConsecutive || isTwoDigitConsecutive || isThreeDigitConsecutive || isFourDigitConsecutive)
System.out.println();
}
}
Hi,
I'm new to Java and I have to write a code that detects whether a 4 digit number(user entered) is consecutive or not using boolean variable. Consecutive as in 0543, 1234, 0009, 0034(a single digit counts as consecutive). I've written this part of code until now, the problem is I don't understand why is my line
boolean isTwoDigitConsecutive;
isTwoDigitConsecutive = (w = 0) && (x = 0) && (y <= 9) && (y - z ==1);
wrong. it says that i can't use && with Int.
I would like to have some clarifications on how to use boolean variable.
Thank you in advance.
*edit: Thank you for your help, I listened to your advice and changed my code accordingly.
Upvotes: 0
Views: 914
Reputation: 5533
In addition to everything said here, your code can be cleaner and more efficient.
public class ConsecutiveChecker {
/**
* return true if number digits are consecutively increasing.
*
* @param num
* @return
*/
private static boolean isConsecutive(int num) {
if(num < 0 || num > 9999) throw new RuntimeException("Input out of range (0-9999)");
// We check if the digits are downward consecutive from right to left
// (which is same as upward consecutive from left to right, but easier
// and more efficient to compute).
while (num > 0) {
int leftDigit = num%10;
int secondFromLeftDigit = (num/10)%10;
if (leftDigit < secondFromLeftDigit) return false;
num = num/10;
}
// if number is now 0, it's consecutive:
return true;
}
public static void main(String[] args) {
try {
java.util.Scanner keyboardReader = new java.util.Scanner(System.in);
int number = keyboardReader.nextInt();
System.out.println(
"Input number is " +
(isConsecutive(number) ? "" : "not ") +
"consecutive"
);
} catch (Exception e) {
System.out.println("Somthing is wrong with the input: ");
e.printStackTrace();
}
}
}
Upvotes: 0
Reputation: 1076
I understand your concern. Unlike C language, java only accepts boolean with these operators.
in C nonzero is true and zero is false
(w=0) would return 0 which is integer. while (w==0) will return true/false which is boolean.
although you might have made a logical error by writing w=0 instead of w==0, C compiler would not generate a compilation error as it gets a value. Whereas java needs boolean. Thus it will show an error.
Upvotes: 0
Reputation: 960
You have two issues:
1- As Amir said:
isTwoDigitConsecutive = (w == 0) && (x == 0) && (y <== 9) && (y - z ==1);
2- Small fix to w,x,y,z
w = (number - (number % 1000)) / 1000;
x = ((number - (number % 100)) % 1000) / 100;
y = ((number - (number % 10)) % 100) / 10;
z = (number % 10);
Also dont forget to initialize the variables isThreeDigitConsecutive and isFourDigitConsecutive
Upvotes: 0
Reputation: 2168
Try isTwoDigitConsecutive = (w == 0) && (x == 0) && (y <== 9) && (y - z ==1);
When you use the ==
sign, the compiler checks for equality. If you're using a single =
, it assigns the right part of the =
sign to the left part.
Upvotes: 2