• February 5th, 2013, 10:21 PM
Rain_Maker
Here is my homework assignment. So far I have a very clear understanding on what I need to do. I just need some hints or ideas.

Write a program that requests a 4-digit integer and displays the digits one at a time. For example,
given 7294, your program should print:
First digit: 7
Second digit: 2
Third digit: 9
Fourth digit: 4
Hint: use the % and / operations to extract the digits.

Okay, I know I will use modules operators and division(From what the professor has told me). I will use a scanner class and ask four a four digit INT number. But I don't know how to explain this in java code.

7 % 7294 = 7
2 % 7294 = 2
9 % 7294 = 9
4 % 7294 = 4

Ask you can see if you use the mod operation on a specific digit from the four digit number. Then you will have the reminder which you can output. But I don't know how to tell java to take a particular from that four digit number. Any suggestions guys? :confused:

Thanks Again!

Know thy enemy and it becomes a lot less fearsome…

--- Update ---

particular number*
• February 6th, 2013, 09:16 AM
Norm
Quote:

I don't know how to tell java to take a particular from that four digit number
Can you explain and give an example? Is there a word missing after the word particular?
• February 6th, 2013, 10:21 AM
Rain_Maker
Particular number*.... I put an update below my post. I didn't know how to edit. But thanks for the reply. Do u have any ideas Norm!!
• February 6th, 2013, 10:42 AM
Norm
Can you explain and give an example? What is a "particular number"?
Given 1234
Could the "particular number" be the digit in the second position: '2'
Or if the "particular number" is a 3 then its position is at index = 2
• February 6th, 2013, 01:23 PM
Rain_Maker
Okay Sorry, :o

For example if I enter 4653, the output should display like this.
First digit: 4
Second digit: 6
Third digit: 5
Fourth digit: 3

Is that clearer Norm???:o

--- Update ---

I just would like to be able to display each indivdual digit as a separate int and display it. :-??
• February 6th, 2013, 01:25 PM
Norm
Quote:

Okay, I know I will use modules operators and division
Those are the operators for this. Getting the rightmost digit is easy. I've not tried getting the leftmost first.
Or convert the number to a String and use the String class's methods.
• February 6th, 2013, 05:39 PM
Rain_Maker
Okay I really don't know what I should, I have the knowledge to solve the problem, but just coming with code is another issue. LOL I know its easy for you but how should I do step by step. If give me starting point I pretty sure I can solve this problem.
• February 6th, 2013, 05:54 PM
Norm
Which method of the two I suggested can you try?
Converting to a String is the easier.
• February 6th, 2013, 05:58 PM
Rain_Maker
Okay I don't think I never converted a int to a string. And my text book did not mentioned. Do you mind teaching me some basic? So can play around with it?
• February 6th, 2013, 06:01 PM
Norm
If you are going to use Strings you should read the API doc for the String class and see what methods it has for working with the characters in the String.
The API doc: Java Platform SE 7
• February 6th, 2013, 06:04 PM
Rain_Maker
Okay Thanks let me look at the API for a bit. I get back to you. : )
• February 7th, 2013, 05:59 PM
Rain_Maker
Hey Norm its me again. I haven't got a chance to look at the API. (Its pretty intimidating) I'm going to go back to her hint and use the modules and division operators. Here is my question once I have my four digit integer where should I go from there? I mean I have nothing to divide or run the modules function on my integer. I know I'm missing something to this project but I just don't know what.
• February 7th, 2013, 07:01 PM
Norm
Think of a 4 digit number as the sum of 4 numbers. Say the number is 2345
That is the sum of
2000
300
40
5

To strip off the loworder 0, divide the number by 10
40/10 = 4
(300/10)/10 = 3
((2000/10)/10)/10 = 2

The repeated divisions would be done in a loop
• February 7th, 2013, 08:11 PM
Rain_Maker