The following code public class test public static void main

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?

Solution

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

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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site