Linkedlist | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Linkedlist

Anyone could tell me what I am making wrong in this code please ? 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 here is my try? https://code.sololearn.com/cA163A23A13a

22nd May 2021, 6:19 PM
JRAMAHES
JRAMAHES - avatar
9 Answers
+ 8
//Please Subscribe to My Youtube Channel //Channel Name: Fazal Tuts4U 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); } for(String wordShow: words){ if(wordShow.length() <= 4){ }else{ System.out.println(wordShow); } } } }
5th Sep 2021, 6:52 AM
Fazal Haroon
Fazal Haroon - avatar
+ 7
import java.util.Scanner; import java.util.Iterator; import java.util.LinkedList; public class Main { public static void main(String[ ] args) { Scanner scanner = new Scanner(System.in); LinkedList<Integer> nums = new LinkedList<Integer>(); while(nums.size()<5){ int num = scanner.nextInt(); nums.add(num); } int sum = 0; //your code goes here for(int s: nums){ sum+= s; } System.out.println(sum); } }
6th Nov 2022, 10:38 PM
Богдан Біжовець
Богдан Біжовець - avatar
+ 1
//your code goes here for(String w: words){ System.out.println(w.length()); } Here you are printing the length of each word. But you need to print w if w.length() > 4 So add a if statement inside the loop.
22nd May 2021, 6:39 PM
Denise Roßberg
Denise Roßberg - avatar
0
thank you very much guys
22nd May 2021, 6:57 PM
JRAMAHES
JRAMAHES - avatar
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<String> words = new LinkedList<String>(); while(words.size()<5){ String word = scanner.nextLine(); //add the word to LinkedList words.add(word); } for(String wordShow: words){ if(wordShow.length() <= 4 ){ } else{ System.out.println(wordShow); } } } }
13th Jul 2022, 8:17 AM
Nesar Ahmed
Nesar Ahmed - avatar
0
import java.util.LinkedList; import java.util.Scanner; public class LinkedListProblem { public static void main(String[ ] args) { Scanner scanner = new Scanner(System.in); LinkedList<String> words = new LinkedList<String>(); System.out.println("Type words and press Enter after each word"); while(words.size()<5){ String word = scanner.nextLine(); words.add(word); } for(String S: words) { if (S.length()>4) { System.out.println(S); } } scanner.close(); } }
2nd Aug 2022, 1:30 AM
Ariaie M
Ariaie M - avatar
0
//This work correctly import java.util.LinkedList; import java.util.Scanner; class Excercise { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); LinkedList<Integer> nums = new LinkedList<Integer>(); while (nums.size() < 5) { int num = scanner.nextInt(); nums.add(num); } int sum = 0; //your code goes here for (int s : nums) { sum += s; } System.out.println(sum); } }
22nd Sep 2022, 12:43 PM
Pathum Kaleesha
Pathum Kaleesha - avatar
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<String> words = new LinkedList(); while(words.size()<5){ String word = scanner.nextLine(); words.add(word); } for(String wordShow: words){ if(wordShow.length() <= 4){ }else{ System.out.println(wordShow); } } } }
15th Nov 2022, 7:38 AM
Pooja Patel
Pooja Patel - avatar
- 1
You're printing word length instead print word if it's length is >=5 as for(String w: words) if(w.length()>=5) System.out.println(w);
22nd May 2021, 6:43 PM
Jayakrishna 🇮🇳