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 17 of 17

Thread: URGENT HELP NEEDED IN AN HOUR:Printing first number in input

  1. #1
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default URGENT HELP NEEDED IN AN HOUR:Printing first number in input

    Write a Java program ExtractNumber.java to read the data via the keyboard, character
    by character and print the first number (with or without a decimal point) contained in the
    input. For example, given the input data: Mary works for $43.75 per hour, the program
    should print 43.75 to the screen.



    When i entered the sentence "The lady works for 8 hours at $10.00 per hour" ERROR: My output was 810.00 instead of simply the first number 8
    Last edited by Java girl; April 13th, 2014 at 03:47 PM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: URGENT HELP NEEDED IN AN HOUR:Printing first number in input

    Didn't make the deadline. Bummer. And so close.

    Your version apparently collects ALL numbers from the input string rather than collecting only the first occurrence of continuous numbers. Another decision statement to end collection with the end of the first number was required.

  3. #3
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: URGENT HELP NEEDED IN AN HOUR:Printing first number in input

    Yup, sighh. Thanks, how exactly do i go about doing that? would still like to know.

    --- Update ---

    I figured it out thanks for the guidance.

    --- Update ---

    Umm, apparently it isn't working like I thought, sighh, any help?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: URGENT HELP NEEDED IN AN HOUR:Printing first number in input

    Post the new version of the code and explain what the problem is. Post the program's output and add some comments showing what it should be.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: URGENT HELP NEEDED IN AN HOUR:Printing first number in input

    if(Character.isDigit(character)||sentence.charAt(i )== '.')
    {
    if (sentence.charAt(i)== ' ') of the digit
    break;
    System.out.printf ("%c",sentence.charAt(i));
    }

    }


    The program should only print the first integer or double entered.
    The above is the new person.
    I entered the sentence "Jane works for $450.00 an hour for 5 days"
    The program prints 450.005 when it should only print 450.00 which is the first double.
    Last edited by Java girl; April 13th, 2014 at 04:06 PM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: URGENT HELP NEEDED IN AN HOUR:Printing first number in input

    Have you worked out the logic that the code needs to implement?
    List the steps the code needs to take to solve the problem.
    Here's a start:
    ask user for input
    read input into a String
    loop through the String one char at a time
    >>> What needs to be done here inside the loop as each character is obtained?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: URGENT HELP NEEDED IN AN HOUR:Printing first number in input

    check to see if it's a digit

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: URGENT HELP NEEDED IN AN HOUR:Printing first number in input

    Ok that's one part,
    then what does it do next:
    if it is a digit
    if it is NOT a digit

    and then what after that
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: URGENT HELP NEEDED IN AN HOUR:Printing first number in input

    if it is a digit, print it, if not do not print it

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: URGENT HELP NEEDED IN AN HOUR:Printing first number in input

    Is that all? That logic will print ALL of the digits in the String? That is what the current code does.

    Don't you want to stop printing digits after the first non-digit is found following some digits?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: URGENT HELP NEEDED IN AN HOUR:Printing first number in input

    stop printing if digit reaches a white space

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: URGENT HELP NEEDED IN AN HOUR:Printing first number in input

    stop printing if digit reaches a white space
    I'm not sure what that means. Can you explain a little more?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: URGENT HELP NEEDED IN AN HOUR:Printing first number in input

    a white space signifies the end of a digit

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: URGENT HELP NEEDED IN AN HOUR:Printing first number in input

    How will the code know if a white space should stop the printing or not? There will be lots of white spaces in the String.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: URGENT HELP NEEDED IN AN HOUR:Printing first number in input

    I don't know if or where to put that in.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: URGENT HELP NEEDED IN AN HOUR:Printing first number in input

    Describe what the code needs to have to do that.
    The code needs to know if it has found a digit or not. A boolean variable can be used to remember that.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: URGENT HELP NEEDED IN AN HOUR:Printing first number in input

    ok, thank you.

Similar Threads

  1. help needed (URGENT)
    By sour_bee in forum Java Theory & Questions
    Replies: 2
    Last Post: December 16th, 2013, 01:55 PM
  2. URGENT! Due in one hour. Please help!
    By imanoob in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 10th, 2013, 11:25 AM
  3. Please Help. Urgent. For loops and printing to output statement.
    By timisaballer23 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 17th, 2013, 08:20 AM
  4. urgent help needed
    By nono in forum Member Introductions
    Replies: 2
    Last Post: March 10th, 2011, 11:18 AM
  5. Urgent help needed
    By mohit1007 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: June 23rd, 2010, 09:34 PM