Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Subsequence words

Suppose this is my .txt file ABCDBCD and I want to use .substring to get this:
ABC BCD CDB DBC BCD

How can I achieve this? I also need to stop the program if a line is shorter than 3 characters.

static void lesFil(String fil, subsekvens allHashMapSubsequences) throws FileNotFoundException{
    Scanner scanner = new Scanner(new File("File1.txt"));
    String currentLine, subString;

    
    

        while(scanner.hasNextLine()){
            currentLine = scanner.nextLine();
            currentLine = currentLine.trim();

            for (int i = 0; i +  subSize <= currentLine.length(); i++){
    
                subString = currentLinje.substring(i, i + subSize);
                subSekStr.putIfAbsent(subString, new subsequence(subString));
            }
        }
    
   

    scanner.close();

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

With a minor changes of your code:

public static void main(String[] args) throws FileNotFoundException {
    Scanner scanner = new Scanner(new File("C:\\Users\\Public\\File1.txt"));
    String currentLine, subString;
    int subSize = 3;

    while (scanner.hasNextLine()) {
        currentLine = scanner.nextLine();
        currentLine = currentLine.trim();

        if (currentLine.length() < subSize) {
            break;
        }

        for (int i = 0; i + subSize <= currentLine.length(); i++) {

            subString = currentLine.substring(i, i + subSize);
            System.out.print(subString + " ");
        }

        System.out.print("\n");
    }

    scanner.close();
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading