Skip to content

Development at ProgramTom Dot Com

Place for coding, programming, development and software in general

Menu
  • Blog
  • PDF Booklets
  • Dev Utils
  • Java Spring Boot Or Web Apps
  • EnglishEnglish
    • българскибългарски
    • EnglishEnglish
    • EspañolEspañol
Menu
ChatGPT Wrote me Code Utility in Java

ChatGPT Wrote me Code Utility in Java

Posted on March 30, 2023April 8, 2023 by Toma Velev

ChatGPT Wrote me Code Utility in Java and it was surprisingly good. I just needed to tell it very specifically what I needed in multiple aspects and multiple dimensions. It requires:

  • The version of programming language. Probably it will going to be the same for – versions of frameworks, libraries, SDKs, operating systems and other items that add variations to the generated code
  • Very well described – the business logic that is required. It understood and put in code all of it.

I needed an utility to:

  • Parce all .java files in a directory
  • Scan for String literals in it and replace their content with their lower case version and replace the interval with _.

The Result is the following:

package mypackage;
import com.helger.commons.regex.RegExPattern;

import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class JavaStringTransformer {
    public static void main(String[] args) {
        // The directory containing the Java classes to analyze
        String directory = "mydir";

        // The file extension of the Java classes to analyze
        String fileExtension = ".java";

        // Loop through all files in the directory
        File folder = new File(directory);
        File[] files = folder.listFiles();
        Pattern pattern = Pattern.compile("\"(.*?)\"");

        for (File file : files) {
            // Check if the file is a Java class
            if (file.isFile() && file.getName().endsWith(fileExtension)) {
                try {
                    // Read the contents of the file into a string
                    BufferedReader reader = new BufferedReader(new FileReader(file));
                    StringBuilder builder = new StringBuilder();
                    String line;
                    while ((line = reader.readLine()) != null) {
                        builder.append(line).append("\n");
                    }
                    String content = builder.toString();
                    reader.close();

                    Matcher matcher = pattern.matcher(content);
                    while (matcher.find()) {
                        // Get the original string literal
                        String original = matcher.group(1);

                        // Convert the string literal to lowercase and replace spaces with underscores
                        String modified = original.toLowerCase().replace(' ', '_');

                        // Replace the original string literal with the modified version
                        content = content.replace("\"" + original + "\"", "\"" + modified + "\"");
                    }
                    // Replace all string literals with lowercase and underscores

//                    content = content.replaceAll("\"(.*?)\"", match -> {
//                        String original = match.group(1);
//                        String modified = original.toLowerCase().replace(' ', '_');
//                        return "\"" + modified + "\"";
//                    });

                    // Write the modified content back to the file
                    BufferedWriter writer = new BufferedWriter(new FileWriter(file));
                    writer.write(content);
                    writer.close();

                    // Print a message indicating the file was processed
                    System.out.println("Processed file: " + file.getName());

                } catch (IOException e) {
                    // Handle any file IO errors
                    System.err.println("Error processing file: " + file.getName());
                    e.printStackTrace();
                }
            }
        }
    }
}

It documented the code and put debug prints – for everything that it do. I’ll most likely try to use it again.

  • What the market wants from a Flutter Developer?
  • Design tools Figma (& others) & Flutter Development
  • Why would someone need an app and especially developed with Flutter
  • How Secure is Flutter?
  • Flutter – the current latest Evolution of Google Android User Interface Building

Categories

  • Apps (10)
  • ChatGPT (5)
  • Choosing a Framework (32)
  • Flutter (61)
  • Graphical User Interface (10)
  • Marketing (92)
  • Software Development (154)
  • Spring (1)
  • StartUp (17)
  • Uncategorized (1)

Tags

Algorithms (9) crypto (29) flutterdev (39) General (86) Java (7) QR & Bar Codes (3) Software Dev Choices (33) Spring Boot (1) standards (1) Theme (3) User Authentication & Authorization (9) User Experience (10) Utilities (19) WordPress (11)

Product categories

  • All Technologies (33)
    • Flutter (11)
    • Java (10)
    • Native Android (3)
    • PHP (6)
    • Spring (Boot) Framework (4)
    • Utils (10)
    • Vaadin 8 (1)
  • Apps (14)
    • Employees DB (1)
    • Notes (5)
    • Personal Budget (1)
    • Recipes Book (1)
    • Stuff Organizer (1)
    • To-Do (1)
  • PDF Books (3)
  • Source Code Generators (14)

Recent Posts

  • What the market wants from a Flutter Developer?
  • Design tools Figma (& others) & Flutter Development
  • Why would someone need an app and especially developed with Flutter
  • How Secure is Flutter?
  • Flutter – the current latest Evolution of Google Android User Interface Building

Post Categories

  • Apps (10)
  • ChatGPT (5)
  • Choosing a Framework (32)
  • Flutter (61)
  • Graphical User Interface (10)
  • Marketing (92)
  • Software Development (154)
  • Spring (1)
  • StartUp (17)
  • Uncategorized (1)
Manage Cookie Consent
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Manage options Manage services Manage vendors Read more about these purposes
View preferences
{title} {title} {title}