0

i'm a beginner in java and i have these codes for my voting system:

public void Result_Election(){
        int vote1=Vote_President();
        String pres1="Theo";
        String pres2="William";
        String pres3="Maxon";
        String pres4="Douglas";
        int n1=0, n2=0, n3=0, n4=0;

        try{
            PrintWriter i=new PrintWriter(new FileWriter("voting_score.txt", true));
            if (vote1==1){
                int[] addVotes = {1};
                for (int add : addVotes){
                    result[add-1]+=1;
                    n1=result[add-1];
                }
                i.println(pres1+" "+n1);
            }
            else if (vote1==2){
                int[] addVotes = {2};
                for (int add : addVotes){
                    result[add-1]+=1;
                    n2=result[add-1];
                }
                i.println(pres2+" "+n2);
            }
            else if (vote1==3){
                int[] addVotes = {3};
                for (int add : addVotes){
                    result[add-1]+=1;
                    n3=result[add-1];
                }
                i.println(pres3+" "+n3);
            }
            else if (vote1==4){
                int[] addVotes = {4};
                for (int add : addVotes){
                    result[add-1]+=1;
                    n4=result[add-1];
                }
                i.println(pres4+" "+n4);
            }
            i.close();
        }catch (Exception e){
        }
    }

my problem is the output. every time i add votes to one candidate, it will add another name with its incremented votes. but i want is just one name per candidate and every time i add up votes to one candidate, it won't add another name. just the number of vote. please help

epascarello
  • 204,599
  • 20
  • 195
  • 236
lel
  • 1
  • 1

1 Answers1

1

To avoid all those variables and if-else block, can we simply do something like -

Map<String, Integer> candidates = new HashMap<String, Integer>();
candidates.put("Theo", 0);
candidates.put("William", 0);
candidates.put("Maxon", 0);
candidates.put("Douglas", 0);

switch (vote1) 
{
    case 1:
        candidates.put("Theo", candidates.get("Theo")+1);
    break;
    case 2:
        candidates.put("William", candidates.get("William")+1);
    break;
    case 3:
        candidates.put("Maxon", candidates.get("Maxon")+1);
    break;
    case 4:
        candidates.put("Douglas", candidates.get("Douglas")+1);
    break;
}

It will be easier to understand and debug. This is just an example. You can use it in the way you want.

I don't see any use of "int[] addVotes = {1};" and iterating over it as this is always going to hold only one value? What is your intention here? Also how did you initialize "result"?

[Update] Doing it in your way and cutting down unnecessary details :

public void Result_Election(){

    int vote1 = Vote_President();
    String[] candidateArray = {"Theo", "William", "Maxon", "Douglas"};
    String fileAbsolutePath = "C:/voting_score.txt";

    try
    {
        int[] result = getStorredResult(fileAbsolutePath, candidateArray);
        PrintWriter pw = new PrintWriter(new FileWriter(fileAbsolutePath));
        result[vote1-1] = result[vote1-1]+1;

        for (int i = 0; i < candidateArray.length; i++) {
            pw.println(candidateArray[i]+" "+result[i]);
        }

        pw.flush();
        pw.close();

    }
    catch (Exception e)
    {
        e.printStackTrace(); // better write in log
    }
}

private int[] getStorredResult(String fileName, String[] candidateArray) throws NumberFormatException, IOException {

    String currentLine = null;
    int[] result = new int[candidateArray.length];
    File file = new File(fileName);

    if(file.exists()) {  
        BufferedReader br = new BufferedReader(new FileReader(file));
        while((currentLine = br.readLine()) != null) {
            for (int i = 0; i < candidateArray.length; i++) {
                if(currentLine.startsWith(candidateArray[i])) {
                    result[i] = Integer.parseInt(currentLine.split(" ")[1]);
                    break;
                }
            }
        }
        br.close();
    }

    return result;
}
Kartic
  • 2,935
  • 5
  • 22
  • 43
  • i would really use this IF i know what is the use of the HashMap. believe it or not, this is my project and i need to defend what is that code or that code. can you suggest any easier way? it's fine if it will make my codes longer (i know it will be great if the codes are shorter as possible) but i want my codes to be understandable enough that i can defend it to our prof. – lel Mar 17 '15 at 16:42
  • In that case, I'll surely have a look. How did you initialize "result"? – Kartic Mar 17 '15 at 19:21