Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

176
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda