How does one determine if an integer is octal or decimal when it is not entered as a string?
I have a code that converts decimals to binary, but while testing it I noticed that if an octal was entered it would output the "wrong" result.
I was wondering if there is any way to exclude octals if they are entered as the integer input when the function is called. So if 0123 is used as the parameter instead of 123, the method would run using 123 as the input or return null for an invalid input. The method must take an int as the input.
The wrapper method checks if the decimal is less than 0 and the helper recursively finds the binary string. Finally, the wrapper removes leading 0s and outputs a binary string.
public static String decimalToBinary(int decimal) {
if (decimal < 0) {
return null;
}
String result = xDecimalToBinary(decimal);
/*
* the following section checks for a string of 0s and removes all of them,
* leaving the last one if the string is all 0s
*/
if (result.charAt(0) == '0') {
result.replaceFirst("^0+(?!$)", "");
}
return "0b" + result;
}
private static String xDecimalToBinary(int decimal) {
if (decimal == 0 || decimal == 1) {
return "" + decimal;
}
/*
* builds binary string right to left taking the recursion of decimalToBin of
* decimal / 2 on the left and the remainder of decimal / 2 (%2) on the right
*/
else {
return "" + xDecimalToBinary(decimal / 2) + decimal % 2;
}
}
Note: This is for a class, but this specific functionality is not a part of the assignment, rather it stems from my curiosity.
java
|
show 3 more comments
I have a code that converts decimals to binary, but while testing it I noticed that if an octal was entered it would output the "wrong" result.
I was wondering if there is any way to exclude octals if they are entered as the integer input when the function is called. So if 0123 is used as the parameter instead of 123, the method would run using 123 as the input or return null for an invalid input. The method must take an int as the input.
The wrapper method checks if the decimal is less than 0 and the helper recursively finds the binary string. Finally, the wrapper removes leading 0s and outputs a binary string.
public static String decimalToBinary(int decimal) {
if (decimal < 0) {
return null;
}
String result = xDecimalToBinary(decimal);
/*
* the following section checks for a string of 0s and removes all of them,
* leaving the last one if the string is all 0s
*/
if (result.charAt(0) == '0') {
result.replaceFirst("^0+(?!$)", "");
}
return "0b" + result;
}
private static String xDecimalToBinary(int decimal) {
if (decimal == 0 || decimal == 1) {
return "" + decimal;
}
/*
* builds binary string right to left taking the recursion of decimalToBin of
* decimal / 2 on the left and the remainder of decimal / 2 (%2) on the right
*/
else {
return "" + xDecimalToBinary(decimal / 2) + decimal % 2;
}
}
Note: This is for a class, but this specific functionality is not a part of the assignment, rather it stems from my curiosity.
java
2
Ints don't have leading zeros.
– shmosel
Nov 21 at 3:19
Decimals don't, but octals have the notation 0XX... where the x's are digits. Octals can be entered in the integer field, so I was wondering if error checking could be built for this issue.
– Larry
Nov 21 at 3:20
3
Don't confuse notation with value.
– Sotirios Delimanolis
Nov 21 at 3:22
Could you explain how I am confusing them? 0123 is a valid input in the integer field and has a different value from 123. I am asking if it could be determined which one was entered and have the function return null if it is an octal as opposed to finding the binary equivalent of 0123 (equal to decimal 83).
– Larry
Nov 21 at 3:26
What do you mean by "the integer field"?
– shmosel
Nov 21 at 3:29
|
show 3 more comments
I have a code that converts decimals to binary, but while testing it I noticed that if an octal was entered it would output the "wrong" result.
I was wondering if there is any way to exclude octals if they are entered as the integer input when the function is called. So if 0123 is used as the parameter instead of 123, the method would run using 123 as the input or return null for an invalid input. The method must take an int as the input.
The wrapper method checks if the decimal is less than 0 and the helper recursively finds the binary string. Finally, the wrapper removes leading 0s and outputs a binary string.
public static String decimalToBinary(int decimal) {
if (decimal < 0) {
return null;
}
String result = xDecimalToBinary(decimal);
/*
* the following section checks for a string of 0s and removes all of them,
* leaving the last one if the string is all 0s
*/
if (result.charAt(0) == '0') {
result.replaceFirst("^0+(?!$)", "");
}
return "0b" + result;
}
private static String xDecimalToBinary(int decimal) {
if (decimal == 0 || decimal == 1) {
return "" + decimal;
}
/*
* builds binary string right to left taking the recursion of decimalToBin of
* decimal / 2 on the left and the remainder of decimal / 2 (%2) on the right
*/
else {
return "" + xDecimalToBinary(decimal / 2) + decimal % 2;
}
}
Note: This is for a class, but this specific functionality is not a part of the assignment, rather it stems from my curiosity.
java
I have a code that converts decimals to binary, but while testing it I noticed that if an octal was entered it would output the "wrong" result.
I was wondering if there is any way to exclude octals if they are entered as the integer input when the function is called. So if 0123 is used as the parameter instead of 123, the method would run using 123 as the input or return null for an invalid input. The method must take an int as the input.
The wrapper method checks if the decimal is less than 0 and the helper recursively finds the binary string. Finally, the wrapper removes leading 0s and outputs a binary string.
public static String decimalToBinary(int decimal) {
if (decimal < 0) {
return null;
}
String result = xDecimalToBinary(decimal);
/*
* the following section checks for a string of 0s and removes all of them,
* leaving the last one if the string is all 0s
*/
if (result.charAt(0) == '0') {
result.replaceFirst("^0+(?!$)", "");
}
return "0b" + result;
}
private static String xDecimalToBinary(int decimal) {
if (decimal == 0 || decimal == 1) {
return "" + decimal;
}
/*
* builds binary string right to left taking the recursion of decimalToBin of
* decimal / 2 on the left and the remainder of decimal / 2 (%2) on the right
*/
else {
return "" + xDecimalToBinary(decimal / 2) + decimal % 2;
}
}
Note: This is for a class, but this specific functionality is not a part of the assignment, rather it stems from my curiosity.
java
java
asked Nov 21 at 3:16
Larry
132
132
2
Ints don't have leading zeros.
– shmosel
Nov 21 at 3:19
Decimals don't, but octals have the notation 0XX... where the x's are digits. Octals can be entered in the integer field, so I was wondering if error checking could be built for this issue.
– Larry
Nov 21 at 3:20
3
Don't confuse notation with value.
– Sotirios Delimanolis
Nov 21 at 3:22
Could you explain how I am confusing them? 0123 is a valid input in the integer field and has a different value from 123. I am asking if it could be determined which one was entered and have the function return null if it is an octal as opposed to finding the binary equivalent of 0123 (equal to decimal 83).
– Larry
Nov 21 at 3:26
What do you mean by "the integer field"?
– shmosel
Nov 21 at 3:29
|
show 3 more comments
2
Ints don't have leading zeros.
– shmosel
Nov 21 at 3:19
Decimals don't, but octals have the notation 0XX... where the x's are digits. Octals can be entered in the integer field, so I was wondering if error checking could be built for this issue.
– Larry
Nov 21 at 3:20
3
Don't confuse notation with value.
– Sotirios Delimanolis
Nov 21 at 3:22
Could you explain how I am confusing them? 0123 is a valid input in the integer field and has a different value from 123. I am asking if it could be determined which one was entered and have the function return null if it is an octal as opposed to finding the binary equivalent of 0123 (equal to decimal 83).
– Larry
Nov 21 at 3:26
What do you mean by "the integer field"?
– shmosel
Nov 21 at 3:29
2
2
Ints don't have leading zeros.
– shmosel
Nov 21 at 3:19
Ints don't have leading zeros.
– shmosel
Nov 21 at 3:19
Decimals don't, but octals have the notation 0XX... where the x's are digits. Octals can be entered in the integer field, so I was wondering if error checking could be built for this issue.
– Larry
Nov 21 at 3:20
Decimals don't, but octals have the notation 0XX... where the x's are digits. Octals can be entered in the integer field, so I was wondering if error checking could be built for this issue.
– Larry
Nov 21 at 3:20
3
3
Don't confuse notation with value.
– Sotirios Delimanolis
Nov 21 at 3:22
Don't confuse notation with value.
– Sotirios Delimanolis
Nov 21 at 3:22
Could you explain how I am confusing them? 0123 is a valid input in the integer field and has a different value from 123. I am asking if it could be determined which one was entered and have the function return null if it is an octal as opposed to finding the binary equivalent of 0123 (equal to decimal 83).
– Larry
Nov 21 at 3:26
Could you explain how I am confusing them? 0123 is a valid input in the integer field and has a different value from 123. I am asking if it could be determined which one was entered and have the function return null if it is an octal as opposed to finding the binary equivalent of 0123 (equal to decimal 83).
– Larry
Nov 21 at 3:26
What do you mean by "the integer field"?
– shmosel
Nov 21 at 3:29
What do you mean by "the integer field"?
– shmosel
Nov 21 at 3:29
|
show 3 more comments
1 Answer
1
active
oldest
votes
You can't.
0123 is a way of writing the number "eighty three" in Java.
If you wanted "one hundred and twenty three" then you have to write 123 (or 0173 or 0x7b).
You are telling your method to convert eighty three to binary, and it is doing so. There is no problem with your method.
Thank you so much this makes perfect sense! I thought there was some fancy string conversion that could be done before java sees it as 83, but I guess that is not the case.
– Larry
Nov 21 at 3:46
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404786%2fhow-does-one-determine-if-an-integer-is-octal-or-decimal-when-it-is-not-entered%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can't.
0123 is a way of writing the number "eighty three" in Java.
If you wanted "one hundred and twenty three" then you have to write 123 (or 0173 or 0x7b).
You are telling your method to convert eighty three to binary, and it is doing so. There is no problem with your method.
Thank you so much this makes perfect sense! I thought there was some fancy string conversion that could be done before java sees it as 83, but I guess that is not the case.
– Larry
Nov 21 at 3:46
add a comment |
You can't.
0123 is a way of writing the number "eighty three" in Java.
If you wanted "one hundred and twenty three" then you have to write 123 (or 0173 or 0x7b).
You are telling your method to convert eighty three to binary, and it is doing so. There is no problem with your method.
Thank you so much this makes perfect sense! I thought there was some fancy string conversion that could be done before java sees it as 83, but I guess that is not the case.
– Larry
Nov 21 at 3:46
add a comment |
You can't.
0123 is a way of writing the number "eighty three" in Java.
If you wanted "one hundred and twenty three" then you have to write 123 (or 0173 or 0x7b).
You are telling your method to convert eighty three to binary, and it is doing so. There is no problem with your method.
You can't.
0123 is a way of writing the number "eighty three" in Java.
If you wanted "one hundred and twenty three" then you have to write 123 (or 0173 or 0x7b).
You are telling your method to convert eighty three to binary, and it is doing so. There is no problem with your method.
answered Nov 21 at 3:38
immibis
34.1k43562
34.1k43562
Thank you so much this makes perfect sense! I thought there was some fancy string conversion that could be done before java sees it as 83, but I guess that is not the case.
– Larry
Nov 21 at 3:46
add a comment |
Thank you so much this makes perfect sense! I thought there was some fancy string conversion that could be done before java sees it as 83, but I guess that is not the case.
– Larry
Nov 21 at 3:46
Thank you so much this makes perfect sense! I thought there was some fancy string conversion that could be done before java sees it as 83, but I guess that is not the case.
– Larry
Nov 21 at 3:46
Thank you so much this makes perfect sense! I thought there was some fancy string conversion that could be done before java sees it as 83, but I guess that is not the case.
– Larry
Nov 21 at 3:46
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404786%2fhow-does-one-determine-if-an-integer-is-octal-or-decimal-when-it-is-not-entered%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Ints don't have leading zeros.
– shmosel
Nov 21 at 3:19
Decimals don't, but octals have the notation 0XX... where the x's are digits. Octals can be entered in the integer field, so I was wondering if error checking could be built for this issue.
– Larry
Nov 21 at 3:20
3
Don't confuse notation with value.
– Sotirios Delimanolis
Nov 21 at 3:22
Could you explain how I am confusing them? 0123 is a valid input in the integer field and has a different value from 123. I am asking if it could be determined which one was entered and have the function return null if it is an octal as opposed to finding the binary equivalent of 0123 (equal to decimal 83).
– Larry
Nov 21 at 3:26
What do you mean by "the integer field"?
– shmosel
Nov 21 at 3:29