I have an array of orange, orange, green, blue, purple, pink, blue, pink I need
ID: 3537461 • Letter: I
Question
I have an array of orange, orange, green, blue, purple, pink, blue, pink
I need to display the different colors and how many of each one that I have. I know that I need to do a count but I can't seem to figure out where to place it. Here is the code for just this portion of my program that I have so far for that method.
for(int i=0;i<color.length;i++){
boolean isDistinct = false;
for(int j=0;j<i;j++){
if(color[i] == color[j]){
isDistinct = true;
// int counts= new int[j]++;
// color+=i;
break;
}
}
if(!isDistinct){
// color+=i;
System.out.print(color[i]+" ");
System.out.println(" ");
}
}
I would like for my printout to display orange 3
green 1
blue 2
purple 1
pink 2
Explanation / Answer
please rate - thanks
any changes needed let me know, I'll make them
public class main
{public static void main(String[] args)
{String color[]={"orange", "orange", "green", "blue", "purple", "pink", "blue", "pink"};
int count=0;
boolean[] counted=new boolean[color.length];
for(int i=0;i<counted.length;i++)
counted[i]=false;
for(int i=0;i<color.length;i++){
boolean isDistinct = false;
if(!counted[i])
{isDistinct=true;
count=1;
counted[i]=true;
for(int j=i+1;j<color.length;j++){
if(color[i].equals(color[j])){
count++;
counted[j]=true;
}
}
if(isDistinct)
{System.out.print(color[i]+" "+count);
System.out.println();
}
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.