Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8750545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:53:08+00:00 2026-06-13T12:53:08+00:00

I wonder how can I change my code so, that the number of given

  • 0

I wonder how can I change my code so, that the number of given value words was specified in another class. And in construction class I need to make a method that prints in every given value. Here is my code:

public void wordBox()
    {
        for (int i = 0; i < words.length(); i++)
        {
        if (words.length() % wordsPerLine == 0 && words.charAt(wordsPerLine) == ' ')
            System.out.println();               
        else 
            System.out.print(words.charAt(i));
        }
        System.out.println();
    }

wordsPerLine was given in another class as a number of 3.
String is this:”the quick brown fox jumped over the lazy dog”

output should be look like:

the quick brown

fox jumped over

the lazy dog

Note: I am not allowed to use any other classes like token or buffer. Also, I cannot use replace/split. So, this is what I was given and I need to work from that.

Any help will be appreciated.
Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:53:10+00:00Added an answer on June 13, 2026 at 12:53 pm

    Rather than making words and wordsPerLine instance variables that are set in the constructor, make them parameters passed to your static method, like this:

    public static void wordBox(String words, int wordsPerLine) {
        int count = 1;
        for (int i = 0; i < words.length(); i++)
        {
        if (words.charAt(i) == ' ' && (count++) % wordsPerLine == 0)
            System.out.println();               
        else 
            System.out.print(words.charAt(i));
        }
        System.out.println();
    }
    

    This way you would be able to specify words and wordsPerLine independently of the constructor, and call wordBox from anywhere with any combination of words and the desired number of words per line.

    Here is a link to ideone with this running program.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wonder about that can I write native SQL to add or delete operations
I wonder whether someone can help me please. I'm using the code below to
In MYSQL, some code from a Wordpress plugin that I don't want to change
I wonder how can I create a shadow similar to the one found in
I wonder how can I export a Visual Studio C++ project to Qt? I
I wonder how can I search for a certain pattern in file, but only
Out of curiosity, I wonder what can people do with parsers, how they are
I'd like to start coding for NVIDIA 3D Vision and wonder where can I
wonder whether someone can help me with the following one... I have a struct
I wonder if I can execute some programs for different cmd's using .bat file.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.