Logo

Generate new code

JDroid AI offers a feature togenerate new code, which turns plain text input or prompts into**** working code. It currently supports over 88 languages, the same as our IDE. Note:**** Thegenerate new code feature**** usesone AI credit. Our JDroid API also supports the feature to generate new code. For more information, refer to the JDroid API server documentation. This documentation provides a detailed explanation of how to use the generate new code feature. Please refer to this documentation to learn the prerequisites for using JDroid AI.

Where can I find the ‘generate new code’ feature?

Here are a few ways you can access JDroid AI’s generate new code feature.

  • When you click ‘Let’s code ’ on the homepage or search for your preferred language in the header’s search bar, you’ll be directed to create a new JDrive (projects).

You can start coding with a sample code generated from JDroid by entering it into the prompt box. Screenshot 2024-04-23 at 13.52.07.png

  • You’ll find a floating JDroid symbol in the IDE editor to your right. When you click the JDroid symbol, the chat opens, and you can talk to JDroid and get the code you need using a simple prompt.

Using JDroid

Important note:

JDroid will generate prompts in the same language as your chosen IDE. For example, if you are in the JAVA compiler IDE, you cannot ask JDroid to generate code in C++ or any other language except JAVA. If you ask the same in the prompt, you will still get code in the same language as the IDE. If you wish to generate code in a different language, go to the IDE where you want the generated code. For example, if you want a code snippet in C++, you must go to the C++ compiler IDE.

How ‘generate new code ’ works?

The working of the generate new code feature is straightforward. You must specify the details you need in your code in the prompt you’re giving to JDroid and hit enter. We’ve used the Java Compiler IDE and gave a prompt to generate astring reversal code. Using JDroid in Java IDE

Here’s the output given by JDroid:

public class Main {
    public static void main(String[] args) {
        String str = "Hello World";
        String reversed = reverseString(str);
        System.out.println("Reversed String is: " + reversed);
    }

    public static String reverseString(String str) {
        String reversed = "";
        for(int i = str.length() - 1; i >= 0; i--) {
            reversed = reversed + str.charAt(i);
        }
        return reversed;
    }
}

Rules for writing prompts

  • The capabilities of JDroid AI are extensive. You can ask JDroid to add several snippets in a single prompt to the code you want.

For example, you can ask JDroid to generate a code that creates a Fibonacci series, create an array on the series, perform sorting on it, then search for the position of a specific number, and so on.

  • If you wish to get input from the user, it’s better to specify it beforehand in the prompt to get precisely what you need. The more specific prompt you can provide, the better output you can get.

For example, in the above string reversal code provided by JDroid, you can specify you want the input string from the user. In this case, you can then use the STDIN box to provide input or turn on the ‘interactive mode.’ To learn more about interactive mode, refer to this documentation.

  • Ensure your class is “public ” so your code can be executed in the IDE.

If you still encounter errors while executing, you can select the debugging feature of JDroid. To learn more about JDroid’s debug code feature, read this documentation.

Example prompts to try

Here are some sample prompts to try. You can choose any language you wish for these prompts. As an example, we will show output in the JAVA IDE.

Implement a calculator with basic arithmetic operations.

Here’s the output provided by JDroid:

import java.util.Scanner;
public class Calculator {
   public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
       System.out.println("Enter two numbers: ");
       double num1 = scanner.nextDouble();
       double num2 = scanner.nextDouble();
       System.out.println("Enter an operator (+, -, *, /): ");
       char operator = scanner.next().charAt(0);
       scanner.close();
       double output;
       switch (operator) {
           case '+':
               output = num1 + num2;
               break;
           case '-':
               output = num1 - num2;
               break;
           case '*':
               output = num1 * num2;
               break;
           case '/':
               if (num2 != 0) {
                   output = num1 / num2;
               } else {
                   System.out.println("Cannot divide by zero");
                   return;
               }
               break;
           // operator doesn't match any case constant (+, -, *, /)
           default:
               System.out.println("Error: Invalid operator");
               return;
       }
       System.out.println(num1 + " " + operator + " " + num2 + " = " + output);
   }
}

Convert temperature to Fahrenheit or Celsius

Here’s the output provided by JDroid:

import java.util.Scanner;
public class TemperatureConverter {
   public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
       System.out.println("Enter temperature:");
       double temperature = scanner.nextDouble();
       System.out.println("Enter unit (F for Fahrenheit, C for Celsius):");
       char unit = scanner.next().charAt(0);
       double convertedTemperature;
       if (unit == 'F' || unit == 'f') {
           convertedTemperature = (temperature - 32) * 5 / 9;
           System.out.println("Temperature in Celsius: " + convertedTemperature);
       } else if (unit == 'C' || unit == 'c') {
           convertedTemperature = (temperature * 9 / 5) + 32;
           System.out.println("Temperature in Fahrenheit: " + convertedTemperature);
       } else {
           System.out.println("Invalid unit. Please enter F for Fahrenheit or C for Celsius.");
       }
       scanner.close();
   }
}

Things to consider while writing prompts

Here are a few cases where JDroid can produce the wrong code or produce no output.

  • You might get a wrong answer if you request that JDroid generate code in a different language from your selected IDE.
  • JDroid AI may not give the right answer if you’re using complex or new languages like Befunge,Cow,Brainfuck,Intercal, etc.
  • JDroid is still learning! It can generate an error in the code if you ask it to perform something very complex.
  • JDroid AI is only created for coding-related tasks. The below categories of prompt will not work correctly:
    • Meaning of words like ‘flabbergasted’
    • Mention that you need input from the user
    • Ask for locations or prompts like ‘Give me a chocolate.’

Found a bug?

If you have a suggestion for a new feature (or a request), spotted a bug, or need more assistance, please fill out this form or email hello@jdoodle.com. Our support team will contact you as soon as possible. You can also ask your queries on our discord community.

Previous Topic ← FlockServe