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

Thread: Java formatting assignment.

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java formatting assignment.

    Hey, new assignment. This time my program needs to read in a width of a line and then format a sentence to create margins.

    For example,

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

    needs to become:

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

    (In java this looks straight)

    The spaces also need to be replaced with .'s (which the program does already) to show it works correctly.

    class Main
    {
        static public void main (String args[]){
            System.out.print("#Width: ");
            int width = 20;
            System.out.println( width );
            System.out.print("#Enter Text: ");
            String text = BIO.getString();
     
            text = text.replace( " ", ".");
            char ch = '.';
            char letters[] = text.toCharArray();
            int amtofchr = text.length();
            int add = count( text, ch) / amtofchr;
     
            while ( ! text.equals ( "END" )){
                System.out.println();
                System.out.printf( "Amount Of Spaces: %d", count( text, ch ));
                System.out.println();
                System.out.printf( "Amount Of Letters: %d", amtofchr );
                System.out.println();
                System.out.printf( "Amount of Spaces To Add: %d", add );
                System.out.println();
                System.out.println();
                System.out.println();
                for ( int i = 0; i<=width; ){
                    for ( int z = 0; z<=width; ){
                    while ( i < text.length() ){
                        System.out.print(letters[i]);
                        i++;
                        z++;
                        if ( z == width ){
                            System.out.println();
                            z = 0;
                        }
                    }
                }
                }
                break;
            }
        }
     
        public static int count( String s, char c )
        {
            String str = s.toLowerCase();
            int count = 0;
            for ( int i = 0; i < str.length(); i++ )
            {
                if ( str.charAt(i) == c ) 
                    count++; 
            }
            return count;
        }
     
    }

    heres my code atm, ive managed to get the program to basically print out the char array of the string.

    I have a theory on how it could work:

    input string
    replace spaces with .'s
    convert string into string array split at .
    calculate how many spaces the sentence has.
    calculate how many characters are left to add till the text.length = 20 (i.e. add= width - text.length)
    calculate how many spaces to add to each word in the array. (i.e. add / howmanyspaces)
    somehow add a space to each word in the array :S
    Print the array out.

    Is this how you would go about it?

    Also tbh you can ignore most of my code, mainly using it to calculate things for the sentence, most of the Amount of spaces stuff will deleted in the final program.

    Thanks for any help
    somehow add the spaces to the word in the array.


  2. #2
    Junior Member
    Join Date
    Oct 2011
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java formatting assignment.

    UPDATE


    Been spending about half my night on it but im still confused on where to add a loop or something. From my current progress below i understand that the if statement is finishing and then the next word is being called before another . is generated but i dont know where to move anything. Everything i have tried has failed

    Results:

    [C] Please wait for processing
    [C] Elapsed time to process request 0.792 seconds
    DataSet 1
    ----------Compilation output--------------------------------------------
    javac Main.java
    classes in application
    class Main
    ----------Data used as input was----------------------------------------
    20
    This is an example
    of text that will
    have straight left
    and right margins
    after formatting
    END
    ----------Expected answer was-------------------------------------------
    This..is..an.example
    of..text..that..will
    have..straight..left
    and...right..margins
    after.....formatting
    ----------Your answer however was [ Excluding lines containing a # ] ---
    This..is..an.example
    of..text..that.will
    have..straight..left.
    and..right..margins.
    after..formatting..
    ----------Differences are-----------------------------------------------
    2,5c2,5
    < of..text..that.will
    < have..straight..left.
    < and..right..margins.
    < after..formatting..
    ---
    > of..text..that..will
    > have..straight..left
    > and...right..margins
    > after.....formatting
    ------------------------------------------------------------------------
    [S] Sorry exercise ci101.java/3.5 was not completed successfully

    class Main
    {
        public static void main( String args[] )
        {
            System.out.print("#Enter Width Of Paragraph:");
            int width = BIO.getInt();
            System.out.print("#Enter Word Here:");
            String text = BIO.getString();
     
            char ch = ' ';
     
            int numberofspaces = count( text, ch);
            int numberofspacesadd = width - text.length();
            int spaceratio = numberofspacesadd % numberofspaces;
     
            String[] words = text.split(" ");
            while ( ! text.equals( "END" )){
                for (int i = 0; i<words.length; i++){
     
                    System.out.print( words[i]);
     
                    if ( i < numberofspaces ){
                        System.out.print( "." );
                    }
           if ( i < spaceratio ){ 
    System.out.print( "." );}
     
                }    
                System.out.println();
                System.out.print("#Enter Word Here:");
                text = BIO.getString();
     
                words = text.split(" ");
     
            }
        }
     
     
        public static int count( String s, char c )
        {
            String str = s.toLowerCase();
            int count = 0;
            for ( int i = 0; i < str.length(); i++ )
            {
                if ( str.charAt(i) == c ) 
                    count++; 
            }
            return count;
        }
    }

Similar Threads

  1. assignment troubles polymorphism (guide for assignment included)
    By tdawg422 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 8th, 2011, 10:01 AM
  2. Gson Formatting
    By techwiz24 in forum Java Theory & Questions
    Replies: 4
    Last Post: October 2nd, 2011, 07:48 PM
  3. HSSF cell date formatting in java
    By masepogurajesh in forum Java SE APIs
    Replies: 1
    Last Post: March 15th, 2011, 08:45 AM
  4. Replies: 1
    Last Post: February 22nd, 2010, 07:20 AM
  5. Replies: 3
    Last Post: August 19th, 2009, 11:30 AM