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

Why does my java letter counter code not work? The only output I get is the orig

ID: 3665727 • Letter: W

Question

Why does my java letter counter code not work?

The only output I get is the original string. Ive tried a few things but I cannot get the value of numX, numY or numZ to save to the variable as it prints nothing every time for them.

Example:

Input: xxyz

output:

Original String: xxyz

public class proj2
{
public static void main(String[] args)
{

String a = args[0];
int numX = 0;
int numY = 0;
int numZ = 0;

System.out.println("Original String: "+a);


for (int i=0; i <= (a.length()); i++)
{

if(a.charAt(i) == ('x'))
{
numX++;
}
  
if(a.charAt(i) == 'y')
{
numY++;
}

if(a.charAt(i) == 'z')
{
numZ++;
}

  
}
System.out.printf("numX: %d", numX);
System.out.printf("numY: %d", numY);
System.out.printf("numZ: %d", numZ);
}
}

Explanation / Answer

public class Proj2

{

String a = args[0];

int numX = 0;

int numY = 0;

int numZ = 0;

public static void main(String[] args)

{

System.out.println("enter u r string");

System.out.println("Original String: "+a);

for (int i=0; i <= (a.length()); i++)

{

if(a.charAt(i) == ('x'))

{

numX++;

}

if(a.charAt(i) == 'y')

{

numY++;

}

if(a.charAt(i) == 'z')

{

numZ++;

}

}

System.out.printf("numX:"+numX);

System.out.printf("numY:"+ numY);

System.out.printf("numZ:"+ numZ);

}

}

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