2

I'm creating a program for making shortcuts. Basically what it does is: I'm providing the number of words i want to process. Let's say I enter 3 in this case. The next step is to get those words from user by Scanner. Seems easy, right? What I've done? I put Scanner inside the loop (it should put the word inside ArrayList), but the loop is skipping the 0 without asking user for word. I think it's a problem with Scanner, cause when I put instead of scanner something like System.out.println the loop is working perfectly.

I already tried to make other function to ask user for word and add this element to list and in the same loop I'm just reaching this function. But it doesn't help. Also i tried moving the "i++" behind the data.add but doesn't work as well.

public class Main {

    public static void main(String[] args) {
        ArrayList<String> data = new ArrayList<>();
        UserInteraction userInteraction = new UserInteraction();
        userInteraction.enterData(data);
        for(String x: data)
            System.out.println(x);
    }
}

public class UserInteraction {
    private Scanner scanner = new Scanner(System.in);
    private int getData() throws InputMismatchException {
        try{
            int number = scanner.nextInt();
            return number;
        }catch (InputMismatchException e){
            System.out.println("Not a number. Try again:");
            return getData();
        }
    }



    void enterData(ArrayList<String> data){
        System.out.println("Enter the number of words:");
        int quantity = getData();
        System.out.println("Now, enter the words. Every word must be separated by clicking enter:");
        for(int i=0; i<quantity;i++){
            System.out.println(i);
            data.add(scanner.nextLine());

        }
    }

}

I'm getting this output:

Enter the number of words:
4
Now, enter the words. Every word must be seperated by clicking enter:
0
1

You can see that between 0 the Scanner is not reached for some reason. But after 1, 2 and 3 it work. Yes, I can just edit the loop changing i

  • Please correct your formatting! – Maxim Khanov Jun 17 '19 at 12:43
  • Someone is faster than me. It's my first post, i've saw it's wrong formated and in 1 second i went do edit but it was already edited. Sorry for that – Adrian Stypiński Jun 17 '19 at 12:44
  • 3
    I believe that getData() uses a Scanner object and a nextInt(), right? – forpas Jun 17 '19 at 12:44
  • 3
    Please read "How to create a [mcve]". Then use the [edit] link to improve your question (do not add more information via comments). Otherwise we are not able to answer your question and help you. – GhostCat Jun 17 '19 at 12:44
  • 7
    Please do **not** explain what *some* of your code is doing. You **assume** that this is what happens. But if all your assumptions about your code would be correct, you wouldnt be here wondering "where is the bug?"! Thus: please step back, and take the time to identify the *core* elements of your problem, then show us a **minimal** example that shows that problem. There is even a high chance that while you do that work, you will identify the cause of your problem yourself. But please dont come here "here is half of my code, the other half, I am not showing, but I think it does this and that... – GhostCat Jun 17 '19 at 12:46
  • getData() is using Scanner and nextInt(). I didn't just use nextInt() because i want to avoid InputMismatchException when user will enter a letter. I can't use this function to read letters beacuse it will return information "It's not a number, try again" – Adrian Stypiński Jun 17 '19 at 12:48
  • Okay, now i added the whole code (ofc, without imports) – Adrian Stypiński Jun 17 '19 at 12:51
  • Idk why but i need to add here the last sentence. "I can just edit the loop changing i – Adrian Stypiński Jun 17 '19 at 12:56

0 Answers0