迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Java >

修复 Java Scanner NextLine Skips 错误

作者:迹忆客 最近更新:2023/11/13 浏览次数:

在 Java 中的 next()nextInt() 方法之后使用 nextLine() 方法时,有时 nextLine() 方法会跳过要读取的当前输入并转到下一个输入。本文将讨论为什么会发生这种情况以及如何解决此问题。

在我们研究 nextLine() 方法的问题之前,让我们首先了解 nextLine() 方法的工作原理。此方法属于 java.util.Scanner 类。

此函数打印整行,除了末尾的行分隔符。它搜索输入以寻找行分隔符。如果没有找到行分隔符,它可能会在搜索要跳过的行的同时搜索所有输入。

此 Scanner 方法越过当前行。如果有任何输入被跳过,它会返回它。

语法:

public String nextLine()

请注意,此函数不接受任何参数并返回跳过的行。看看下面的例子,看看它是如何工作的。

// To demonstrate the working of nextLine() method

import java.util.*;

public class Example {
  public static void main(String[] args) {
    String a = "Java \n is \n intriguing";

    // create a new scanner
    Scanner sc = new Scanner(a);

    // print the next line
    System.out.println(sc.nextLine());

    // print the next line again
    System.out.println(sc.nextLine());

    // print the next line again
    System.out.println(sc.nextLine());
  }
}

输出:

Java
 is
 intriguing

Java 中 nextLine() 方法的主要用途是读取带有空格的字符串输入。

请注意,我们也可以使用 next() 方法输入一个字符串,但它只读取它遇到的第一个空格。此外,next() 方法在输入后将光标放在同一行。

然而,nextLine() 方法在读取输入后将光标置于下一行。看下面的例子。

import java.util.*;
public class Demo {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    // Try reading a string with space using next()
    System.out.println("Enter the name of a movie");
    String name = sc.next();

    // Done to bring the cursor to next line
    sc.nextLine();

    // Re-reading the string using nextLine()
    System.out.println("Re-enter this name");
    String rename = sc.nextLine();

    System.out.println();
    System.out.println("Here is the output:");

    // printing the output
    System.out.println(name);
    System.out.println(rename);
  }
}

输出:

Enter the name of a movie
Harry Potter
Re-enter this name
Harry Potter

Here is the output:
Harry
Harry Potter

请注意,next() 方法仅读取空格前的名字并忽略其余部分。然而,nextLine() 方法读取带有空格的整个字符串。

nextLine() 方法的问题

要了解此问题,请查看以下示例。

import java.util.Scanner;
public class Demo {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    // This part takes the input
    System.out.println("Input the Roll Number:");
    int number = sc.nextInt();
    System.out.println("Input your full name:");
    String name = sc.nextLine();
    System.out.println("Input your branch:");
    String branch = sc.nextLine();

    // This part prints the values
    System.out.println("Roll No: " + number);
    System.out.println("Name: " + name);
    System.out.println("Branch: " + branch);
  }
}

输出:

Input the Roll Number:
1
Input your full name:
Input your branch:
Computer Science
Roll No: 1
Name:
Branch: Computer Science

但所需的输出是这样的:

Input the Roll Number : 1 Input your full name : Harry Potter Input your branch
    : Computer Science Roll No : 1 Name : Harry Potter Branch : Computer Science

请注意,nextLine() 方法跳过 Name 的输入,而是转到分支的下一个输入。

为什么会出现这个问题

这是因为当 Scanner 类的 nextInt() 方法读取卷号时,它返回值 1。该值存储在变量编号中。

但是读取卷号后光标不会转到下一行。它在 1 之后仍然存在。像这样:

1_ // the cursor remains here only

因此,当我们使用 nextLine() 方法读取名称时,它会从光标的当前位置开始读取输入。所以在 1 之后,下一行就是新行本身。 \n 字符表示这个新行。因此,名称只是\n

如何解决 Java Scanner NextLine 跳过的问题

有两种方法可以解决此问题。

parseInt() 方法属于 java.lang 包的 Integer 类,返回某个字符串的原始数据类型。这些方法主要有两种:

  • Integer.parseInt(String s)
  • Integer.parseInt(String s, int radix)

要使用 nextLine() 跳过,我们将使用第一种方法 - Integer.parseInt(String s)。这会将整数的完整行读取为字符串。稍后,它将其转换为整数。

语法:

int value = Integer.parseInt(sc.nextLine());

让我们使用 Java Integer.parseInt() 方法实现我们上面使用的相同代码并纠正问题。

