1

Code:

public void addTest(int idUser) throws IOException {

    String date = null;
    String tec = null;

    System.out.println("Enter name for test file :");
    String file = input.next(); //Name of file

    System.out.println("Enter date formatted as dd/mm/yyyy hh:mm :");
    date = input.nextLine(); //String 2 parts
    input.next();

    System.out.println("Enter technician name :");
    tec = input.nextLine(); // String 2+ parts
    input.next();

    String path = "C:\\Test\\Sample\\" + file;
    String chain = readFile(path);

    ClinicalTest test = new ClinicalTest(chain, date, idUser, tec);
    System.out.println(test.getDate()+"\n" + test.getTec());

    createTest(test);
}

When enter date 12-12-2018 13:45 and tec name Mark Zus, trying to create test fails. sysout only shows 13:45.

enter image description here

I tried input.next() under each nextLine() because if I don't, never let me complete date field.

enter image description here

This is what happen if only use nextLine() for each entry

0

1 Answer 1

1

I suggest you to read JavaDoc which is helpful in using methods. As it is written above the nextLine() method:

This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.

It means that by using next() method you are reading the first part of your input and then when you use nextLine() it captures the rest of the line which is 13:45 in your input sample.

So you don't need input.next(). The following code works perfectly:

public static void main(String[] args){
    Scanner input = new Scanner(System.in);

    String date = null;
    String tec = null;

    System.out.println("Enter name for test file :");
    String file = input.nextLine();

    System.out.println("Enter date formatted as dd/mm/yyyy hh:mm :");
    date = input.nextLine(); //String 2 parts

    System.out.println("Enter technician name :");
    tec = input.nextLine(); // String 2+ parts
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, that was my first guess. I add the screen for what happen when I do that.
EDIT: Just create a local Scanner for that method and works great. May be some issue with global scanner. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.