I really need your help! I am new to java and have some problems with my code. I have a program which inputs questionnaires and creates an object for each questionnaire. These objects are then saved in a vector which in turn is then encoded in an XML file. For all this I have made a class called MainInput. The problem is with MainOutpu class. I want to make a class that prints the questionnaires in alphabetical order (which is already done) and then shows the number of people who responded to the same question with the same response. Moreover I also need to make some statistics like taking the average of all ages, another saying the maximum age, the minimum age...
Here is the code of my MainOutput. If anyone can help me it would be greatly apprciated as this is for an assignment that needs to be given tomorrow! Thanks a lot!
Expand|Select|Wrap|Line Numbers
- import java.beans.XMLDecoder;
- import java.beans.XMLEncoder;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.Scanner;
- import java.util.Vector;
- import java.util.Comparator;
- import java.util.*;
- import java.math.BigDecimal;
- class MainOutput{
- private static int lessthan25 = 0, twentysixto45 = 0, biggerthan46 = 0, male = 0, female = 0, age;
- private static char sex;
- private static int answers[];
- private static int format [];
- private static int responses[][];
- private static BigDecimal average;
- public static void printQuestionnaire(QuestionnaireFields questionnaire){
- System.out.println("SerialNumber: " + questionnaire.getSerialNumber());
- System.out.println("Sex: "+ questionnaire.getSex());
- System.out.println("Age: " + questionnaire.getAge());
- System.out.println("Postcode: " +questionnaire.getPostcode());
- System.out.println("Answers");
- int[] answers = questionnaire.getAnswers();
- for(int count=0; count<answers.length; count++){
- System.out.print(" Answer "+(count+1)+ ": " + answers[count]);
- }
- System.out.println();
- System.out.println();
- }
- public static void getStatistics (QuestionnaireFields questionnaire)throws IOException{
- Vector<QuestionnaireFields> loaded = null;
- XMLDecoder decoder = null;
- try {
- decoder = new XMLDecoder(new BufferedInputStream(
- new FileInputStream("QuestionnaireData.xml")));
- loaded = (Vector<QuestionnaireFields>) decoder.readObject();
- } catch (Exception e) {
- loaded = new Vector<QuestionnaireFields>();
- } finally {
- if (decoder != null) {
- decoder.close();
- }
- }
- age = questionnaire.getAge();
- if (age <= 25){
- lessthan25 ++;
- }else if (age >25 && age <45){
- twentysixto45++;
- }else{
- biggerthan46++;
- }
- Vector averageAge = new Vector();
- averageAge.add (age);
- Iterator iter = averageAge.iterator();
- BigDecimal bdSum = new BigDecimal(0);
- while(iter.hasNext()){
- bdSum = bdSum.add((BigDecimal)iter.next());
- }
- BigDecimal average = bdSum.divide(BigDecimal.valueOf(averageAge.size()));
- sex = questionnaire.getSex();
- if(sex == 'm' | sex == 'M'){
- male++;
- }else{
- female++;
- }
- answers = questionnaire.getAnswers();
- for(int count=0; count<responses.length; count++){
- for(int count1=0; count1<responses[count].length; count1++){
- if (answers[count] == (responses[count].length)){
- responses[count][count1]++;
- }
- }
- }
- }
- public static void main (String [] args) throws IOException{
- Vector<QuestionnaireFields> loaded = null;
- XMLDecoder decoder = null;
- try {
- decoder = new XMLDecoder(new BufferedInputStream(
- new FileInputStream("QuestionnaireData.xml")));
- loaded = (Vector<QuestionnaireFields>) decoder.readObject();
- } catch (Exception e) {
- loaded = new Vector<QuestionnaireFields>();
- } finally {
- if (decoder != null) {
- decoder.close();
- }
- }
- Collections.sort (loaded, new Comparator<QuestionnaireFields>(){
- public int compare (QuestionnaireFields one, QuestionnaireFields two){
- return one.getSerialNumber()- two.getSerialNumber();
- }
- });
- for (int count = 0; count < loaded.size(); count ++){
- printQuestionnaire (loaded.get (count));
- }
- format = CheckingStructureSetting.loading();
- responses = new int [format.length][];
- for (int count = 0; count<format.length; count++){
- responses[count] = new int [format[count]];
- }
- for (int count = 0; count < loaded.size(); count ++){
- getStatistics (loaded.get (count));
- }
- System.out.println ("Age: <= 25: "+lessthan25+" 26-45: "+twentysixto45+" >=46: "+biggerthan46);
- System.out.println("Male: "+male+" Female: "+female+" Total: "+(male+female));
- System.out.println("Average age" + average);
- for (int count=0; count<responses.length; count++){
- System.out.println ("Question: "+(count+1));
- System.out.print ("Responses: ");
- for(int count1=0; count1<responses[count].length; count1++){
- System.out.print (" "+(count1+1) + ":");
- System.out.print (responses[count][count1]);
- if ((count1+1) == responses[count].length){
- System.out.println(" ");
- }
- }
- }
- }
- }