Skip to content

Programming by Design

If you're not prepared to be wrong, you'll never come up with anything original. – Sir Ken Robinson

  • About
  • Java-PbD
  • C-PbD
  • ASM-PbD
  • Algorithms
  • Other

Project 6 Hashing Stuff

Posted on November 17, 2021April 6, 2024 By William Jojo
Code

To get you started on Project 6, here is the basic code to read the checkme.txt file. It reads words, converts to upper case, and strips extraneous punctuation.

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;

public class Main {

    public static String removeNonLetDig(String s) {
        int b = 0, e = s.length()-1;

        // Trim from the beginning
        while (b <= e && !Character.isLetterOrDigit(s.charAt(b)))
            b++;

        // Trim from the end
        while (e >= b && !Character.isLetterOrDigit(s.charAt(e)))
            e--;

        if (b <= e)
            return s.substring(b,e + 1);
        else
            return null;
    }

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

        Scanner inFile = new Scanner(new FileReader("checkme.txt"));

        while (inFile.hasNext()) {
            String word = inFile.next().toUpperCase();
            String stripped = removeNonLetDig(word);
            System.out.printf("Original %-15s Modified %-15s%n", word, stripped);
        }
        inFile.close();
    }
}

The output shows that null is possible. Remember that this indicates that no letters (or digits) remained after processing. This is not a misspelled word; it's not a word at all.

Original -*-             Modified null           
Original OUTLINE         Modified OUTLINE        
Original -*-             Modified null           
Original THIS            Modified THIS           
Original DIRECTORY       Modified DIRECTORY      
Original CONTAINS        Modified CONTAINS       
Original BISON           Modified BISON          
Original SKELETONS:      Modified SKELETONS
...

Post navigation

❮ Previous Post: An Introduction to Regular Expressions
Next Post: Installing JetBrains IntelliJ and Java JDK FX ❯

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Copyright © 2018 – 2025 Programming by Design.