• Post Reply Bookmark Topic Watch Topic
  • New Topic

Converting an octal to an int but keeping the octal number  RSS feed

 
Joshua Amy
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another day and another question...

I'm trying to write a short program that quizzes the user on addition and subtraction in base 8. My question is this: is it possible to convert an octal string to an int without it getting changing to decimal? Every time I try, it gets converted automatically.

Here's a bit of code (just to try out different avenues) meant to test whether I'm getting the proper output. I'm not. I'm trying to have it take an integer, convert to octal, and return an integer that is that octal number, but it's always converted to decimal. Of course I can get it to print a string octal correctly if I modify the method to return strings only, but it doesn't work once I try to return an integer (that integer is in decimal, which is useless to me).

Any help or insight is hugely appreciated as usual, and worth a firstborn.

 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, that isn't right. An int value isn't decimal or octal, it isn't in any format at all. It's just a number. You can choose to display it as text, as you did, and in that case the default is to display it as decimal. So your idea of converting it to a String in octal format is the right answer.
 
Joshua Amy
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I understand that the number is just a number. You were helpful in getting me to see that I was bloody-mindedly trying to change the default format of the int. I'm trying to refactor a program that adds numbers so that it will quiz a user on octals instead of decimals, and I've discovered that I have a lot more work ahead of me than I thought. Anyway, that's beside the point...thanks again!

Paul Clapham wrote:No, that isn't right. An int value isn't decimal or octal, it isn't in any format at all. It's just a number. You can choose to display it as text, as you did, and in that case the default is to display it as decimal. So your idea of converting it to a String in octal format is the right answer.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joshua Amy wrote:You were helpful in getting me to see that I was bloody-mindedly trying to change the default format of the int.


It sounds like you still think an int has a format. It doesn't.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think that Integer.getInteger does what you think it does. It takes its arguments, and uses it to lookup a system property (through System.getProperty) which it then converts into an Integer. To convert a String into an Integer, use Integer.valueOf.
 
Joshua Amy
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My current, possibly wrong, understanding is that it's a stored value that can be displayed in any radix I designate. I AM blundering through, and I appreciate any insight anyone is willing to give.

Jeff Verdegan wrote:
Joshua Amy wrote:You were helpful in getting me to see that I was bloody-mindedly trying to change the default format of the int.


It sounds like you still think an int has a format. It doesn't.
 
Joshua Amy
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob, thanks. I at least didn't actually make this mistake. What I DID do was frustratedly start running through all of the things that wouldn't immediately throw up exceptions in eclipse right around the time I wanted to toss my notebook out the window last night. My mistake (this time) was posting this code instead of the code in which I diduse valueOf. Anyway, I nevertheless thank you for taking the time to reply...it was still helpful to get a little clearer understanding of Integer.



Rob Spoor wrote:I don't think that Integer.getInteger does what you think it does. It takes its arguments, and uses it to lookup a system property (through System.getProperty) which it then converts into an Integer. To convert a String into an Integer, use Integer.valueOf.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joshua Amy wrote:My current, possibly wrong, understanding is that it's a stored value that can be displayed in any radix I designate. I AM blundering through, and I appreciate any insight anyone is willing to give.


That is correct, if by "it" you mean the int value. What I was getting at is this:



All three of those values are identical. They all have the bit pattern 00000000 00000000 00000000 00001010. Using decimal vs. octal vs. hex for the integer literals in your source code only affects what you see in the source. The values of the int expressions are all the same, and the values stored in variables are all the same.

It sounded like you might have thought the above three each had their own inherent "format" in terms of how they're represented internally.
 
Joshua Amy
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff, thanks. That was the conclusion I'd come to, but you're right to say that I didn't articulate it well.

The patient responses are really helpful and even when I've got something right it's good to see it iterated differently for reinforcement.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!