• Post Reply Bookmark Topic Watch Topic
  • New Topic

Validating length of digit without using string  RSS feed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sean Marquis
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you need to know the length of the number?
 
Sean Marquis
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Sean Marquis
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sean Marquis
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The number is being held as int in the number variable

basically in
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sean Marquis
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sean Marquis
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

?
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some thing like 

Yes, something like that.  Write some short test lines of code to see what happens.
 
Sean Marquis
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I can not remember how long it first took.  It was a long time ago
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!