- 1

Can you help me?

The program you are given declares LinkedList "words". Write a program to take words as input and add them to LinkedList untill its size isn't equal to 5, then output only those words whose length is more than 4 characters. Sample Input Java practice is makes perfect Sample Output practice makes perfect This is what i manage to do now: import java.util.LinkedList; import java.util.Scanner; public class Main { public static void main(String[ ] args) { Scanner scanner = new Scanner(System.in); LinkedList<String> words = new LinkedList<String>(); while(words.size()<5){ String word = scanner.nextLine(); words.add(word); } } } I tried to use the word.length(), but it gave me that it can;t find the symbol

5th Jul 2021, 1:42 PM
ShadowRise
9 Réponses
+ 2
Why not try it after gone to home..? I think that all code is given in question itself. If you not understand anything then you can ask .. or about any silly try or mistake,still post it undoubtedly.. I meant only saying 'without try you can't understand it' or "giving code will not help you.. ShadowRise it's up to you.. Anyways : you can code like these 2 ways: In while: while(words.size()<5){ String word = scanner.nextLine(); words.add(word); if(word.length()>=5) System.out.println(word); } Or in separate loop like: for(String s: words) if(s.length()>4) System.out.println(s); both works same.
5th Jul 2021, 2:14 PM
Jayakrishna 🇮🇳
+ 1
Where you using word.length() ? Upto now It's correct. But it is incomplete. just Add code for printing words which have length more than 4. hope it helps..
5th Jul 2021, 1:54 PM
Jayakrishna 🇮🇳
+ 1
Thank you, my problem was that i was printing out words instead of word, that;s my fault, sorry
5th Jul 2021, 2:17 PM
ShadowRise
+ 1
@ShadowRice Oh. you found it. Task completed .. You're welcome..
5th Jul 2021, 6:19 PM
Jayakrishna 🇮🇳
0
i don't know where to put it, in the while? i tried outside, but it won;t find my word.
5th Jul 2021, 1:56 PM
ShadowRise
0
You can put in while or also outside in other loop. Can you post that your try..?
5th Jul 2021, 1:57 PM
Jayakrishna 🇮🇳
0
I'm not home, can you solve it for me please?
5th Jul 2021, 2:06 PM
ShadowRise
0
you are declare word variable inside while so outside word doesn't exist
5th Jul 2021, 2:21 PM
zemiak
0
import java.util.LinkedList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); LinkedList<Integer> numbers = new LinkedList<>(); for (int i = 0; i < 5; i++) { int input = scanner.nextInt(); numbers.add(input); } int sum = 0; for (int number : numbers) { sum += number; } System.out.println(sum); } }
20th Oct 2023, 2:24 AM
Juan David Suarez Holguin
Juan David Suarez Holguin - avatar