Moy_Desu
Moy_Desu

Reputation: 19

User Input Error, no throwing Exceptions or using try{}catch{}

I'm trying to create a method that uses a while loop that includes a look ahead method to handle wrong user input: input out of 1-10 range or input a string. I am trying to do this with out throwing exceptions or using try{}catch{}; if possible. I haven't found a post that does not use these and all my attempts have failed so far. A basic idea will work.

will not stop if input is correct

import java.util.*;
public class UserErrors{

  public static Scanner console = new Scanner(System.in);
  public static void main(String[]args){
    String s  = "Enter a integer between 1-10: ";
  get(s);
  }
  public static int get(String prompt){

    System.out.print(prompt);
    while(console.hasNext()){
      while(!console.hasNextInt()){
        console.next();
        System.out.println("Invalid data type");
        System.out.print(prompt);
      }
      if(console.nextInt() > 10 || console.nextInt() <1){
        System.out.println("not in range");
        System.out.print(prompt);
      }
    }
    return console.nextInt();
  }
}

requires the right answer to be imputed 3 times before it stops

import java.util.*;
public class UserErrors{
  public static Scanner console = new Scanner(System.in);
  public static void main(String[]args){
    String s  = "Enter a integer between 1-10: ";
  get(s);
  }
  public static int get(String prompt){

    System.out.print(prompt);
    boolean b = false;
    while(!b){
      if(!console.hasNextInt()){
        console.next();
        System.out.println("Invalid data type");
        System.out.print(prompt);
        console.nextInt();
      }
      else if(console.nextInt() < 10 && console.nextInt() >1){
        b = true;
      }
      else{
        System.out.println("not in range");
        System.out.print(prompt);
        console.nextInt();
      }
    }
    return console.nextInt();
  }
}

I deleted some other failed attempts too. What do I need to fix (basic idea will do)?

Upvotes: 0

Views: 60

Answers (3)

benoit doll
benoit doll

Reputation: 19

The method nextInt() of the Scanner class read the standard exit

import java.util.*;

public class UserErrors{
  public static Scanner console = new Scanner(System.in);
  public static void main(String[]args){
    String s  = "Enter a integer between 1-10: ";
     get(s);
  }

  public static int get(String prompt){    
    boolean b = false;
    int number;
    while(!b){
      System.out.print(prompt);
      if(!console.hasNextInt()){
        console.next();
        System.out.println("Invalid data type");
        System.out.print(prompt);
        console.nextInt();
      }else {
        number = console.nextInt();
        if(nb<1 || nb>10){
            System.out.println("not in range");
        }else{
           b=true;
        }
      }
    }
    return nb;
  }
}

Upvotes: 0

Arjun Lajpal
Arjun Lajpal

Reputation: 308

Each nextInt call is a blocking call and it waits for user input. For your code when you write below line:

    if(console.nextInt() < 10 && console.nextInt() >1)

Essentially the console waits for first user input, checks it against 10 and then waits for next input which could be any (which you type second), not necessarily the same, waits for that and then finally enters the if condition.

The console input should always be taken upfront and assigned to our local variable and then it needs to be checked for your conditions like below:

    int userInput = console.nextInt();

then the checking goes below with the userInput variable:

    if(userInput < 10 && userInput >1)

Upvotes: 0

Wojciech Tomczyk
Wojciech Tomczyk

Reputation: 96

There's an error:

if(console.nextInt() > 10 || console.nextInt() <1){ ... }

change this line to:

int i = console.nextInt();
if(i > 10 || i <1){ ... }

You can not reuse console.next...() just like that :)

Upvotes: 2

Related Questions