Reputation: 77
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.
I tried input.next()
under each nextLine()
because if I don't, never let me complete date field.
This is what happen if only use nextLine()
for each entry
Upvotes: 1
Views: 71
Reputation: 892
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
}
Upvotes: 1