0

Write a method called countWords that accepts an ArrayList of String as argument and prints out the number of words (i.e. Strings) that start with ―A‖ or ―a‖ and prints all words longer than 5 characters on one line.

My solution is like

int count=0;
    String[] st=null;

    Scanner input=new Scanner(System.in);
    ArrayList<String> array = new ArrayList<String>();
    System.out.println("please input something");

while(input.hasNext()) {
        String st1=input.next();
        array.add(st1);

    }


    for(int i=0; i<array.size();i++) {
        if(array.get(i).startsWith("a")||array.get(i).startsWith("A")) {
            count++;
        }
    }
        for(int j=0; j<array.size(); j++) {
            if(array.get(j).length()>5) 
                st[j]=array.get(j);

        }

        System.out.println(count);
        System.out.println(st);
}

but there will be no end for typing in Strings

BigDirk
  • 11
  • 2

2 Answers2

0

As the last line of your question said

but there will be no end for typing in Strings

Well That is because you did not provided any way to end the while loop.

while(input.hasNext())

Will run forever and ever waiting for next user input. You have to break the while loop once the inputting is done.

AFTERWARDS

As the question said "prints out the number of words that start with A or a and prints all words longer than 5 characters on one line."

For this you can loop through the ArrayList and check for

if(array.get(i).startsWith("A") || array.get(i).startsWith("a")) count++;

if(array.get(i).length()>5) System.out.print(array.get(i)+" ");

and print the number of A or a Occurrence after the loop

System.out.println("\n Number of word with A or a:"+count);

Here is a working implementation of your code

public static void main(String[] args) {
    int count=0;
    String[] st=null;
    Scanner input=new Scanner(System.in);
    ArrayList<String> array = new ArrayList<String>();
    System.out.println("please input something");
    //System.out.println(input.hasNext());
while(input.hasNext()) {
        String st1=input.next();
        //System.out.println((int) st1.charAt(0));
        if(st1.equals("exit")) break;
        array.add(st1);

    }


    for(int i=0; i<array.size();i++) {
        if(array.get(i).startsWith("A") || array.get(i).startsWith("a")){
            count++;
        }
        if(array.get(i).length()>5) {
            System.out.print(array.get(i)+" ");
        }

    }
    System.out.println("\nNumber of word with A or a:"+count);
}

to end the loop you have to type exit.

Sundeep
  • 452
  • 2
  • 12
  • Thank you so so so so much, I have tried on eclipse, that is really helpful for a rookie like me! – BigDirk Jun 17 '18 at 11:49
  • you should support everyone else too if your problem is solved and you are satisfied you must mark question as solved and vote the answer up :) it helps – Sundeep Jun 17 '18 at 15:45
0

Here is a solution to your problem..

import java.util.Arrays;
import java.util.List;

public class Test {

    public static void main(String... args){
        // Sample String sentence 
        String sentence = "This is the sentence with 5 words starting with 
             all like allwords alltogether also Allnotout allother and allofus.";

        // Splitting above sentence to get each word separately and storing them into List
        List<String> strings = Arrays.asList(sentence.split("\\s+"));
         // calling a method named countWord() as per your assignment question.
        Test.countWords(strings);
    }


    // implementing that method
    static void countWords(List<String> input){
        long count = input.stream().filter(word -> word.startsWith("all") || word.startsWith("All")).count();
        System.out.print("Total words starting with all/All are : "+ count +"\t");
        input.stream().filter(word -> word.length() > 5).forEach( word -> System.out.print(word + "\t"));

    }

}
miiiii
  • 1,580
  • 1
  • 16
  • 29
  • Thank you for reply – BigDirk Jun 17 '18 at 11:50
  • Most welcome..!! I think you already found your solution.. but I would recommend If you are on Java 8, you must try this.. you will learn a lot. !! So many complex things, in so simple way..!! :) – miiiii Jun 17 '18 at 11:52
  • You're supposed to print all words words longer than 5 and the number of words that starts with "all" or "All", your code doesn't do that at all. – Joakim Danielson Jun 17 '18 at 12:27
  • @JoakimDanielson thanks for raising hand.. see edit, did I still miss anything?? – miiiii Jun 17 '18 at 12:44