I am currently trying to write a program that goes through a text file to find the first word longer than 9 characters.
I thought that I first need to get the full word, so for this I have to use a for loop to iterate through each element in the file, adding it as the currentWord variable.
Secondly, if I got this word / String, I would take the second step - to compare its length with the target ("THRESHOLD") length 9. If it was longer, I found the first word of length greater than or equal to 10 and will return it is in the offer. Otherwise, the loop would continue the iteration?
Here is what I still have:
import java.util.Scanner;
import java.io.FileReader;
import java.io.FileNotFoundException;
public class LearningLoops {
public static void main(String[] args) throws java.io.FileNotFoundException
{
Scanner in = new Scanner(new FileReader("aliceInWonderland.txt"));
String longWord = "";
boolean found = false;
final int THRESHOLD = 9;
int i;
String currentWord;
for (i = 0; in.charAt(i) != " "; i++) {
currentWord = currentWord + i;
i++;
}
while (in != null) {
if (currentWord.length() > THRESHOLD) {
longWord = currentWord;
}
System.out.println("The first long word is: " + longWord);
}
}
}
Is this the right approach? I am stuck and so I will be grateful for any help.