Thufir
Thufir

Reputation: 8487

how to enter an infinite loop until valid data is input?

The loop should "stay" on the same record until a valid State is entered. The valid options are logged as a warning: [undefined, x, o, c, a, l, d] but, apparently, the loop just continues on to the next record.

It should be an "infinite" loop until valid input is received.

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:~$ 

code:

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;
    }

}

I thought that I had the loop worked out, but can't see what the problem is, unfortunately.

Upvotes: 0

Views: 386

Answers (3)

Baalthasarr
Baalthasarr

Reputation: 367

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);

The Problem is in while (title.getState() == State.undefined); you never change the state of title and it has a state that is not 'undifined'. Now you compare it with the state undefined and that will be always false. So the inner while loop ends there.

use yure variable state or an boolean that is set to true and if the input is correct it will be set to false, so you get out of the inner while loop.

Try this:

 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);

EDIT: Like i saw it right now your previous code of this was right, because you compared the state with the State.undefined but then you changed it so the while loop ends after one round.

Upvotes: 2

Thufir
Thufir

Reputation: 8487

Trial and error solution:

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);
        }
    }
}

although I can't imagine what broke it, becuase, as I recall, it was looping correctly before.

output:

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:~$ 

Upvotes: 0

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35557

It is not a complex scenario. you can just try as follows

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

Upvotes: 0

Related Questions