+ 4

Can you please give me suggestions?

Hello, can you please tell me some improvements and suggestions for my Code? Thanks https://code.sololearn.com/c5m86uLyLc2V/?ref=app

7th Jul 2017, 5:50 PM
reqqu
reqqu - avatar
4 Respuestas
+ 1
Sorry, just saw this. Here you go. It's a start. Basically, just storing the string array into a list for more functionality, and then I create a new list that we'll place all the "valid" words into. Each time we add an element to the new list, I increase wordcount so we get a proper count of the words. Hope this helps. https://code.sololearn.com/cFm52PXrGER5/#java public static void main(String[] args) { Scanner sc = new Scanner(System.in); String input = sc.nextLine(); String[] words = input.split(" "); List<String> wordsList = new ArrayList<String>(Arrays.asList(words)); List<String> wordsFinal = new ArrayList<String>(); int wordCount = 0; for (String s: wordsList) { if(s != null && !s.isEmpty()) { wordsFinal.add(s); wordCount++; } } if (input.length() != 0){ System.out.println("Your original sentence was:\n" + input + "\n"); System.out.println("Your sentence has got " + wordCount + (" word(s).")); }else{ System.out.println("Please enter a sentence to check...\nGood luck!"); } }
7th Jul 2017, 6:44 PM
AgentSmith
+ 4
Thanks mate! How can I do this? Any examples?
7th Jul 2017, 5:55 PM
reqqu
reqqu - avatar
+ 2
Check for double spaces. ;) If not, it'll count each additional space as a word. There may be an easier way, but you can just have it find the consecutive spaces and replace it with one space instead.
7th Jul 2017, 5:56 PM
AgentSmith
+ 2
You're welcome bro.
7th Jul 2017, 5:58 PM
AgentSmith