0
import java.util.InputMismatchException;
import java.util.Scanner;

class ClassB {

    public static void main(String[]args) throws Exception{
        ClassB b = new ClassB();
        b.getInput();
    }

    public void getInput() throws Exception {
        String label = null;
        int input;

        Scanner scan = new Scanner(System.in);
        for (input = 1; input != 4; input++){
            switch(input){
            case 1: label = "name";
            break;
            case 2: label = "password";
            break;
            case 3: label = "Room number";
            break;
            }
            System.out.println("Enter your " + label);
            scan.next();
            try{
                if (input == 1){
                    int name = scan.nextInt();
                    //residence.changeName(name);       
                }
                else if(input == 2){
                    String password = scan.next();
                }
                int rmNumber = scan.nextInt();
            }catch (IllegalArgumentException | InputMismatchException  me ){
                String type = "A string";
                String message = (input == 1 || input == 2) ? type : "An integer";
                input = 1;
            }
        }
    }
}

When I run this code, the first scan.next() in try-catch block does not respond when the enter key of the computer is pressed, and because of this, the subsequent if () statement cannot be entered. The cursor in the text field only breaks line but the input is never accepted. I am using netbean IDE

Pshemo
  • 113,402
  • 22
  • 170
  • 242
  • I think the answer is your *delimiter*. Note that the [`next()`](http://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html#next--) Javadoc says *a complete token is preceded and followed by input that matches the delimiter pattern*. Also, what does your actual `Exception`say? (The one you swallow in your `catch`). – Elliott Frisch Feb 13 '16 at 00:14
  • Not related to the problem at hand, but why this complication with the loop and the encoded indexes and all the ifs? What's wrong with three sequential prompts? The code would be a lot shorter and easier to reason about that way. – Thilo Feb 13 '16 at 00:15
  • The answer for your problem can be found on [this](http://stackoverflow.com/a/23194400/1346996) post. – aribeiro Feb 13 '16 at 00:18
  • @Pshemo question revised with main class –  Feb 13 '16 at 00:26
  • What is the purpose of that `scan.next();` right after `System.out.println("Enter your " + label);`? You are not storing its result anywhere so it only consumes value provided by user. Honestly it is hard to tell what you are trying to achieve here (like asking for name but reading integer is confusing) which makes it hard to help you. – Pshemo Feb 13 '16 at 00:34

2 Answers2

1

http://javatutorialhq.com/java/util/scanner-class-tutorial/

nextInt method does not read the last newline character of your input, and thus that newline is consumed in the next call to nextLine. To resolve this just use next instead of nextline but if you insist of using nextLine add another scan.nextLine() after nextInt. Take a look below snippet

-> Add a scan.nextLine() after scan.nextInt()

JohnRW
  • 676
  • 7
  • 22
  • Thank you, after introducing scan.nextLine(); at the end of loop cycle, it works okay. Plus I have learnt a little more of Scanner on the link. –  Feb 13 '16 at 00:54
0

From your response that you are still seeing it stuck and requiring the user to enter the value multiple times, I'n not sure what the code difference is. Here is my code that I tested with, that does not experience that issue (for me anyway).

import java.util.Scanner;

public class TestInput{
    private void display(String s){
        System.out.println("Please input a valid value: " + s);
    }

    public void getInput(Residence residence){
        String label = null;
        int input;
        Scanner scan = new Scanner(System.in);

        for (input = 1; input != 4; input++){

            switch(input){
                case 1: label = "name";
                break;
                case 2: label = "password";
                break;
                case 3: label = "Room number";
               break;
            }

            System.out.println("Enter your " + label);

            try{
                if (input == 1){
                    residence.changeName(scan.next());       
                }
                else if(input == 2){
                   String password = scan.next();
                   residence.changePassword(password);
                }
                else if(input == 3){
                   int roomNumber = scan.nextInt();
                   residence.changeRoomNumber(roomNumber);
                }
            }
            catch (Exception me ){
                String type = "A string";
                String message = (input == 1 || input == 2) ? type : "An integer";
                display(message);
                input = 1;
            }
        }
   }

   public static void main(String... args){
    TestInput ti = new TestInput();
    ti.getInput(new Residence());
   }
}

class Residence{
    String password;
    String name;
    int room;

    public void changeName(String n){
        this.name = n;
    }
    public void changePassword(String pass){
        this.password = pass;
    }
    public void changeRoomNumber(int n){
        room = n;
    }
}

In your try/catch if/else statement, you did not check for condition 3. The reason you were 'stuck' is that you also had a second call to nextInt(), causing it to wait for additional input even after entering the room number the first time. Replace the if condition with the one below and try it again:

           if (input == 1){
                residence.changeName(scan.next());       
            }
            else if(input == 2){
                String password = scan.next();
                residence.changePassword(password);
            }
            else if(input == 3){
                int roomNumber = scan.nextInt();
                residence.changeRoomNumber(roomNumber);
            }
pczeus
  • 7,195
  • 4
  • 34
  • 50
  • It is not working as expected, On first enter press, it accepts input like after 3 tries –  Feb 13 '16 at 00:45