+ 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
9 Réponses
+ 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);
}
}
}
}
+ 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);
}
}
+ 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.
+ 1
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);
}
}
}
}
0
thank you very much guys
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();
}
}
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);
}
}
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);
}
}
}
}
- 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);