2014-10-30 66 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循环。

相关问题