Java - 如何存储String的先前值并将其与当前值进行比较

时间:2015-09-23 12:59:11

标签: java string

假设我有一个名为read的String变量。此变量从用户输入获取值,保存在另一个类中并打印该值。 如果在第二次给出相同的值,我不希望它被打印。 我做了这样的事情,但它不起作用。

private String pmsg;

String read = GlobalState.getmsg();

boolean pch = read.equals(pmsg);

if (!pch) {
    pmsg = new String(read);
    System.out.println(read);
}

似乎每当read发生变化时,pmsg也会发生变化,因此无法打印,因为pch始终为真。我想存储String的前一个值,并将其与String的当前值进行比较。我如何用Java做到这一点?

2 个答案:

答案 0 :(得分:0)

现在正在运作。 Setmsg()和getmsg()在while(true)循环中位于两个独立的线程中。我所做的就是在while循环中添加以下语句;了Thread.sleep(500);

          class  MThread extends Thread {

    private DatagramSocket dsocket; 


        public void run() {
            try {
                   int port = 8888;
                   // Create a socket to listen on the port.
                     dsocket = new DatagramSocket(port);
                   // Create a buffer to read datagrams into.
                    byte[] buffer = new byte[30000];
                   // Create a packet to receive data into the buffer
                   DatagramPacket packet = new DatagramPacket(buffer,buffer.length);
                    while (true) {
                               try {
                                      // Wait to receive a datagram
                                       dsocket.receive(packet);
                                      //Convert the contents to a string,
                                      String msg = new String(buffer, 0, packet.getLength());



                                      GlobalState.setmsg(msg);

                                       // Reset the length of the packet before reusing it.
                                         packet.setLength(buffer.length);
                                         Thread.sleep(500); 


                                    } catch (Exception e) {
                                       dsocket.close();
                                        break; }
                                  }

                } catch (Exception e) {
                  dsocket.close();  e.printStackTrace();}

        }

 }


class PThread extends Thread {


private String pmsg;
        public void run() {
            try {

                while (true) {
                    try {

                       String read = GlobalState.getmsg();



                     //  boolean rdch;
                        boolean pch= read.equals(pmsg);

                       if(!pch)
                       {
                          pmsg =new String(read);
                           System.out.println(read);

                        }

                       Thread.sleep(500);


                      } catch (Exception e) { System.out.println("error");
                    e.printStackTrace();}

        } //while

            } catch (Exception e) {
                    e.printStackTrace();}        
        }         


 }

答案 1 :(得分:-1)

我会在您的代码末尾设置pmsg。这样的事情应该可以实现你想要的东西(还要注意我在String.equals()语句中包含if检查的方式,这样你就不必创建boolean)。

//Declare variables
private String pmsg = null; //You should probably set this to null so even if your first read is "" it will still print
private String read;

//Method to read and print message
public void readMessage(){
    read = GlobalState.getmsg();

    if (!read.equals(pmsg)){  //Checks if read is the same as the previous message
        System.out.println(read);
    }

    pmsg = read;  //Cache previous message
}

希望这会有所帮助。