Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

179
Vistas
Missing While loop logic

found this code on the internet, it's missing the while loop logic "while(i....)" for some reason and though I found other working solutions for the PigLatin* problem, I really want to understand how this one is working.

*PigLatin problem: take a sentence, take the first letter from each word and place it at the end of the same word, then suffix "ay". So "I am confused" becomes "Iay maay onfusedcay".

Here is the code:

        import java.util.*;
        public class PigLatin {
            
            public static void main(String[] args) {
                        
                Scanner input = new Scanner(System.in);
                    
                System.out.println("Please enter an English sentence: ");
                String sentence = input.nextLine();
                
                System.out.println("Original sentence: "+sentence);
                System.out.println("PigLatin conversion: "+convert(sentence));
            }
            
        private static String convert (String sentence) {
                
                String []words = sentence.split(" ");
                int i = 0;
                String pigLatin = "";
                while(i ){ //MISSING CODE
                    pigLatin+=words[i].substring(1,words[i].length())+words[i].charAt(0)+"ay"+" ";
                    i++;
                }
                return pigLatin;
            }
        }

Thank you.

PS: I basically found the "convert" method on the internet and wrote the rest of the code, tried a few things but could not get the while loop to work.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

The loop appears to be iterating the words array. So it should just be something like

while(i < words.length) {
    pigLatin += words[i].substring(1, words[i].length())
            + words[i].charAt(0) + "ay ";
    i++;
}
over 4 years ago · Santiago Trujillo Denunciar

0

import java.util.Scanner;

public class PigLatin {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.println("Please enter an English sentence: ");
        String sentence = input.nextLine();

        System.out.println("Original sentence: "+sentence);
        System.out.println("PigLatin conversion: "+convert(sentence));
    }

    private static String convert (String sentence) {

        String []words = sentence.split(" ");
        int i = 0;
        String pigLatin = "";
        while(i<words.length){ //CORRECTION CODE
            pigLatin+=words[i].substring(1,words[i].length())+words[i].charAt(0)+"ay"+" ";
            i++;
        }
        return pigLatin;
    }
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda