2014-10-30 42 views
1

我想寫一個程序,我使用UDP客戶端/服務器進行投票。客戶輸入其選擇的候選人,將其發送到服務器,服務器記錄候選人的投票,併爲25個有效投票執行此操作。是否有可能在java中重用字符串?

進入第一個投票工作,但是,我輸入剩餘的選票時出現問題,因爲我在while循環中使用相同的字符串將客戶投票與數組中的一個字符串進行比較。 (不知道如果我解釋這顯然)

class UDPVoteServer { 

public static final String[] candidates = new String[] { 
    "Peter Singh", 
    "Ricardo Allen", 
    "Winston Alibocas", 
    "Linda Jenkins", 
    "Marlene Williams" 
};// 5 candidates to choose from 

public static void main(String args[]) throws Exception { 

    DatagramSocket serverSocket = new DatagramSocket(9816); 
    System.out.println("UDP Server Started\n"); 
    byte[] receiveData = new byte[1024]; 
    byte[] sendData = new byte[1024]; 
    int maxVotes = 0; int hacked = 0; 
while(true){ 

     int success = 0; int invalid = 0; int numVotes = 0; 
     String canVote= ""; 

     int[] record = new int [5]; // records votes for each of the 5 candidates 
     for(int i=0; i<5; i++) record[i] = 0; 

     while (maxVotes < 25){ 

      DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); 
      serverSocket.receive(receivePacket); 

      String clientIP = receivePacket.getAddress().getHostAddress(); 

      System.out.println("Received vote from client IP "+clientIP); 

      /*block a hacker IP from trying to vote*/ 
      String blockedIP = "109.211.55.44"; 


      if (clientIP.equals(blockedIP)){ 
       System.out.println("\nRestricted IP contact made"); 
       InetAddress IPAddress = receivePacket.getAddress(); 

       int port = receivePacket.getPort(); 
       String error = "Your machine has been debarred from voting!"; 

       sendData = error.getBytes(); 

       DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); 
       serverSocket.send(sendPacket); 

       hacked++; 
       break; //exit loop if a blocked ip tries to access server 

      } 

     String VoterPick = new String(receivePacket.getData());//from client 
     canVote = VoterPick.trim().replaceAll(" +", " "); //removes extra white spaces 
     System.out.println(canVote); 

     InetAddress IPAddress = receivePacket.getAddress(); 
     int port = receivePacket.getPort(); 
     String result= ""; 

      for(String pick : candidates) { 

       for(int i=0; i<5; i++){ 

       if(canVote.equalsIgnoreCase(pick)){ //checks to see if voter spelt candidate name correctly after removing unnecessary white space 
        //Calculate voting here 

        record[i] = numVotes + 1; //adds a vote to the respective candidate 


        result = "Vote Successful"; 
        success++; 
       maxVotes++; //increment only if the vote was successful; therefore loop stops when there are 25 valid votes entered 
        sendData = result.getBytes(); 
        break; 
       } 

       //if spelt wrong record it as an invalid vote 
       else { 
        result = "Invalid Candidate"; 
        invalid++; 
        sendData = result.getBytes(); 
        break; 
       } 
       } 
      } 

      DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); 

      serverSocket.send(sendPacket); 

      //maxVotes++; 
      //canVote = ""; //this does not seem to work 

     } 
     System.out.println("Hacker tried to access server " +hacked+ " times."); 
     System.out.println("Successful votes: " +success); 
     System.out.println("Invalid Votes: " +invalid +"\n"); 
     for(int i=0; i<5; i++) { 
       System.out.println(candidates[i] + " " + record[i]); 
     } 
    } 
    } 

} 

我試圖清除存儲在字符串canVote的信息,因此新candidateVote可以存儲在字符串canVote,但它似乎沒有不工作,因此即使輸入正確,我仍然會在客戶端上打印無效的候選人。

是否有可能重用字符串canVote還是有另一種方式來存儲信息?

欣賞任何幫助。

+0

numVotes從來沒有分配任何值除了0.不知道我看到記錄數組的點,因爲每個元素都結束了。 – CandiedOrange 2014-10-30 04:30:28

+0

記錄數組是爲了記錄每個候選人爲了輸出贏家而收到的票數。然而,這是未完成的代碼,我試圖解決我的字符串問題之前使程序更復雜,更難以調試。 – user3054901 2014-10-30 04:42:38

+0

我明白這是未完成的,但這讓我感到困惑。爲什麼每個候選人需要5票投票?什麼是25票有效票? 25選民?我目前的猜測是,你不需要靜態canVote來清除它並重用它。幫助我理解代碼背後的目的,我將能夠提供幫助。也許給代碼添加一些註釋? – CandiedOrange 2014-10-30 05:00:51

回答

2

如果你想在函數調用之間持續使用canVote變量,你可以使它變成static,但是你應該謹慎的是,沒有額外的邏輯,它不會是線程安全的。

// static class variable 
private static String canVote = ""; 

// reference in your method as... 
MyClass.canVote = "something"; 

也有類似的數據庫,分佈式緩存等其它持久數據存儲選擇,但你又需要考慮你的代碼是如何線程安全的。

+0

這是正確的答案(upvoted)...更改我的評論。錯誤的問題。繼續。 – mttdbrd 2014-10-30 04:16:36

1

我不相信這一點:

 for(String pick : candidates) { 

      for(int i=0; i<5; i++){ 

       if(canVote.equalsIgnoreCase(pick)){ //checks to see if voter spelt candidate name correctly after removing unnecessary white space 

做什麼,你認爲它。

也就是說將循環時,有5名候選人足夠但對於每一個候選(名爲pick)確保它checkes相同canVote 5倍記錄在record陣列的不同元件相同的結果的25倍。

如果沒有i循環並且在候選循環內只增加i,您可能會更好。你需要一個外部循環,直到有效投票數達到25。可能有一個while循環。

相關問題