import java.util.Scanner;
public class Demo {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    // This part takes the input
    System.out.println("Input the Roll Number:");

    // Using the parseInt() method this time
    int number = Integer.parseInt(sc.nextLine());
    System.out.println("Input your full name:");
    String name = sc.nextLine();
    System.out.println("Input your branch:");
    String branch = sc.nextLine();

    // This part prints the values
    System.out.println("Roll No: " + number);
    System.out.println("Name: " + name);
    System.out.println("Branch: " + branch);
  }
}

输出:

Input the Roll Number:
1
Input your full name:
Harry Potter
Input your branch:
Computer Science
Roll No: 1
Name: Harry Potter
Branch: Computer Science

这次代码运行良好。但是我们不能将它用于 Byte character(Byte.parseByte(sc.nextLine())) 之后的输入字符串。

在这种情况下,我们可以使用第二种方法。这是文档的链接以获取更多信息。

这会消耗剩余的新行。看例子。

import java.util.Scanner;
public class Demo {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    // This part takes the input
    System.out.println("Input the Roll Number:");
    int number = sc.nextInt();

    // An extra nextLine() to consume the leftover line
    sc.nextLine();

    System.out.println("Input your full name:");
    String name = sc.nextLine();
    System.out.println("Input your branch:");
    String branch = sc.nextLine();

    // This part prints the values
    System.out.println("Roll No: " + number);
    System.out.println("Name: " + name);
    System.out.println("Branch: " + branch);
  }
}

输出:

Input the Roll Number:
1
Input your full name:
Harry Potter
Input your branch:
Computer Science
Roll No: 1
Name: Harry Potter
Branch: Computer Science

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

如何在 Java 中延迟几秒钟的时间

发布时间:2023/12/17 浏览次数:217 分类:Java

本篇文章主要介绍如何在 Java 中制造程序延迟。本教程介绍了如何在 Java 中制造程序延时,并列举了一些示例代码来了解它。

如何在 Java 中把 Hashmap 转换为 JSON 对象

发布时间:2023/12/17 浏览次数:187 分类:Java

它描述了允许我们将哈希图转换为简单的 JSON 对象的方法。本文介绍了在 Java 中把 Hashmap 转换为 JSON 对象的方法。我们将看到关于创建一个 hashmap,然后将其转换为 JSON 对象的详细例子。

如何在 Java 中按值排序 Map

发布时间:2023/12/17 浏览次数:171 分类:Java

本文介绍了如何在 Java 中按值对 Map 进行排序。本教程介绍了如何在 Java 中按值对 Map 进行排序,并列出了一些示例代码来理解它。

如何在 Java 中打印 HashMap

发布时间:2023/12/17 浏览次数:192 分类:Java

本帖介绍了如何在 Java 中打印 HashMap。本教程介绍了如何在 Java 中打印 HashMap 元素,还列举了一些示例代码来理解这个主题。

在 Java 中更新 Hashmap 的值

发布时间:2023/12/17 浏览次数:146 分类:Java

本文介绍了如何在 Java 中更新 HashMap 中的一个值。本文介绍了如何在 Java 中使用 HashMap 类中包含的两个方法-put() 和 replace() 更新 HashMap 中的值。

Java 中的 hashmap 和 map 之间的区别

发布时间:2023/12/17 浏览次数:79 分类:Java

本文介绍了 Java 中的 hashmap 和 map 接口之间的区别。本教程介绍了 Java 中 Map 和 HashMap 之间的主要区别。在 Java 中,Map 是用于以键值对存储数据的接口,

在 Java 中获取用户主目录

发布时间:2023/12/17 浏览次数:218 分类:Java

这篇文章向你展示了如何在 Java 中获取用户主目录。本教程介绍了如何在 Java 中获取用户主目录,并列出了一些示例代码以指导你完成该主题。

Java 中 size 和 length 的区别

发布时间:2023/12/17 浏览次数:179 分类:Java

这篇文章教你如何知道 Java 中大小和长度之间的区别。本教程介绍了 Java 中大小和长度之间的区别。我们还列出了一些示例代码以帮助你理解该主题。

Java 中的互斥锁

发布时间:2023/12/17 浏览次数:111 分类:Java

了解有关 Java 中互斥锁的一切,在计算机科学领域,互斥或互斥被称为并发控制的属性。每台计算机都使用称为线程的最小程序指令序列。有一次,计算机在一个线程上工作。为了更好地理解,

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便