Convert pseudocode to Java
Codoritmo translates your algorithm into a Java class with its main method: it works out the types (int, double, boolean, String), turns your functions into static methods and adds what's needed to read values from the keyboard.
Opens the program in Codoritmo; choose Java in View › Translate To.
The same algorithm in pseudocode and in Java
The “Grade Average” example, with an array and a Para loop, and the Java that Codoritmo generates for it, untouched.
Pseudocode
Proceso PromedioNotas Definir notas Como Real Dimension notas[5] Definir suma, promedio, maximo Como Real Definir i Como Entero suma <- 0.0 maximo <- 0.0 // Llenar el arreglo Para i <- 1 Hasta 5 Hacer Escribir "Ingrese nota ", i, ": " Leer notas[i] suma <- suma + notas[i] // Encontrar la nota máxima Si notas[i] > maximo Entonces maximo <- notas[i] FinSi FinPara promedio <- suma / 5 Escribir "Promedio: ", promedio Escribir "Nota más alta: ", maximoFinProcesoJava generated by Codoritmo
// Este código fue generado por Codoritmo a partir del pseudocódigo.// Puede que no sea completamente correcto: revíselo antes de usarlo.import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.math.BigDecimal;import java.math.RoundingMode;class Promedionotas { static BufferedReader entrada = new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args) { int i = 0; double maximo = 0.0; double promedio = 0.0; double suma = 0.0; double[] notas = new double[5]; suma = 0; maximo = 0; for (i = 1; i <= 5; i++) { System.out.println("Ingrese nota " + i + ": "); notas[i - 1] = leerReal(); suma = suma + notas[i - 1]; if (notas[i - 1] > maximo) { maximo = notas[i - 1]; } } promedio = suma / 5; System.out.println("Promedio: " + texto(promedio)); System.out.println("Nota más alta: " + texto(maximo)); } // Lee una línea de la entrada. static String leer() { try { String linea = entrada.readLine(); return linea == null ? "" : linea; } catch (IOException e) { return ""; } } static double leerReal() { return Double.parseDouble(leer().trim()); } // Escribe un número como PSeInt: redondeado a 10 decimales, sin ceros // finales y sin notación exponencial. static String texto(double valor) { if (Double.isNaN(valor) || Double.isInfinite(valor)) return String.valueOf(valor); return new BigDecimal(valor).setScale(10, RoundingMode.HALF_UP).stripTrailingZeros().toPlainString(); } static String texto(boolean valor) { return valor ? "VERDADERO" : "FALSO"; }}This is exactly the code Codoritmo generates for this program, including the comment at the top, which is always in Spanish.
How to translate your pseudocode to Java
Write or open your algorithm
It must be free of syntax errors; the Problems panel lists any.
Choose Java
In View › Translate To, choose Java. The code tab in the side panel shows the translation.
Compare and copy
The translation updates as you type. Copy it, or download it as .java with File › Export As.
Run it
Save it under the class name, for example Promedionotas.java, and run java Promedionotas.java (Java 11 or later).
Opens the program in Codoritmo; choose Java in View › Translate To.
What changes in Java
The class is named after your algorithm: Algoritmo PromedioNotas becomes class Promedionotas.
To read input, the code adds small methods (leerEntero, leerReal…) that read a line from the keyboard and convert it to the variable's type.
Arrays are created with new and their size can be a variable, so Dimension datos[n] translates directly. As in C, indexes start at 0 and datos[i] becomes datos[i - 1] when your profile uses 1-based arrays.
Real numbers are printed as PSeInt does, rounded to 10 decimals, by a helper method called texto.
How each instruction is translated
Each row is one PSeInt instruction translated by Codoritmo inside a program with integers n and i, a real x, a text nombre and an array datos[5]. The output is generated when this page is built.
| Instruction | PSeInt | Java |
|---|---|---|
| Write (Escribir) | Escribir "Hola, ", nombre | System.out.println("Hola, " + nombre); |
| Write without a newline | Escribir Sin Saltar "Total: " | System.out.print("Total: "); |
| Read an integer (Leer) | Leer n | n = leerEntero(); |
| Read a real number | Leer x | x = leerReal(); |
| Random number | n <- Aleatorio(1, 6) | n = 1 + (int) (Math.random() * (6 - 1 + 1)); |
| If … else (Si … SiNo) | Si n MOD 2 = 0 Entonces
Escribir "par"
SiNo
Escribir "impar"
FinSi | if (n % 2 == 0) {
System.out.println("par");
} else {
System.out.println("impar");
} |
| Switch (Segun) | Segun n Hacer
1: Escribir "uno"
De Otro Modo: Escribir "otro"
FinSegun | switch (n) {
case 1:
System.out.println("uno");
break;
default:
System.out.println("otro");
} |
| While (Mientras) | Mientras n > 0 Hacer
n <- n - 1
FinMientras | while (n > 0) {
n = n - 1;
} |
| Repeat … until (Repetir) | Repetir
Leer n
Hasta Que n > 0 | do {
n = leerEntero();
} while (n <= 0); |
| For (Para) | Para i <- 1 Hasta 10 Hacer
Escribir i
FinPara | for (i = 1; i <= 10; i++) {
System.out.println(i);
} |
| Array element | datos[1] <- n | datos[0] = n; |
Codoritmo and PSeInt desktop
PSeInt desktop also exports to Java. These are the differences.
What Codoritmo adds
- You see the translation next to the pseudocode, and it updates as you type.
- It also exports to C#, PHP, Pascal, Visual Basic, QBasic, MatLab and HTML, like PSeInt.
- The translation follows the chosen profile, such as 0-based or 1-based arrays.
What PSeInt desktop does that Codoritmo doesn't yet
- Its exporter has many years of classroom use; Codoritmo's is newer, so review the code before you hand it in.
Codoritmo is an independent project, inspired by PSeInt and compatible with its syntax and school profiles. It is not PSeInt and is not affiliated with it. Official PSeInt website
Frequently asked questions
Which Java version do I need?
The code compiles with Java 8 or later. To run it directly with java File.java, without compiling first, you need Java 11 or later.
Why doesn't it use Scanner?
It uses BufferedReader, which reads a whole line at a time, like Leer in PSeInt. That way each answer you type matches one Leer.
Does the code follow Java style?
It's written to resemble your pseudocode, not to follow every Java convention: for example, it declares every variable at the start of main. It's a good starting point to rewrite your own way.
Examples to try
Solved programs, each with its code, its flowchart and a sample run.
Grade Average
Stores five grades in an array and finds the average and the highest.
BeginnerLinear Search
Finds a name in a list, ignoring upper and lower case.
IntermediateFactorial and Power
Functions that compute a factorial and a power.
IntermediateSelection Sort
Finds the smallest of the remaining numbers and puts it in its place.
Intermediate
By concept:Arrays examples
More Codoritmo tools
- PSeInt onlineWrite and run pseudocode with PSeInt's syntax from your browser.
- PSeInt alternativeAn honest comparison of Codoritmo and PSeInt desktop.
- Online flowchart makerYour algorithm's flowchart, generated from the code and animated as it runs.
- Trace tablesA table of every variable's value at each step, filled in while the program runs.
- Pseudocode to PythonYour algorithm translated into a Python program you can run.
- Pseudocode to CYour algorithm translated into a C (or C++) program ready to compile.
- Block-based programmingBuild your algorithm with Scratch-like blocks and see the pseudocode they write.