如何在输入有效数据之前进入无限循环?

时间:2014-07-07 12:28:40

标签: java loops while-loop infinite-loop do-while

循环应该"保持"在同一记录上,直到输入有效的State。有效选项被记录为警告:[undefined, x, o, c, a, l, d]但显然,循环只会继续到下一条记录。

成为"无限"循环,直到收到有效输入。

thufir@dur:~$ 
thufir@dur:~$ java -jar NetBeansProjects/Client/dist/Client.jar 
Jul 07, 2014 5:23:39 AM net.bounceme.dur.client.driver.Client inputOutput
INFO: 

id  1
phone   456465
title   record number 1
state   x
enter the state for record:fjdkfjd
Jul 07, 2014 5:23:43 AM net.bounceme.dur.client.driver.Client inputOutput
WARNING: [undefined, x, o, c, a, l, d]
Jul 07, 2014 5:23:43 AM net.bounceme.dur.client.driver.Client inputOutput
INFO: 

id  2
phone   123
title   second record
state   o
enter the state for record:^Cthufir@dur:~$ 
thufir@dur:~$ 

代码:

package net.bounceme.dur.client.driver;

import net.bounceme.dur.data.Title;
import java.net.*;
import java.io.*;
import java.util.Arrays;
import java.util.logging.Logger;
import net.bounceme.dur.data.State;

public class Client {

    private static final Logger log = Logger.getLogger(Client.class.getName());
    private String server = "localhost";
    private int portNumber = 8080;
    private final Console c = System.console();

    private Client() {
    }

    public Client(String server, int portNumber) {
        this.server = server;
        this.portNumber = portNumber;
    }

    public void inputOutput() throws IOException, ClassNotFoundException {
        Socket socket = new Socket(server, portNumber);
        boolean eof = false;
        Title title = null;
        String stringState = null;
        State state = State.undefined;
        try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
                ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream())) {
            do {
                try {
                    title = (Title) objectInputStream.readObject();
                    log.info(title.toString());
                    do {
                        c.printf("enter the state for record:");
                        stringState = null;
                        state = State.undefined;
                        try {
                            stringState = c.readLine();
                            state = State.valueOf(stringState);
                        } catch (java.lang.IllegalArgumentException iae) {
                            log.warning(Arrays.deepToString(State.values()));
                        }
                    } while (title.getState() == State.undefined);
                    title.setState(state);
                    log.fine(title.toString());

                    title.setTitle("modified from client");
                    objectOutputStream.writeObject(title);
                } catch (java.io.EOFException eofe) {
                    eof = true;
                }
            } while (!eof);
        }
    }
}


package net.bounceme.dur.data;

public enum State {

    undefined(0), x(1), o(2), c(3), a(4), l(5), d(6);
    private int code = 0;

    State(int code) {
        this.code = code;
    }

    public int getCode() {
        return this.code;
    }

    public static State getState(int i) {
        for (State state : State.values()) {
            if (state.getCode() == i) {
                return state;
            }
        }
        return undefined;
    }

}

我认为我已经完成了循环,但不幸的是,无法看到问题所在。

3 个答案:

答案 0 :(得分:2)

do {
  c.printf("enter the state for record:");
  stringState = null;
  state = State.undefined;
  try {
    stringState = c.readLine();
    state = State.valueOf(stringState);
  } catch (java.lang.IllegalArgumentException iae) {
    log.warning(Arrays.deepToString(State.values()));
  }
} while (title.getState() == State.undefined);

问题出在while (title.getState() == State.undefined);,你永远不会改变标题的状态,并且它的状态不是“未分类的”。现在将它与未定义的状态进行比较,这将始终为false。所以内部while循环在那里结束。

使用yure变量状态或设置为true的布尔值,如果输入正确,则将其设置为false,这样就可以摆脱内部while循环。

试试这个:

 do {
  c.printf("enter the state for record:");
  stringState = null;
  state = State.undefined;
  try {
    stringState = c.readLine();
    state = State.valueOf(stringState);
  } catch (java.lang.IllegalArgumentException iae) {
    log.warning(Arrays.deepToString(State.values()));
  }
} while (state == State.undefined);

