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

Thread: Formatting User input

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Formatting User input

    Hey everyone!

    So, I have been given a piece of work to do for College that requires me to format user input. For example:

    This is an example of text that will have straight left and right margins after formatting

    will transform into:

    This..is..an.example
    of..text..that..will
    have..straight..left
    and...right..margins
    after.....formatting


    The user inputs the width, in the case above being 20. Each line has to therefore be 20 characters long. All spaces are to be replaced with full stops.

    I do not want an answer, and just need help to guide me in where I should start! My progress so far results in

    This.is.an.example.o
    f.text.that.will.hav
    e.straight.left.and.
    right.margins.after.
    formatting..........


    Any guidance is much appreciated, I am at a total loss after working on it for about four hours!!

    public class Main
    {
     
     
        public static void main ( String args[])
     
        {
     
            System.out.println("Please input the desired length for the text");
            int length = BIO.getInt();
     
            System.out.println("Please input the text you wish to edit");
            String text = BIO.getLine();
     
            int number = text.length() % length;
            System.out.println(number);
     
            text = text.replace(' ', '.');
            int i = 0;
            while ( i < text.length() )
            {
                for (int x = 0; x < length; x++)
                {
                    if (i < text.length())
                    {
                        System.out.print (text.charAt(i));
                        i = i + 1;
                    }
     
     
                }
                if (i == text.length())
                {
                    for (int y = 0; y < number; y++)
                    {
                        System.out.print(".");
                    }
                }
                System.out.println();
            }
     
        }
    }
    Last edited by tamtimtom; February 4th, 2014 at 07:46 PM. Reason: I think I put this in the wrong forum, not sure how to change it. I am starting my code from scratch


  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: Formatting User input

    Welcome to the forum! Thanks for taking the time to learn to post code correctly.

    full stop = period or '.', right?

    The general algorithm I would start with is to determine the number of words on the line, the number of characters those words contain, the number of 'padding' characters that must be added, then distribute those padding characters as evenly as possible between the words.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    tamtimtom (February 5th, 2014)

  4. #3
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Formatting User input

    Quote Originally Posted by tamtimtom View Post
    Any guidance is much appreciated, I am at a total loss after working on it for about four hours!!
    You need more "logic" .... more more logic. Not just a simple replace and a bunch of loops!

    First, split the long string into an array of strings, each item containing only one word (no spaces around).

    From "This is an example of ........."

    obtain an array with strings:

    "This" "is" "an" "example" "of" .........

    Then start summing the lengths, taking in account 1 slot between words:

    "this" space "is" (7) is less than 20? continue.
    "this" space "is" space "an" (10) is less than 20? continue.
    "this" space "is" space "an" space "example" (18) is less than 20? continue.
    "this" space "is" space "an" space "example" space "of" (21) is less than 20? No, you have to go back, ignore now "of" and so remains:

    "this" space "is" space "an" space "example"

    The sum of characters (excluding spaces) is 15. You have 5 "slots" left to arrive at 20: distribute the dots '.' uniformly to have:

    this..is..an.example

    And then continue starting again from "of".
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  5. The Following User Says Thank You to andbin For This Useful Post:

    tamtimtom (February 5th, 2014)

  6. #4
    Junior Member
    Join Date
    Feb 2014
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Formatting User input

    Thank you both so much! I shall have another crack today!

    Makes so much more sense to split the words individually and determine how many will go on each line. I worry how my head functions sometimes...

  7. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Formatting User input

    UPDATE:

    I REALLY need help with this, I have been tearing my hair out for days now.

    So far, I have managed to get the top 4 lines working perfectly, but I cannot get the last line to work due to an outOfBounds on the array. While I understand why this is happening, I cannot think of a solution to remedy it. Any help is MUCH appreciated, I really am at a loss for what to do.

     
     
    public class Main
    {
     
        public static int lineLengthIncrease (int lineLength, int newWord, int width)
        {
            if ( lineLength == 0) // Adding the new word, but no space added as the first word does not come after a space
            {
                return (lineLength + newWord);
            }
     
            if (lineLength > width) // To subtract the last word added that caused the line length to exceed 20.
     
            {
     
                return (lineLength - newWord) - 1;
            }
     
     
            else // The +1 is to account for the added space before
            {
                return (lineLength + newWord) + 1;
            }
     
        }
     
        public static int gapCount (int count)
        {
            return (count - 2);
        }
     
        public static int leftoverSpace (int line, int width, int gaps)
        {
            return width - (line - gaps);
        }
     
        public static void main ( String args[])
        {
            System.out.println("Please input the desired length for the text");
            int width = BIO.getInt();
     
            System.out.println("Please input the text you wish to edit");
            String textInput = BIO.getLine();
            String arrayInput [] = textInput.split(" ");
     
            int wordUsed = 0;
            int currentWord = 0;
            int lastline = 0;
     
            // A gap must be made here, as wordNumber does not reset, but count does!!!
            while (currentWord < arrayInput.length){
                int lineLength;
                int wordNumber = 0;
                for ( lineLength = 0; lineLength < width; ){ // The length cannot go over the width.
     
                    lineLength = lineLengthIncrease(lineLength, arrayInput[currentWord].length(), width);
                    if (lineLength < width){
                        wordNumber++;
                        currentWord++;
                    }
     
                    if (currentWord == arrayInput.length){
                        currentWord = (currentWord - wordNumber);
     
                        System.out.println(currentWord);
                        System.out.println(lineLength);
     
     
                    }
     
     
                }
     
     
                lineLength = lineLengthIncrease(lineLength, arrayInput[currentWord].length(), width);
     
                int gapCount = wordNumber - 1;
     
                int leftoverSpace = leftoverSpace (lineLength, width, gapCount);
     
                int adjustedGapCount = gapCount;
                // The number of spaces for the line -- This resets for each line
                int dotUsedCounter = 0;
                currentWord = currentWord - wordNumber;
                for (int currentLineLength = 0; currentLineLength < width;){
     
                    currentLineLength = currentLineLength +(printLine(arrayInput, currentWord));
                    currentLineLength = currentLineLength + (printDot(wordNumber, leftoverSpace, adjustedGapCount, gapCount, dotUsedCounter));
     
                    adjustedGapCount--;
                    currentWord++;
                    dotUsedCounter++;
                }
     
            }
        }
     
        public static int printLine (String arrayInput[], int currentWord)
        {
            System.out.print(arrayInput[currentWord]);
            return arrayInput[currentWord].length();
        }
     
        public static int printDot (int wordNumber, int leftoverSpace, int adjustedGapCount, int gapCount, int extraDotsUsed)
        {
            int dotsPerGap = leftoverSpace / gapCount;
            int extraDots = (leftoverSpace % gapCount);
     
            if (extraDots != 0)
            {
                extraDots = extraDots - extraDotsUsed;
            }
     
            if (adjustedGapCount == 0)
            {
                System.out.println();
                return 0;
            }
     
            if (extraDots == 0){
     
                for (int x = 0; x < dotsPerGap; x++)
                {
                    System.out.print(".");
     
                }
                return dotsPerGap;
            }
     
            if (extraDots != 0){
                for (int x = 0; x < dotsPerGap; x++)
                {
                    System.out.print(".");
                }
     
                System.out.print(".");
     
                return dotsPerGap + 1;
            }
     
     
            return 0;
        }
     
    }

  8. #6
    Junior Member
    Join Date
    Feb 2014
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Formatting User input

    Bump, please can anybody help?

  9. #7
    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: Formatting User input

    Make it easier for us to help you. In this case, post EXACT error messages and sample runs that show the problem. For example, this is what I got when I ran your program:
    Please input the desired length for the text
    20
    Please input the text you wish to edit
    This..is..an.example
    of..text..that..will
    have..straight..left
    and...right..margins
    14
    16
    right.....margins
    after.....15
    10
    afterException in thread "main" java.lang.ArithmeticException: / by zero
    	at TestClass.printDot(TestClass.java:109)
    	at TestClass.main(TestClass.java:91)
    That's not what you're asking for help with, so I can't help with your error until I see it and understand what caused it.

  10. #8
    Junior Member
    Join Date
    Feb 2014
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Formatting User input

     
    public class Main
    {
     
        public static int lineLengthIncrease (int lineLength, int newWord, int width)
        {
            if ( lineLength == 0) // Adding the new word, but no space added as the first word does not come after a space
            {
                return (lineLength + newWord);
            }
     
            if (lineLength > width) // To subtract the last word added that caused the line length to exceed 20.
     
            {
     
                return (lineLength - newWord) - 1;
            }
     
     
            else // The +1 is to account for the added space before
            {
                return (lineLength + newWord) + 1;
            }
     
        }
     
        public static int gapCount (int count)
        {
            return (count - 2);
        }
     
        public static int leftoverSpace (int line, int width, int gaps)
        {
            return width - (line - gaps);
        }
     
        public static void main ( String args[])
        {
            System.out.println("Please input the desired length for the text");
            int width = BIO.getInt();
     
            System.out.println("Please input the text you wish to edit");
            String textInput = BIO.getLine();
            String arrayInput [] = textInput.split(" ");
     
            int wordUsed = 0;
            int currentWord = 0;
            int lastline = 0;
     
            // A gap must be made here, as wordNumber does not reset, but count does!!!
            while (currentWord < arrayInput.length){
                int lineLength;
                int wordNumber = 0;
                for ( lineLength = 0; lineLength < width; ){ // The length cannot go over the width.
     
                    lineLength = lineLengthIncrease(lineLength, arrayInput[currentWord].length(), width);
                    if (lineLength < width){
                        wordNumber++;
                        currentWord++;
                    }
     
     
     
                }
     
     
                lineLength = lineLengthIncrease(lineLength, arrayInput[currentWord].length(), width);
     
                int gapCount = wordNumber - 1;
     
                int leftoverSpace = leftoverSpace (lineLength, width, gapCount);
     
                int adjustedGapCount = gapCount;
                // The number of spaces for the line -- This resets for each line
                int dotUsedCounter = 0;
                currentWord = currentWord - wordNumber;
                for (int currentLineLength = 0; currentLineLength < width;){
     
                    currentLineLength = currentLineLength +(printLine(arrayInput, currentWord));
                    currentLineLength = currentLineLength + (printDot(wordNumber, leftoverSpace, adjustedGapCount, gapCount, dotUsedCounter));
     
                    adjustedGapCount--;
                    currentWord++;
                    dotUsedCounter++;
                }
     
            }
        }
     
        public static int printLine (String arrayInput[], int currentWord)
        {
            System.out.print(arrayInput[currentWord]);
            return arrayInput[currentWord].length();
        }
     
        public static int printDot (int wordNumber, int leftoverSpace, int adjustedGapCount, int gapCount, int extraDotsUsed)
        {
            int dotsPerGap = leftoverSpace / gapCount;
            int extraDots = (leftoverSpace % gapCount);
     
            if (extraDots != 0)
            {
                extraDots = extraDots - extraDotsUsed;
            }
     
            if (adjustedGapCount == 0)
            {
                System.out.println();
                return 0;
            }
     
            if (extraDots == 0){
     
                for (int x = 0; x < dotsPerGap; x++)
                {
                    System.out.print(".");
     
                }
                return dotsPerGap;
            }
     
            if (extraDots != 0){
                for (int x = 0; x < dotsPerGap; x++)
                {
                    System.out.print(".");
                }
     
                System.out.print(".");
     
                return dotsPerGap + 1;
            }
     
     
            return 0;
        }
     
    }

    java.lang.ArrayIndexOutOfBoundsException: 16
    at Main.main(Main.java:56)

    The line in question being:

    lineLength = lineLengthIncrease(lineLength, arrayInput[currentWord].length(), width);


    This is the error I am getting. The code I posted earlier was the wrong one, that was my bad! I know I am getting the error due to the value of currentWord going over the limit of the array. I do not however know what the best way to remedy this is.

  11. #9
    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: Formatting User input

    The array index, currentWord, is being incremented beyond the end of the arrayInput[]. Find out why and fix it. Put a boundary condition on the increment, if necessary.

Similar Threads

  1. User input trouble
    By CruelCoin in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 30th, 2013, 07:07 AM
  2. User Input help
    By dx2731 in forum Java Theory & Questions
    Replies: 3
    Last Post: May 13th, 2013, 12:46 AM
  3. User Input help
    By lanmonster in forum What's Wrong With My Code?
    Replies: 20
    Last Post: January 20th, 2013, 10:44 AM
  4. Take user input
    By frabbi.cse in forum Java Theory & Questions
    Replies: 1
    Last Post: July 22nd, 2012, 12:48 PM
  5. User Input Loop
    By cfmonster in forum Loops & Control Statements
    Replies: 7
    Last Post: August 24th, 2009, 01:52 PM