The Mother Board

Motherboards.org forums. Free tech support, motherboard ID, and more.
It is currently Wed Sep 19, 2018 1:44 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: stupid java question
PostPosted: Wed Sep 22, 2004 9:00 am 
Offline
Pilgrim
Pilgrim

Joined: Wed Sep 01, 2004 9:55 am
Posts: 12
Hey guys I am doing an assignment for Java and I need to find the last digit in a value entered by the user.

for example:
If the user entered: 12344321566
i need to find the last 6
then divide the full # by 7 to see if the remainder is = to 6

even the smallest help is appreciated. thanks


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Sep 22, 2004 9:46 am 
Offline
Black Belt 2nd Degree
Black Belt 2nd Degree

Joined: Tue May 11, 2004 11:18 pm
Posts: 2191
Location: Outworld
The last digit of a number is its remainder (modulus) when divided by 10.

In Java, like in C and C++, the % operator is used for remainder.

So, if your number is stored in a variable of type int called num, this will give you the last digit:

num%10

And this will give you the remainder when divided by 7:

num%7

So, your condition would be:

if (num%7==6) ......


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Sep 22, 2004 9:46 am 
Offline
Black Belt 2nd Degree
Black Belt 2nd Degree

Joined: Fri Feb 28, 2003 2:59 pm
Posts: 2884
Location: Minneapolis, MN
So, an assignment...I'll try to help without doing it for you...by breaking down some implicit functional requirements:

User-entered value has a length, set a variable to that length-1

set a one-character to the value at (length-1)

Check that single-character variable for valid numeric value (yes, you may have defined input as numeric, but this way it'll never crash because of any change(s).)

Convert the character to a numeric value, divide it by seven (or "if less than 7 - it's already a remainder - else if it's 7 or more subtract 7 and the result is a remainder). For larger intigers, divide by seven - forcing answer to a integer-only value, then multiply that value by 7 and subtract it from the original number to obtain the remainder (making sure that you use integer type numerics for all of this, it would get messy if using a floating point number or any decimal places).

Now turn the requirement into Java.

_________________
Better living thru technology...
"Open the Pod Bay Doors, HAL..."
Join Folding team #: 33258


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: Baidu [Spider] and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group