修改 就像我现在看到的那样,你之前的代码是正确的,因为你将状态与State.undefined进行了比较,但是你改变了它,所以while循环在一轮之后结束。

答案 1 :(得分:0)

这不是一个复杂的场景。你可以尝试如下

boolean status=true; 
while(status){
  if("matching_input".equals(your_input)){
    status=false;
    // now loop will exit
  }
}

答案 2 :(得分:0)

试错法解决方案:

package net.bounceme.dur.client.driver;

import net.bounceme.dur.data.Title;
import java.net.*;
import java.io.*;
import java.util.Arrays;
import java.util.logging.Logger;
import net.bounceme.dur.data.State;

public class Client {

    private static final Logger log = Logger.getLogger(Client.class.getName());
    private String server = "localhost";
    private int portNumber = 8080;
    private final Console c = System.console();

    private Client() {
    }

    public Client(String server, int portNumber) {
        this.server = server;
        this.portNumber = portNumber;
    }

    public void inputOutput() throws IOException, ClassNotFoundException {
        Socket socket = new Socket(server, portNumber);
        boolean eof = false;
        Title title = null;
        String stringState = null;
        State state = State.undefined;
        try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
                ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream())) {
            do {
                try {
                    title = (Title) objectInputStream.readObject();
                    do {
                        try {
                            log.info(title.toString());
                            c.printf("enter the state for record:");
                            stringState = null;
                            state = State.undefined;
                            stringState = c.readLine();
                            state = State.valueOf(stringState);
                            log.warning(state.toString());
                        } catch (java.lang.IllegalArgumentException iae) {
                            log.warning(Arrays.deepToString(State.values()));
                        }
                    } while (state == State.undefined);
                    title.setState(state);
                    log.fine(title.toString());

                    title.setTitle("modified from client");
                    objectOutputStream.writeObject(title);
                } catch (java.io.EOFException eofe) {
                    eof = true;
                }
            } while (!eof);
        }
    }
}

虽然我无法想象是什么打破了它,因为我记得,它之前正在循环。

输出:

thufir@dur:~$ 
thufir@dur:~$ java -jar NetBeansProjects/Client/dist/Client.jar 
Jul 07, 2014 5:41:33 AM net.bounceme.dur.client.driver.Client inputOutput
INFO: 

id  1
phone   456465
title   record number 1
state   undefined
enter the state for record:fjdskl
Jul 07, 2014 5:41:36 AM net.bounceme.dur.client.driver.Client inputOutput
WARNING: [undefined, x, o, c, a, l, d]
Jul 07, 2014 5:41:36 AM net.bounceme.dur.client.driver.Client inputOutput
INFO: 

id  1
phone   456465
title   record number 1
state   undefined
enter the state for record:fjdksl
Jul 07, 2014 5:41:37 AM net.bounceme.dur.client.driver.Client inputOutput
WARNING: [undefined, x, o, c, a, l, d]
Jul 07, 2014 5:41:37 AM net.bounceme.dur.client.driver.Client inputOutput
INFO: 

id  1
phone   456465
title   record number 1
state   undefined
enter the state for record:a
Jul 07, 2014 5:41:38 AM net.bounceme.dur.client.driver.Client inputOutput
WARNING: a
Jul 07, 2014 5:41:38 AM net.bounceme.dur.client.driver.Client inputOutput
INFO: 

id  2
phone   123
title   second record
state   o
enter the state for record:fjklds
Jul 07, 2014 5:41:42 AM net.bounceme.dur.client.driver.Client inputOutput
WARNING: [undefined, x, o, c, a, l, d]
Jul 07, 2014 5:41:42 AM net.bounceme.dur.client.driver.Client inputOutput
INFO: 

id  2
phone   123
title   second record
state   o
enter the state for record:c
Jul 07, 2014 5:41:46 AM net.bounceme.dur.client.driver.Client inputOutput
WARNING: c
Jul 07, 2014 5:41:46 AM net.bounceme.dur.client.driver.Client inputOutput
INFO: 

id  3
phone   14564654
title   etc
state   x
enter the state for record:^C
thufir@dur:~$