How does one determine if an integer is octal or decimal when it is not entered as a string?












1














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.










share|improve this question


















  • 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
















1














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.










share|improve this question


















  • 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














1












1








1







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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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














  • 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












1 Answer
1






active

oldest

votes


















0














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.






share|improve this answer





















  • 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











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
});


}
});














draft saved

draft discarded


















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









0














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.






share|improve this answer





















  • 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
















0














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.






share|improve this answer





















  • 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














0












0








0






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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'