How to find the first line in a text file with a specific length?

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:

    // Write a program that finds the first word in Alice In Wonderland
// that is longer than a given number of characters.

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.

+4
6

:

BufferedReader br = new BufferedReader(new FileReader("aliceInWonderland.txt"));
String line;
while ((line = br.readLine()) != null) {
    String[] words = line.split(" ");
    for (String word : words) {
        if (word.length() >= 9) {
            return word;
        }
    }
}
+3

. :

  • FileReader/Scanner / BufferdReader .
  • trim() , .
  • - \\s+.
  • , String split, . > , String List/array. , .
  • .
+1

; for while.

- , , . , , .

, :

  • , .
  • 1

, , , , , .

. while - , .

 while(word.length < TARGET_LENGTH) {
     word = ... // get next word
 }

, , Scanner , , , . Scanner, , , . , , , .

0

Scanner. (. API)

Here you have an example of using Scanner, but there are other options, as other answers show (dividing lines into words). I don't like to generate so many String objects, but this is a question or opinion)

public static void main(String[] args) throws java.io.FileNotFoundException {

    Scanner in = new Scanner(new FileReader("aliceInWonderland.txt"));
    in.useDelimiter("^\\w"); //any character which is not a letter or number or _
    while (in.hasNext()) {
        String s=in.next();
        if (s.length>=9) {
            System.out.println("The first long word is: " +s);
            break;
        }
    }
}
0
source

I would go for it BufferedReader, read the file line by line, split the lines into spaces and return the first token that matches this condition.

public static void main(String[] args) throws FileNotFoundException, IOException {
    File file = new File("aliceInWonderland.txt");
    int threshold = 9;

    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
    String line;

    while ((line = br.readLine()) != null) {
        String[] tokens = line.split("\\s+");
        for (String token : tokens) {
            if (token.length() > threshold) {
                System.out.println("The first long word is: " + token);
                return;
            }
        }
    }
    br.close();
}
0
source

You can use the BufferedReader class to improve your performance. Like that:

public static String getLongest() throws IOException{
    BufferedReader reader = new BufferedReader(new FileReader("aliceInWonderland.txt"));
    final int THRESHOLD = 9;
    String line = "";
    while((line = reader.readLine()) != null) {
        String[] arr = line.split("\\s+");
        for(String word: arr) {
            System.out.println(word);
            if(word.length() > THRESHOLD) {
                return word;
            }
        }
    }
    return null;
}
0
source

Source: https://habr.com/ru/post/1527915/


All Articles