Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## Question about homework problem.

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?

Thanks Again!

Know thy enemy and it becomes a lot less fearsome…

--- Update ---

particular number*

2. ## Re: Question about homework problem.

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?

3. ## Re: Question about homework problem.

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!!

4. ## Re: Question about homework problem.

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

5. ## Re: Question about homework problem.

Okay Sorry,

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???

--- Update ---

I just would like to be able to display each indivdual digit as a separate int and display it.

6. ## Re: Question about homework problem.

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.

7. ## Re: Question about homework problem.

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.

8. ## Re: Question about homework problem.

Which method of the two I suggested can you try?
Converting to a String is the easier.

9. ## Re: Question about homework problem.

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?

10. ## Re: Question about homework problem.

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

11. ## Re: Question about homework problem.

Okay Thanks let me look at the API for a bit. I get back to you. : )

12. ## Re: Question about homework problem.

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.

13. ## Re: Question about homework problem.

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

14. ## The Following User Says Thank You to Norm For This Useful Post:

Rain_Maker (February 7th, 2013)

15. ## Re: Question about homework problem.

Thanks Norm I solved it!