I'm new to Java. I've been writing code to find the binary equivalent of an integer in Java using a while loop. I've written the following code and it's not throwing any error, but it was not printing INVALID INPUT. It is printing the valid binary number of a given integer value. Can anyone suggest where and what I'm doing wrong? And how should I get the proper input?
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if ((n >= 0) && n > 999) {
System.out.println("Invalid Input");
} else {
while (n > 1) {
if (n <= 999) {
System.out.println(Integer.toBinaryString(n));
break;
}
}
}
}
}
Change && to ||:
if (!(n >= 0) || n > 999) {
Or even better, express conditions without negation:
if (n < 0 || n > 999) {
Positive conditions, ie "a is b", are easier to read.
Why are you using a loop at all? You're just adding needless complexity. Your code can be re-written to this:
if (n >= 0 && n < 1000) {
System.out.println(Integer.toBinaryString(n));
return;
}
System.out.println("Invalid Input");
You haven't said if this is an assignment but typically it is done like this for such questions.
loop until value is 0
get remainder from division by 2.
prepend to string
divide n by 2 to expose next binary digit.
// get some input,
int n = 1249;
String s = "";
while(n != 0) {
s = (n%2) + s;
n/=2;
}
System.out.println(s);
prints
10011100001
I had to write a program that will receive an int 'n' and another one 'd' - and will print the number n with commas every d digits from right to left.
If 'n' or 'd' are negative - the program will print 'n' as is.
I although had to make sure that there is no commas before or after the number and I'm not allowed to use String or Arrays.
for example: n = 12345678
d=1: 1,2,3,4,5,6,7,8
d=3: 12,345,678
I've written the following code:
public static void printWithComma(int n, int d) {
if (n < 0 || d <= 0) {
System.out.println(n);
} else {
int reversedN = reverseNum(n), copyOfrereversedN = reversedN, counter = numberLength(n);
while (reversedN > 0) {
System.out.print(reversedN % 10);
reversedN /= 10;
counter--;
if (counter % d == 0 && reversedN != 0) {
System.out.print(",");
}
}
/*
* In a case which the received number will end with zeros, the reverse method
* will return the number without them. In that case the length of the reversed
* number and the length of the original number will be different - so this
* while loop will end the zero'z at the right place with the commas at the
* right place
*/
while (numberLength(copyOfrereversedN) != numberLength(n)) {
if (counter % d == 0) {
System.out.print(",");
}
System.out.print(0);
counter--;
copyOfrereversedN *= 10;
}
}
}
that uses a reversNum function:
// The method receives a number n and return his reversed number(if the number
// ends with zero's - the method will return the number without them)
public static int reverseNum(int n) {
if (n < 9) {
return n;
}
int reversedNum = 0;
while (n > 0) {
reversedNum += (n % 10);
reversedNum *= 10;
n /= 10;
}
return (reversedNum / 10);
}
and numberLength method:
// The method receives a number and return his length ( 0 is considered as "0"
// length)
public static int numberLength(int n) {
int counter = 0;
while (n > 0) {
n /= 10;
counter++;
}
return counter;
}
I've been told that the code doesn't work for every case, and i am unable to think about such case (the person who told me that won't tell me).
Thank you for reading!
You solved looping through the digits by reversing the number, so a simple division by ten can be done to receive all digits in order.
The comma position is calculated from the right.
public static void printWithComma(int n, int d) {
if (n < 0) {
System.out.print('-');
n = -n;
}
if (n == 0) {
System.out.print('0');
return;
}
int length = numberLength(n);
int reversed = reverseNum(n);
for (int i = 0; i < length; ++i) {
int nextDigit = reversed % 10;
System.out.print(nextDigit);
reversed /= 10;
int fromRight = length - 1 - i;
if (fromRight != 0 && fromRight % d == 0) {
System.out.print(',');
}
}
}
This is basically the same code as yours. However I store the results of the help functions into variables.
A zero is a special case, an exception of the rule that leading zeros are dropped.
Every dth digit (from right) needs to print comma, but not entirely at the right. And not in front. Realized by printing the digit first and then possibly the comma.
The problems I see with your code are the two while loops, twice printing the comma, maybe? And the println with a newline when <= 0.
Test your code, for instance as:
public static void main(String[] args) {
for (int n : new int[] {0, 1, 8, 9, 10, 234,
1_234, 12_345, 123_456, 123_456_789, 1_234_567_890}) {
System.out.printf("%d : ", n);
printWithComma(n, 3);
System.out.println();
}
}
Your code seems overly complicated.
If you've learned about recursion, you can do it like this:
public static void printWithComma(int n, int d) {
printInternal(n, d, 1);
System.out.println();
}
private static void printInternal(int n, int d, int i) {
if (n > 9) {
printInternal(n / 10, d, i + 1);
if (i % d == 0)
System.out.print(',');
}
System.out.print(n % 10);
}
Without recursion:
public static void printWithComma(int n, int d) {
int rev = 0, i = d - 1;
for (int num = n; num > 0 ; num /= 10, i++)
rev = rev * 10 + num % 10;
for (; i > d; rev /= 10, i--) {
System.out.print(rev % 10);
if (i % d == 0)
System.out.print(',');
}
System.out.println(rev);
}
Are you allowed to use the whole Java API?
What about something as simple as using DecimalFormat
double in = 12345678;
DecimalFormat df = new DecimalFormat( ",##" );
System.out.println(df.format(in));
12,34,56,78
Using...
,# = 1 per group
,## = 2 per group
,### = 3 per group
etc...
It took me a bunch of minutes. The following code snippet does the job well (explanation below):
public static void printWithComma(int n, int d) { // n=number, d=commaIndex
final int length = (int) (Math.log10(n) + 1); // number of digits;
for (int i = 1; i < Math.pow(10, length); i*=10) { // loop by digits
double current = Math.log10(i); // current loop
double remains = length - current - 1; // loops remaining
int digit = (int) ((n / Math.pow(10, remains)) % 10); // nth digit
System.out.print(digit); // print it
if (remains % d == 0 && remains > 0) { // add comma if qualified
System.out.print(",");
}
}
}
Using (Math.log10(n) + 1) I find a number of digits in the integer (8 for 12345678).
The for-loop assures the exponents of n series (1, 10, 100, 1000...) needed for further calculations. Using logarithm of base 10 I get the current index of the loop.
To get nth digit is a bit tricky and this formula is based on this answer. Then it is printed out.
Finally, it remains to find a qualified position for the comma (,). If modulo of the current loop index is equal zero, the dth index is reached and the comma can be printed out. Finally the condition remains > 0 assures there will be no comma left at the end of the printed result.
Output:
For 4: 1234,5678
For 3: 12,345,678
For 2: 12,34,56,78
For 1: 1,2,3,4,5,6,7,8
My code is counting the wrong number of zeroes in a read in text file and I'm not sure how to fix it. Random numbers are coming up either one more than what i need or not reading at all. Can anyone help?
private static int count0(int n, boolean zero) {
if (n <= 0)
return 0;
else if (n % 10 == 0)
return 1 + (zero ? 1 : 0) + count0(n / 10, true);
else
return count0(n / 10, false);
}
public static int count0(int n) {
return count0(n, false);
}
enter code here
Getting rid of 'zero', we have
n is 0 --> count 0
otherwise add 1 if this digit is zero, and
count the zero digits to the left
private static int count0(int n) {
if (n <= 0)
return 0;
else
return (n % 10 == 0 ? 1 :0) + count0(n / 10);
}
This works for (say) original n = 10 but IMO does not work for original n = 0; the answer should surely be 1? That is, 0 is a special case. Both '10' and '0' have one zero.
I recently has a test in university and I was struggling with a problem. The task was defined very specifically as following:
Write a recursive method (don't change the signature, or parameters; no global variables allowed; don't use Strings or the method Stringbuffer; no loops) which returns "true" if the number of zeros in number "n" is odd and "false" if the number of zeros is even.
Signature and Parameter:
public static boolean oddZeros(int n) {
}
So for:
n = 10 //true
n = 100 //false
n = 1402050 //true
n = 0 // true
n = 12 // false
you get the idea..
I understand the concept of recursion but i fail to see how i can count something, given only booleans. I tried adding a counter variable inside the method but whenever i make a recursive call, obviously the variable would be reset to its initialization.
Since this is a very specific problem, i didn't find any solutions so far. How would a method like this look like?
public static boolean oddZeroes(int n) {
if (n < 10) {
return n == 0;
}
return (n % 10 == 0) ^ oddZeroes(n / 10);
}
You can even make it one-liner:
public static boolean oddZeroes(int n) {
return n < 10 ? n == 0 : (n % 10 == 0) ^ oddZeroes(n / 10);
}
And if you want to process negative inputs as well, add something like if (n < 0) {return oddZeroes(-n);} in the beginning, i.e.:
public static boolean oddZeroes(int n) {
if (n < 0) {
return oddZeroes(-n);
}
if (n < 10) {
return n == 0;
}
return (n % 10 == 0) ^ oddZeroes(n / 10);
}
You don't have to count anything.
You only have to observe that:
if you remove a 0 digit from a number that has an odd number of zeroes, the resulting (smaller) number does not have an odd number of zeroes.
if you remove a non 0 digit from a number that has an odd number of zeroes, the resulting (smaller) number also has an odd number of zeroes.
Finally, as the base of the recursion, if 0 < number < 10, it has an even number of 0s (0 0s), so your method should return false.
You can write a shorter implementation, but I preferred readability:
public static boolean oddZeros(int n) {
if (n == 0)
return true;
else if (n < 10)
return false;
else if (oddZeros (n / 10)) {
return n % 10 != 0; // removed digit is not 0
} else {
return n % 10 == 0; // removed digit is 0
}
}
EDIT:
This assumes the input is non-negative. If you need to support negative input, you can add an initial condition of:
if (n < 0) {
return oddZeros (-n);
}
I adding a continue statement to end the current iteration so that the rest of the statement in the loop body is not executed.
public class Main {
public static void main(String[] args) {
int sum = 0;
int number = 0;
while (number < 20) {
number++;
if (number == 10 || number == 11)
continue;
sum += number;
}
System.out.println(sum);
}
}
The things I can't understand is why I will get error if I added {} ?
public class Main {
public static void main(String[] args) {
int sum = 0;
int number = 0;
while (number < 20) {
number++;
if (number == 10 || number == 11) {
continue;
sum += number;
}
}
System.out.println(sum);
}
}
Error
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Unreachable code
at Main.main(Main.java:18)
This will work.
if (number == 10 || number == 11) {
continue;
}
sum += number;
Explanation
When you don't add {} to your if statement, only the next line will be considered. Therefore, you need to leave sum += number outside the {}
Because continue statement is final in your branch (between {}), so next statement (sum += number) will not execute never. Your IDE must warn you of that, that's why it didn't compile it and you got error.
In your first block, the one without { you effectively wrote:
if (number == 10 || number == 11) {
continue;
}
sum += number;
The sum += number is reachable, as long as the expression is false.
In the second you actually wrote:
if (number == 10 || number == 11) {
continue;
sum += number;
}
The sum += number; has become unreachable because if the expression is true it will always be skipped because of the continue, and if the expression is false it will not be executed because it is not in the block statement.
As other answers said correctly, only the first line of code will be run after an if statement. However, another rule is that you cannot add any more lines of code after the "continue" statement in an if statement. So, that is really the error. If you were to tweak the code to switch the "sum += number;" with continue, you won't receive the same error. Hope this helps.
You execute sum += number; inside curly brackets ({}) of your if-block:
if (number == 10 || number == 11) {
continue;
sum += number;
}
This will work:
if (number == 10 || number == 11) {
continue;
}
sum += number;