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

Can someone help me shorten this method or write it in a different manner? publi

ID: 3545028 • Letter: C

Question

Can someone help me shorten this method or write it in a different manner?



public static void countNums(String nums)

{

int x;

int[] count = new int[5];

for(int y = 0; y < count.length; y++)

count[y] = 0;

for(int c = 0;c < nums.length();c++)

{

x = Integer.parseInt(nums.charAt(c)+"");

if(x == 1)

count[0]++;

else if(x == 2)

count[1]++;

else if (x == 3)

count[2]++;

else if (x == 4)

count[3]++;

else

count[4]++;

}

int m = 1;

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

{

System.out.println(m+": "+count[d]);

m++;

}

}

}

Can someone help me shorten this method or write it in a different manner?

Explanation / Answer

public class vote{

public static void countNums(String nums)

{

int x;

int[] count = new int[5];

for(int y = 0; y < count.length; y++)

count[y] = 0;

for(int c = 0;c < nums.length();c++)

{

x = Integer.parseInt(nums.charAt(c)+"");

if(x>0){

count[x-1]++;

}}

int m = 1;

for(int d = 0; d < count.length; d++)

{

System.out.println(m+": "+count[d]);

m++;

}

}

}

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