-3

I have an arraylist that contains 3 Strings.

ArrayList<String> sentences = new ArrayList<String>();
                    sentences.add("it would be for his happiness and having some feelings himself");
                    sentences.add("a plan to promote the happiness of all she and Mr" );
                    sentences.add("a most unreasonable degree of happiness She must wait a moment");

                       for(String f : sentences){
                        System.out.println(f);
                        }

Has the output

it would be for his happiness and having some feelings himself 
a plan to promote the happiness of all she and Mr 
a most unreasonable degree of happiness She must wait a moment 

The MIDDLE word in each sentence is "happiness"

I need the output on the screen to be in the form

          it would be for his happiness and having some feelings himself 
        a plan to promote the happiness of all she and Mr 
a most unreasonable degree of happiness She must wait a moment 

Where the word happiness is positioned centrally neatly underneath each other with the rest of the sentence around it. Any help is welcome

Steve
  • 77
  • 1
  • 11
  • 2
    Any first attempt is welcome too. Without posting your attempt, we have no idea what you're having trouble with, so we won't know how to advise you. For another, if you don't at least try to solve what you can first, you're cheating yourself out a valuable part of your learning experience. Please don't do this. – Hovercraft Full Of Eels Jul 12 '14 at 13:19
  • 5
    As you can see based on your text, what you need to do is find the index of "H" in the word "happiness", find the maximum of these numbers, then add MAX-(currentRowHIndex) spaces before the string. – EpicPandaForce Jul 12 '14 at 13:21

1 Answers1

1

As you can see based on your text, what you need to do is find the index of "H" in the word "happiness", find the maximum of these numbers, then add MAX-(currentRowHIndex) spaces before the string.

And yup, after waiting a bit I felt like doing it, and the idea as mentioned by the comment, worked:

public class Happiness
{    
    public static void main(String[] args)
    {
        List<String> sentences = new ArrayList<String>(); //USE THE INTERFACE
        sentences.add("it would be for his happiness and having some feelings himself");
        sentences.add("a plan to promote the happiness of all she and Mr");
        sentences.add("a most unreasonable degree of happiness She must wait a moment");

        int maxHIndex = -1;
        for(String sentence : sentences) 
        {
            int hIndex = sentence.indexOf("happiness");
            if(hIndex > maxHIndex)
            {
                maxHIndex = hIndex;
            }
        }
        for(String sentence : sentences)
        {
            StringBuffer sb = new StringBuffer("");
            for(int i = 0; i < maxHIndex - sentence.indexOf("happiness") ; i++)
            {
                sb.append(" ");
            }
            System.out.println(sb.toString() + sentence);
        }
    }
}

Output:

          it would be for his happiness and having some feelings himself
        a plan to promote the happiness of all she and Mr
a most unreasonable degree of happiness She must wait a moment

If there's a possibility that "happiness" is not inside the sentence, then just do an if(!sentence.contains("happiness")) or something of that sort.

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428