#How can I catch the wrong format of input and let the user re-write it?

1 messages · Page 1 of 1 (latest)

native sedge
#

I have following code:
`
public class HumanPlayer implements Player {
private final PrintStream out;
private final Scanner in;

public HumanPlayer(final PrintStream out, final Scanner in) {
    this.out = out;
    this.in = in;
}

public HumanPlayer() {
    this(System.out, new Scanner(System.in));
}

@Override
public Move move(final Position position, final Cell cell) {
    while (true) {
        out.println("Position");
        out.println(position);
        out.println(cell + "'s move");
        out.println("Enter row and column");
        final Move move = new Move(in.nextInt(), in.nextInt(), cell);
        if (position.isValid(move)) {
            return move;
        }
        final int row = move.getRow();
        final int column = move.getColumn();
        out.println("Move " + move + " is invalid");
    }
}

} `
Is there any way to let user correct their input? (for example, he will have the opportunity to input values again if he accidentaly wrote "hello world" instead of numbers)

karmic epochBOT
#

<@&987246399047479336> please have a look, thanks.

karmic epochBOT
# native sedge I have following code: ` public class HumanPlayer implements Player { priva...

Detected code, here are some useful tools:

Formatted code
I have following code : `public class HumanPlayer implements Player {
  private final PrintStream out;
  private final Scanner in;
  public HumanPlayer(final PrintStream out, final Scanner in) {
    this .out = out;
    this .in = in;
  }
  public HumanPlayer() {
    this (System.out, new Scanner(System.in));
  }
  @Override
  public Move move(final Position position, final Cell cell) {
    while (true) {
      out.println("Position");
      out.println(position);
      out.println(cell + "'s move");
      out.println("Enter row and column");
      final Move move = new Move(in.nextInt(), in.nextInt(), cell);
      if (position.isValid(move)) {
        return move;
      }
      final int row = move.getRow();
      final int column = move.getColumn();
      out.println("Move " + move + " is invalid");
    }
  }
}
`Is there any way to let user correct their input ? (for example, he will have the opportunity to input values againif he accidentaly wrote"hello world"instead of numbers)
thin stag
#

i.e. a classic
while (invalid) { read input }

#

ideally move it into a helper method to not clutter ur code

somber ruin
#

hi there!

#

can you show us what your main() looks like ?

gray skiff
native sedge
thin stag
#

u have to attempt parsing it and catch the exception

#

its a bit annoying

#

see here: #snippets message

native sedge
#

yes, but i have 2 or 3 numbers in input line...

thin stag
gray skiff
native sedge
native sedge
gray skiff
thin stag
#

if u call nextInt() and there is no int, scanner throws a NoSuchElementException

#

well, i guess u could catch that as well and then do ur repeat stuff

#

but still, not ideal

native sedge
#

and string tokenizer as well

woeful stag
gray skiff
#

this will be too easier

thin stag
#

u can also use next() instead of nextInt()

#

and then attempt to parse with the shown method

#

but that also wont work if the user only inputs one thing

woeful stag
thin stag
#

the safest and easiest route would be to use nextLine() and then split by space and then check length and attempt to parse both as int

#

if u cant do that, it will be a bit annoying

native sedge
thin stag
#

exception

#

if u tell scanner to read sth and its not there or incorrect, ull get NoSuchElementException

#

and if ur not allowed to use split, u have to go that route

#

unless u want to implement split manually urself

#

with a combination of find and substring

gray skiff
#

or just traverse it i guess lol

thin stag
#

its still fairly straightforward, just a bit more cumbersome

#

perhaps 15 lines of code all together, for the entire validate/repeat thing

somber ruin
#

i'd suggest to use do..while

#

I like it in such programs

#

to give the user the 1st time to enter something, and repeat as long as it's invalid

thin stag
#

thats not the problem at hand though

#

the problem is the validation of multiple inputs in a single line

#

while sticking to the teachers constrains

somber ruin
#

i think using sc.hasNext()

thin stag
#

stick to nextInt instead

#

this is one case

#

and this the other:

#

so all u need to do is call nextInt() and if u get one of those 2 exceptions, it failed

#

no exception and its correct

#

if it failed, dont forget to call nextLine() once to purge the rest of the input

#

then loop into the next round

#

but step by step

#

get started and then share where ur stuck at, or is sth unclear still? 🙂 @native sedge

native sedge
#

ok, i will try, thank you 😄