Results 1 to 6 of 6
  1. #1
    TheRocket is offline Member
    Join Date
    Nov 2008
    Posts
    20
    Rep Power
    0

    Default Printing out ... depending on character count of previous words entered.

    Ok I have a program which allows the user to input two words, I then use the length() function to find out how many characters are in the words. Is it possible to add this two together and take it away from say 70 and then with the remaining character stick it between the two words.

    Example:

    First word = Hello

    Second word = Goodbye

    5+7 = 12
    70 - 12 = 58

    So it would output,

    Hello............................................. .............Goodbye

    The dots in the middle would vary depending on the length of the two words entered. I don't want to know how to code this just how I would work out how to calculate how many dot's would be needed and then how to print that out.

    Thanks

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    What you have tried so far? You have to do few things.

    1. How to get the user information, first word and last word
    Hint: Use the Scanner class and netLine() method

    2. How to calculate the length of the first word and the last and number of dots.
    Hint: length() of the String class

    3. How to display first word, dots required and the last word on a single line.
    Hint: print(), not println()

  3. #3
    AnGuRuSO is offline Member
    Join Date
    Oct 2008
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by TheRocket View Post
    I don't want to know how to code this just how I would work out how to calculate how many dot's would be needed and then how to print that out.
    Well to calculate how many dots needed you would use code along the lines of:

    int dotsRequires = 70 - ( word1.length() + word2.length());

    To print out the dots you would need a while or for loop. Search up while and for loops in google and you should be able to print out required number of dots.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by AnGuRuSO View Post

    int dotsRequires = 70 - ( word1.length() + word2.length());

    To print out the dots you would need a while or for loop. Search up while and for loops in google and you should be able to print out required number of dots.
    Yes, or else you can do the same thing without using extra variable. Something like this, pretty straight forward,

    Java Code:
            for(int i = 0; i < (70 - (first.length() + last.length())); i++) {
                System.out.print(".");
            }
    Put some effort ans try to answer my three questions, you'll have the solution.

  5. #5
    TheRocket is offline Member
    Join Date
    Nov 2008
    Posts
    20
    Rep Power
    0

    Default

    Thanks a lot guys, with a bit of trial and error from the above I got it :)

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. count character in text file as input file
    By aNNuur in forum New To Java
    Replies: 7
    Last Post: 03-25-2010, 05:01 PM
  2. Replies: 0
    Last Post: 07-07-2008, 05:35 PM
  3. calendar of 12months when year is entered
    By veena in forum New To Java
    Replies: 16
    Last Post: 05-06-2008, 06:03 AM
  4. Replies: 2
    Last Post: 04-28-2008, 11:26 AM
  5. reading text character by character
    By bugger in forum New To Java
    Replies: 2
    Last Post: 11-09-2007, 09:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •