[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 393

# generate percentage in java

hi guys

I have a requirment where user has 30 answers to fill in a UI form. The answers are then stored in a java object AnswerVO. finally the answers are stored  in a list

List answerList = new ArrayList()
for int i=0; i< 30; i++
{
}

public Class AnswerVO {
set()
get()
}

Based on the number of answers filled out by users i have to generate a percentage.
any idea how best that can be done?

thanks
0
royjayd
3 Solutions

Commented:
A percentage of what though?
0

Commented:
If it's a percentage of the complete number of answers, it would be
``````double percent = n / 30.0 * 100; // (n is number of answers filled in)
``````
0

Author Commented:
i have to check how many answers are NOT empty (meaning they are filled with answers) and then calcualte how many of the answers have been filled out of the 30 and generate a percentage completed
0

Commented:
You probably check how many correct answers, say you have method
returning boolean for correct answer and then
something like that:

int count = 0;

for(int j=0; j<answerList.size(); j++){
if(aa.correct())count++;
}

float percent = ((float)count/(float)answerList.size())*100.0

Sistem.out.println("Good answers: " + percent + "%");

0

Commented:
Then you should hav ethe checck method if answer is responded
int count = 0;

for(int j=0; j<answerList.size(); j++){
}

float percent = ((float)count/(float)answerList.size())*100.0

Sistem.out.println(" answers responded: " + percent + "%");
0

Commented:
Your code would be something like
``````List<AnswerVO> answers = ...;
if (userAnswer != null && userAnswer.trim().length() > 0) {
count++;
}
}
double percent = count / 30.0 * 100; // (don't use the magic number 30 though)
``````
0

Commented:

Then you should hav ethe checck method if answer is responded
You just need to know what is the correcrt name of your method
for checking if there i answer:

int count = 0;

for(int j=0; j<answerList.size(); j++){
}

float percent = ((float)count/(float)answerList.size())*100.0
formatter = new DecimalFormat("#.#");

Sistem.out.println(" answers responded: " + formatter.format(percent )+ "%");
0

Commented:
have a user object that wraps over AnswerVO. if percentage alone needed then there is no need to store the answer objects in the list at all. its enough to have two counts as correctAnswerCount or answeredCount and wrongAnsweredCount or notAnsweredCount.

public class User
{

private int correctAnswerCount = 0;
private int wrongAnswerCount = 0;

{
if(vo.get() != null)
{
}
else
{
}
}

public int getPercentage()
{