+ 1

What's is wrong in my code?

I'm trying solve task in java course at Linked list. This is that code: 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); } //your code goes here for (int i=0;i<=words.size();i++){ word = words.get(i); if (word.lenght() > 4){ System.out.println(word);} } } } Have You some idea? On margin I say that code from "solution" from Sololearn either doesn't work (at least for me). You can check.

15th Nov 2022, 11:17 AM
Janusz
Janusz - avatar
15 Answers
+ 2
You declared word in while loop, will exist in while loop block only. Not after loop completed so in for loop word is undeclared. Also I<=words.size () will raise error if you read list 6th time. Must be i<words.size() only..
15th Nov 2022, 11:23 AM
Jayakrishna 🇮🇳
+ 1
Jayakrishna🇮🇳 Ok, thanks. How I can change declaration so that it seen outside the while block
15th Nov 2022, 11:28 AM
Janusz
Janusz - avatar
+ 1
Jayakrishna🇮🇳 That code contains "solution" from Sololearn: import java.util.LinkedList; import java.util.Scanner; public class Main { public static void main(String[ ] args) { String ileznakow; 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); } //your code goes here for (String w : words){ //ileznakow = w; if (w.lenght() > 4){ System.out.println(w); } } } } it either doesn't work
15th Nov 2022, 11:42 AM
Janusz
Janusz - avatar
+ 1
Yes, I tried many times with different ideas. Variable "ileznakow" I commented.
15th Nov 2022, 4:08 PM
Janusz
Janusz - avatar
+ 1
Jayakrishna🇮🇳 Thank you 🙂 big. Tell me if I can give you something points for your helping. I copied last code from post from you and program started. Then I looked one more time and realized that in my text I used "lenght" instead "length" ("ht" instead "th"). In our country (Poland) we say "czeski błąd " (so-called"Czech error ")🙂
15th Nov 2022, 4:20 PM
Janusz
Janusz - avatar
+ 1
Oh. You have code typo error. Check spelling of length() in your code. You are using length() instead. Error. Try in playground, for to see what error if.. edit: Janusz oh. already found it. 👍 I was just looked into code. Could have checked in playground. further mention problem details clearly for quick accurate answers.. "does not work" means I thought logic error. typo mistakes hard to find by only seeing sometimes..
15th Nov 2022, 4:23 PM
Jayakrishna 🇮🇳
+ 1
Jayakrishna🇮🇳 Ok, you are right... ...but I try explain reason: 1) I'm from Poland and my English level is about B1+ 2) when I was looking for a bug, at that point, I didn't know it was just a typing error either 🙂😀 What about did you thought used "in playground code". Is in Sololearn that? When? How I can use this?
15th Nov 2022, 4:40 PM
Janusz
Janusz - avatar
+ 1
Yes. In code coach, you can't see error details. Just case see test passed or not. So if you can't find there errors then check in SL playground by saving code. Code section => new code ( + ) => select Java.. Ok. That's no problem. Am also not native English speaker. Iam trying to say : as much as possible, put clear details.. Get quick, accurate answers.. Some times, because of network issue coach does not work even with correct code. Need to just try again after some time. Rare cases. So i thought, may be network issue. Ok. All fine. Continue happy learning.. You're welcome...
15th Nov 2022, 4:52 PM
Jayakrishna 🇮🇳
0
You can declare again in for loop also or declare it before while loop.. But note : Generally strings assignments in loop are not prefered. or directly use list word length without string declaration as words.get(i).length() String word ; for( int i = 0; i < words.size() ; i++ ) { word = words.get(i) ; if ( word.length() > 4 ) System.out.println( word ) ; }
15th Nov 2022, 11:41 AM
Jayakrishna 🇮🇳
0
Janusz Yes. This is another version of for loop called enhanced or for-each loop. for ( String word : words) if ( word.length() > 4 ) System.out.println(word); In this, not use index, instead directly use element . And dont use size() method. I don't see String ileznakow; declaration in solution.. It it not used anywhere. You can comment or remove as you did in loop...
15th Nov 2022, 11:52 AM
Jayakrishna 🇮🇳
0
Never mind. I found it. I don't realized that it has name "code playground". I know it earlier. Thanks
15th Nov 2022, 4:45 PM
Janusz
Janusz - avatar
0
Hello guys help me with a source code for building a chat bot with a python
17th Nov 2022, 5:16 AM
Tkidd Pro
Tkidd Pro - avatar
0
Ja pisze po polsku
17th Nov 2022, 7:23 AM
Staś
Staś - avatar
0
Nie umiem tak dobrze angielskiego
17th Nov 2022, 7:23 AM
Staś
Staś - avatar
0
Praktycznie wogule
17th Nov 2022, 7:25 AM
Staś
Staś - avatar