Results 1 to 6 of 6
  1. #1
    MrKrow is offline Member
    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0

    Default Help on File Processing Problem (WordWrap)

    Hello, I have been independently studying my java textbook I will be using in the fall, with the text came a website my course uses which is essentially the book problems but with answer verification. I am on a problem that is rather difficult, and my code is so very, very close. Please is anyone can help me complete this, I would be grateful.

    Link to the Problem

    And for those who wish to not use the link I will post it here as well.

    "Write a method called wordWrap that accepts a Scanner representing an input file as its parameter and outputs each line of the file to the console, word-wrapping all lines that are longer than 60 characters. For example, if a line contains 112 characters, the method should replace it with two lines: one containing the first 60 characters and another containing the final 52 characters. A line containing 217 characters should be wrapped into four lines: three of length 60 and a final line of length 37."

    Something worth noting is that for this problem I do not need to construct a class because the online IDE generates one for me when I compile, and adding a class of my own will not allow me to submit a answer.


    Java Code:
    public static void wordWrap(Scanner input) {
        while(input.hasNextLine()) {
            
            String line = input.nextLine();
            int length = line.length();
            
            if(length >= 60) {
                for(int i = 0; i < length; i++ ) {
                    if((i % 59) == 0 && i != 0) {
                        System.out.print(line.charAt(i));
                        System.out.println();
                    } else {
                        System.out.print(line.charAt(i));
                    }
                }
                System.out.println();
            } else {
                System.out.println(line);
            }
        }
    }
    There are 5 cases that the website tests to make sure the program meets the specifications, below I will post what my output is and what the expected it.
    Reviewing the differences myself, I can ascertain that their is an issue with how my method splits up the characters because each of my lines has 61 characters,
    the only issue is if i reduce my method to checking for length of 59 instead of 60 then my lines print out 59 characters. I seem to be stuck between only getting 59 or 61.

    Case 1:
    My output - Failed

    We're no strangers to love, You know the rules and so do I,
    A full commitment's what Im thinking of, You wouldn't get t
    his from any other guy.
    I just wanna tell you how I'm feeling, Gotta make you unders
    tand.
    Never gonna give you up, Never gonna let you down, Never gon
    na run around and desert you.
    Never gonna make you cry, Never gonna say goodbye, Never gon
    na tell a lie and hurt you.

    Expected output -

    We're no strangers to love, You know the rules and so do I,
    A full commitment's what Im thinking of, You wouldn't get th
    is from any other guy.
    I just wanna tell you how I'm feeling, Gotta make you unders
    tand.
    Never gonna give you up, Never gonna let you down, Never gon
    na run around and desert you.
    Never gonna make you cry, Never gonna say goodbye, Never gon
    na tell a lie and hurt you.

    Case 2:
    My Output - Failed

    We're no strangers to love, You know the rules and so do I,
    A full commitment's what Im thinking of, You wouldn't get t
    his from any other guy. I just wanna tell you how I'm feeli
    ng, Gotta make you understand. Never gonna give you up, Nev
    er gonna let you down, Never gonna run around and desert yo
    u. Never gonna make you cry, Never gonna say goodbye, Never
    gonna tell a lie and hurt you.

    Expected Output -

    We're no strangers to love, You know the rules and so do I,
    A full commitment's what Im thinking of, You wouldn't get th
    is from any other guy. I just wanna tell you how I'm feeling
    , Gotta make you understand. Never gonna give you up, Never
    gonna let you down, Never gonna run around and desert you. N
    ever gonna make you cry, Never gonna say goodbye, Never gonn
    a tell a lie and hurt you.

    Case 3:
    My Output - Failed

    To be, or not to be- that is the question: Whether 'tis n
    obler in the mind to suffer The slings and arrows of out
    rageous fortune Or to take arms against a sea of trouble
    s, And by opposing end them. To die- to sleep- No mor
    e; and by a sleep to say we end The heartache, and the t
    housand natural shocks That flesh is heir to.

    Expected Output -

    To be, or not to be- that is the question: Whether 'tis n
    obler in the mind to suffer The slings and arrows of outr
    ageous fortune Or to take arms against a sea of troubles,
    And by opposing end them. To die- to sleep- No more;
    and by a sleep to say we end The heartache, and the thous
    and natural shocks That flesh is heir to.

    Case 4:
    My Output - Failed

    Hello
    How are you
    The quick brown fox jumps over the lazy dog the quick brown
    fox jumps over the lazy dog
    I am fine

    Thank you
    The quick brown fox jumps over the lazy dog the quick brown
    fox jumps over the lazy dog
    The quick brown fox jumps over the lazy dog the quick brown
    fox jumps over the lazy dog

    The quick brown fox jumps over the lazy dog the quick brown
    fox jumps over the lazy dog
    The quick brown fox jumps over the lazy dog the quick brown
    fox jumps over the lazy dog
    The quick brown fox jumps over the lazy dog the quick brown
    fox jumps over the lazy dog

    This line is exactly sixty characters long; how interesting!


    Goodbye

    Expected Output -

    Hello
    How are you
    The quick brown fox jumps over the lazy dog the quick brown
    fox jumps over the lazy dog
    I am fine

    Thank you
    The quick brown fox jumps over the lazy dog the quick brown
    fox jumps over the lazy dog
    The quick brown fox jumps over the lazy dog the quick brown
    fox jumps over the lazy dog

    The quick brown fox jumps over the lazy dog the quick brown
    fox jumps over the lazy dog
    The quick brown fox jumps over the lazy dog the quick brown
    fox jumps over the lazy dog
    The quick brown fox jumps over the lazy dog the quick brown
    fox jumps over the lazy dog

    This line is exactly sixty characters long; how interesting!

    Goodbye

    Case 5:
    My Output - Successful!

    It was the best of times, it was the worst of times.

    Expected Output -

    It was the best of times, it was the worst of times.
    Last edited by MrKrow; 06-30-2013 at 10:41 AM.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,996
    Blog Entries
    7
    Rep Power
    19

    Default Re: Help on File Processing Problem (WordWrap)

    This forum won't show them by default, but does your output contain lines with leading spaces?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    MrKrow is offline Member
    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0

    Default Re: Help on File Processing Problem (WordWrap)

    I was able to solve it by modifying this portion of my code, after starting for an hour I realised my innermost if statement prints the 61st character, because the code is already on the 60th before entering the if statement. So by adding a println before I was able to come up with a correct method.
    Correct Code:
    Java Code:
    public static void wordWrap(Scanner input) {
        while(input.hasNextLine()) {
            
            String line = input.nextLine();
            int length = line.length();
            if(length >= 60) {
                for(int i = 0; i < length; i++ ) {
                    if((i % 60) == 0 && i != 0) {
                        System.out.println();
                        System.out.print(line.charAt(i));
                    } else {
                        System.out.print(line.charAt(i));
                    }
                }
                System.out.println();
            } else {
                System.out.println(line);
            }
        }
    }

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,996
    Blog Entries
    7
    Rep Power
    19

    Default Re: Help on File Processing Problem (WordWrap)

    Good to see that you solved it yourself; b.t.w. did you have a look at the String.substring( ... ) method?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    MrKrow is offline Member
    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0

    Default Re: Help on File Processing Problem (WordWrap)

    Ah yes, my original method used the .substring, I was having issues with keeping up with where the next line should start thought because its a single long string so i switched to characters.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,996
    Blog Entries
    7
    Rep Power
    19

    Default Re: Help on File Processing Problem (WordWrap)

    If a String has at least 60 characters then line.substring(0, 60) is a String containing the first 60 characters and line.sbstring(60) is a String containing the rest of the characters ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Execute Processing file from within Java
    By Smila in forum Advanced Java
    Replies: 4
    Last Post: 10-04-2012, 03:51 PM
  2. still problem with image processing
    By syarizma in forum Advanced Java
    Replies: 2
    Last Post: 01-26-2012, 06:47 PM
  3. Delimited file processing
    By lottalava in forum Advanced Java
    Replies: 7
    Last Post: 06-17-2010, 11:19 PM
  4. image processing memory problem
    By mr_empty in forum Java 2D
    Replies: 7
    Last Post: 12-16-2009, 11:03 AM
  5. File Import/Buffer processing issues?
    By javanewbee in forum New To Java
    Replies: 0
    Last Post: 11-24-2009, 11:57 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
  •