When AI Meets Java: Hilarious Misadventures with ChatGPT and GitHub Copilot

George Adams
3 min readApr 18, 2023

--

Introduction

Java developers, prepare for a wild ride! AI-powered tools like ChatGPT and GitHub Copilot are here to spice up your coding experience. While they can be incredibly helpful, they sometimes lead us down a rabbit hole of hilariously wrong code suggestions. In this blog post, we’ll explore the lighter side of AI in Java development and share some laugh-out-loud examples of when things didn’t go quite as planned.

When AI Suggestions Go Awry

A Recursive Nightmare

Imagine working on a simple Java method to calculate the factorial of a number, and GitHub Copilot suggests the following gem:

public static int factorial(int n) {
if (n == 0) {
return factorial(1);
} else {
return n * factorial(n - 1);
}
}

As you might notice, there’s a minor issue (ahem) with the base case — instead of returning 1, it calls factorial(1), resulting in infinite recursion. Good luck getting out of that one!

The “Helpful” ChatGPT

When you ask ChatGPT for help on how to parse a CSV file in Java, it gets a bit too creative and suggests:

public static void parseCSV(String csvContent) {
String[] lines = csvContent.split("\n");
for (String line : lines) {
String[] fields = line.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
System.out.println("Banana: " + fields[0] + ", Apple: " + fields[1]);
}
}

While the regex magic is impressive, I’m not sure why the AI is suddenly obsessed with fruits. But hey, maybe it’s trying to remind us to eat healthily?

The Not-So-Secure Password Generator

GitHub Copilot, attempting to flex its security expertise, proposes this excellent password generator:

public static String generatePassword() {
return "password123";
}

Well, that’s certainly a secure password, right? I’m sure no hacker would ever think of trying that one first.

The Java Time Machine

ChatGPT, in an ambitious attempt to help you manipulate dates in Java, offers this peculiar code snippet:

public static LocalDate timeTravel(LocalDate date, int years) {
return date.plusYears(-years);
}

While the intention was to add the specified number of years to the given date, the AI seems to have taken “time travel” a bit too literally by subtracting the years instead. Great Scott!

The Mysterious Enum

GitHub Copilot, eager to help you with a custom enumeration, presents this enigmatic creation:

public enum MysteriousEnum {
RED("Red"),
GREEN("Green"),
BLUE("Blue"),
BLARGH("Blargh");

private String value;

MysteriousEnum(String value) {
this.value = value;
}

public String getValue() {
return value;
}
}

While the first three enum values seem reasonable, “BLARGH” makes a surprise appearance, leaving us to wonder what exactly the AI was thinking. Perhaps it’s a new colour from a galaxy far, far away?

The Unhelpful Helper Method

When you ask ChatGPT to help you create a helper method to check if a string contains a specific character, it offers this peculiar suggestion:

public static boolean containsChar(String input, char target) {
return input.indexOf(target) == input.lastIndexOf(target);
}

While this method does check for the presence of the target character, it returns true only if the character appears exactly once in the input string. It seems the AI decided to add a twist, turning a simple task into a more confusing one. Surprise!

Embrace the Fun

When using ChatGPT and GitHub Copilot, always remember that these tools are not perfect. They might provide you with brilliant code solutions one moment and hilarious missteps the next. To make the most of your AI-powered coding journey, follow these tips:

  1. Enjoy the humour: When AI-generated code goes wrong, have a good laugh and appreciate the absurdity.
  2. Stay vigilant: Always review the generated code and verify its correctness, relevance, and security.
  3. Learn and adapt: Use AI tools as a resource, not a crutch. Learn from the examples they provide and adapt them to your specific needs.

And remember, as Java developers, we’re here to have fun and learn from our experiences — even if they involve AI-induced hilarity. Happy coding!

--

--

George Adams

Java Champion, Senior Engineer @Microsoft. Chairman @Adoptium. Views are my own.