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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: Question about homework problem.

  1. #1
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default 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. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default 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. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default 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. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default 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. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Question about homework problem.

    Which method of the two I suggested can you try?
    Converting to a String is the easier.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default 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. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Question about homework problem.

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

  12. #12
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default 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. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

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

    Rain_Maker (February 7th, 2013)

  15. #14
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Question about homework problem.

    Thanks Norm I solved it!

Similar Threads

  1. Homework help
    By hockey15 in forum Object Oriented Programming
    Replies: 3
    Last Post: September 23rd, 2012, 11:28 PM
  2. If else question/problem
    By Callcollect in forum Loops & Control Statements
    Replies: 1
    Last Post: November 22nd, 2011, 09:23 AM
  3. Problem-specific design question
    By olsonpm in forum Object Oriented Programming
    Replies: 6
    Last Post: July 20th, 2011, 02:00 PM
  4. Homework problem (scanner problems)
    By TommyFiz in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: September 20th, 2009, 06:10 PM
  5. [SOLVED] What is cast operator and how to use it?
    By napenthia in forum Java Theory & Questions
    Replies: 11
    Last Post: April 27th, 2009, 06:11 AM