Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

175
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!