D
D
dvlprjv152015-11-24 21:30:46
Java
dvlprjv15, 2015-11-24 21:30:46

How does the nextLine() method work in the Scanner class?

Wrote a little program:

import java.util.*;

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

// объявлена константа сколько сантиметров в дюйме
    final double CM_PER_INCH = 2.54;
// объявлены переменные: ширина листа, высота листа, исполнитель
    double paperWidth;
    double paperHeigth;
    String name;
// получить от пользователя значения ширины и высоты листа в дюймах
    System.out.print("Введите ширину листа в дюймах: ");
    paperWidth = ott.nextDouble();
    System.out.print("Введите высоту листа в дюймах: ");
    paperHeigth = ott.nextDouble();
// получить от пользователя его фамилию
    System.out.print("Введите свою фамилию: ");
    name = ott.nextLine();

// программа выводит сообщение о размерах листа и фамилию исполнителя
    System.out.println("Размеры листа в сантиметрах: " 
+ paperWidth * CM_PER_INCH + " на " + paperHeigth * CM_PER_INCH
+ " сантиметров");
    System.out.println("Данные вводил инженер: " + name);
  }
}

Compiled. Started.
The program asked to enter the width.
Enter the width.
The program asked me to enter a length.
Enter the length.
But then the program should have asked to enter a surname, but it did not stop. And immediately she displayed a message about the size of the sheet and about the engineer, but without a surname, since the program did not provide a chance to enter it.
For the second day I have been reading the description of the nextLine () method in English. But I can't understand why nextLine() doesn't work after using nextDouble(). More precisely, it works, but in a way that I don't understand.
Because if you put lines with
System.out.print("Введите свою фамилию: ");
    name = ott.nextLine();

before lines with nextDouble(), the program will stop three times as intended.
If one of the bison has the time and desire to tell the newcomer "on matches" why this happens, I will be very grateful. Or share, please, a link to the Russian translation of the description of the nextLine () method.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
MiiNiPaa, 2015-11-24
@dvlprjv15

nextLine() reads to the end of the current line (newline or end-of-stream) and returns whatever was on that line.
nextDouble() reads double. It doesn't read or skip anything AFTER the double.
So you enter something like "3.15\n", where \n is the newline character.
nextDouble() extracts 3.14 and leaves "\n" in the buffer.
nextLine() encounters \n and exits after reading 0 characters - an empty line.
Check: type "1 2 Ivanov" in one line and it will work (True, the space before Ivanov will be read as part of the last name)
It's fashionable to call nextLine() to absorb this line break and wait for a newline.

D
DiMelnitsa, 2017-07-26
@DiMelnitsa

Faced a similar problem.
Instead of nextLine() I used next().
Earned normally.

T
tatirybina, 2017-10-02
@tatirybina

Scanner methods are well described here in this article https://vertex-academy.com/tutorials/ru/rabota-so-...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question