Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The following code public class test {public static void main(String[] args) {St

ID: 3933848 • Letter: T

Question

The following code public class test {public static void main(String[] args) {String name - "Kirby Godsey"; boolean firstloop = true; while (true) {int nameLength = name.length(); if (firstloop) {int i = 0; firstloop = false;} if (i == nameLength) break; System.out.print(name.charAt(i++));} System, out .printIn (" as " + i + " characters in his name.")}} gives the following errors during compilation. test.java:15: cannot resolve symbol symbol: variable i location: class test if (i = nameLength) break; test.java:16: cannot resolve symbol symbol: variable i location: class test System. out. print (name. char At (i++)); test.java:18: cannot resolve symbol symbol: variable i location: class test System. out. printIn (" as " + i + " characters in his name."); 3 errors How can you fix this problem?

Explanation / Answer

public class test {

   public static void main(String[] args) {
       String name = "Kirby Godsey";
       boolean firstLoop = true;
       int i = 0;
       while (firstLoop) {

           int nameLength = name.length();
           /*
           * if (!firstLoop) {
           *
           * firstLoop = false;
           * }
           */
           if (i == nameLength)
               break;
           System.out.print(name.charAt(i++));

       }

       System.out.println(" as " + i + " characters in the name.");

   }
}

OUTPUT:
Kirby Godsey as 12 characters in the name.
NOTE:    if (firstLoop) {
           int i=0;
           firstLoop = false;
       }
       i is declared in the if block but accessing the i is out of the block, and also some logical errors also there please check

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote