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++;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.