
class Studnt implements Comparable {
String name;
int marks;
}
public class StudentData {
public static void main(String[] args) {
List<Studnt> stuList=new ArrayList<Studnt>();
Studnt s1=new Studnt("name",70);
Studnt s2=new Studnt("anil",54);
Studnt s3=new Studnt("name",75);
Studnt s4=new Studnt("name",45);
Studnt s5=new Studnt("name",74);
Studnt s6=new Studnt("anil",70);
Studnt s7=new Studnt("hi",50);
Studnt s8=new Studnt("hello",40);
Studnt s9=new Studnt("anil",50);
Studnt s10=new Studnt("hi",60);
Studnt s11=new Studnt("hi",80);
stuList.add(s1);
stuList.add(s2);
stuList.add(s3);
stuList.add(s4);
stuList.add(s5);
stuList.add(s6);
stuList.add(s7);
stuList.add(s8);
stuList.add(s9);
stuList.add(s10);
stuList.add(s11);
}
## Heading ##
hi friends i want this type of output give me sol plz=====
Anil::174
hello::40
hi::190.

import java.util.*;
class Studnt{
String name;
int marks;
Studnt(String name,int marks){
this.name=name;
this.marks=marks;
}
public String getName(){
return name;
}
public int getMarks(){
return marks;
}
}
public class StudentData{
private static String[] getUniqueElements(String[] allStrs){
String[] temp = new String[allStrs.length];
int count = 0;
for(int j = 0; j < allStrs.length; j++){
if(isUnique(allStrs[j], temp))
temp[count++] = allStrs[j];
}
String[] uniqueStrs = new String[count];
System.arraycopy(temp, 0, uniqueStrs, 0, count);
return uniqueStrs;
}
private static boolean isUnique(String s, String[] array){
for(int j = 0; j < array.length; j++){
if(array[j] != null && s.equals(array[j]))
return false;
}
return true;
}
public static void main(String[] args) {
List<Studnt> stuList=new ArrayList<Studnt>();
Studnt s1=new Studnt("name",70);
Studnt s2=new Studnt("anil",54);
Studnt s3=new Studnt("name",75);
Studnt s4=new Studnt("name",45);
Studnt s5=new Studnt("name",74);
Studnt s6=new Studnt("anil",70);
Studnt s7=new Studnt("hi",50);
Studnt s8=new Studnt("hello",40);
Studnt s9=new Studnt("anil",50);
Studnt s10=new Studnt("hi",60);
Studnt s11=new Studnt("hi",80);
stuList.add(s1);
stuList.add(s2);
stuList.add(s3);
stuList.add(s4);
stuList.add(s5);
stuList.add(s6);
stuList.add(s7);
stuList.add(s8);
stuList.add(s9);
stuList.add(s10);
stuList.add(s11);
int i=0;
String names[]=new String[stuList.size()];
for(Studnt std:stuList){
names[i]=std.getName();
i++;
}
int sum=0;
String name[]=getUniqueElements(names);
int tot[]=new int[name.length];
for(int k=0;k<name.length;k++){
for(Studnt std:stuList){
if(std.getName().equals(name[k])){
sum+=std.getMarks();
}
}
tot[k]=sum;
}
for(int k=name.length-1;k>=0;k--){
if(k==0){
System.out.println(name[0]+" "+(tot[0]));
}
else{
System.out.println(name[k]+" "+(tot[k]-tot[k-1]));
}
}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.