Forums Register Login

Validating length of digit without using string

+Pie Number of slices to send: Send
Hey guys, new here doing some work for my CIST 1400 so I'll most likely be posting a lot.

Anyway, I'm working on an octal conversion code that takes an octal number and converts it to a regular number in decimal using two methods( a main and convert method). So for my main(which is the validation) I have this code; we can't use string as that's beyond the material learned so far


It doesn't seem to compile as it gives me this error

Wondering how to fix this?
+Pie Number of slices to send: Send
The log10() method takes a double parameter and returns a double result. You can implicitly cast from int data type to double data type, which is why the method call is fine, but you can't implicitly cast the double result back to an int type.

How to fix? Well, first, determine if the int value is what you want, as you will lose precision. Once you determine that it is what you want, then you can explicitly cast the double type back to an int type.

Henry
+Pie Number of slices to send: Send
So Math.log uses double types only then? Is there a way to validate without using Math.log, it seemed like a good idea but might be a little complicated to use now
1
+Pie Number of slices to send: Send
Welcome to the Ranch

Your line 6 doesn't give you an octal number. It gives you a number without 8 or 9 maybe, but in decimal. If you look through the Scanner documentation, you shou‍ld find a way to correct that.
+Pie Number of slices to send: Send
Why do you need to know the length of the number?
+Pie Number of slices to send: Send
I am checking the length to validate and make sure the length isn't greater than 8. From my instructor

"Your program should prompt the user to enter a number of no greater than 8 digits. If they enter a number greater than 8 digits or a
value less than 0, it should re-prompt the user to enter a number again. You do not need to check if the digits are valid octal
numbers (0-7), as this is guaranteed."

All I am doing is making sure I have the length validation dwon as of now
+Pie Number of slices to send: Send
I don't believe that log-base-10 is the correct function to use to find the number of digits, when given a number in base-8.
+Pie Number of slices to send: Send

Why do you actually need the length to perform the validation?  After all, aren't you just confirming that the number is less than 80,000,000?

Henry
+Pie Number of slices to send: Send
Sorry everyone, maybe I'm not being specific enough.

An octal number is up to 8 digits long in length and uses numbers 0-7

What my assignment is basically having us do is use two methods; one to validate and one to confirm.
For validation i would have to check the following

1.) The number is 8 digits long
2.) Does not use any digit less than 0 or greater than 7

I thought that by using log I would be able to validate the length but maybe that's not the best solution.
+Pie Number of slices to send: Send
 

we can't use string


Where is the number's value  held? What kind of variable?
In decimal a number with less than 3 digits will have a value less than 100. 00 to 99
In octal a number with less than 3 digits will have a value less than 0100 (leading 0 says octal) 00 to 077
+Pie Number of slices to send: Send
The number is being held as int in the number variable

basically in
+Pie Number of slices to send: Send
Then the number of digits for a number represented in an octal base can be tested for by using ranges of values.  For example less than 2 digits would have a value less then 010.
+Pie Number of slices to send: Send
 

Norm Radder wrote:Then the number of digits for a number represented in an octal base can be tested for by using ranges of values.  For example less than 2 digits would have a value less then 010.



Where are you getting 010?
+Pie Number of slices to send: Send
010 is an octal number.

Think of decimal numbers. For the value in an int to have a single digit, its value must be less than 10.  Two digits would be less than 100 and greater than 9 (10 to 99). In octal two digits would be less than 0100 and greater than 07.
+Pie Number of slices to send: Send
 

Norm Radder wrote:010 is an octal number.

Think of decimal numbers. For the value in an int to have a single digit, its value must be less than 10.  Two digits would be less than 100 and greater than 9 (10 to 99). In octal two digits would be less than 0100 and greater than 07.



That makes sense, thank you.

Now when you said

Then the number of digits for a number represented in an octal base can be tested for by using ranges of values


How would I implement this?
Some thing like

?
+Pie Number of slices to send: Send
 

Some thing like  


Yes, something like that.  Write some short test lines of code to see what happens.
+Pie Number of slices to send: Send
I will; unrelated question. How long did it take for you and everyone else to get good at this? I appreciate the help greatly but often I end up being even more confused

I asked my instructor and though he said it varies from person to person he didn't give me a concrete answer as to how much I should be studying/practicing a day. Any suggestions for a frustrated newbie?
+Pie Number of slices to send: Send
Sorry, I can not remember how long it first took.  It was a long time ago
+Pie Number of slices to send: Send
 

Sean Marquis wrote:. . . How long did it take for you and everyone else to get good at this? . . . how much I should be studying/practicing a day. Any suggestions for a frustrated newbie?

What are you doing? Are you a student? You will spend about 33%‑40% of your working week in lectures classes practicals or labs. Spend about half the remainder reading round the subject. And about half the remainder doing exercises directly related to it, including programming.
+Pie Number of slices to send: Send
 

Sean Marquis wrote:. . . Some thing like
?

Yes, but I would check for errors rather than correct values:-Did you really write 09? That won't compile. Surely you mean 00? 0, 00, 0b0 (binary) and 0x0 (hexadecimal) shou‍ld be regarded as interchangeable. The underscores in number literals were introduced in Java7.
She said she got a brazillian. I think owning people is wrong. That is how I learned ... tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 679 times.
Similar Threads
Having trouble with odd results from octal-decimal converter
Octal to decimal using while loop
Lost code Binary to decimal using array
help with arrays program in java
read int and encrypt it
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 03:14:56.