Trouble with order of reading next line with Scanner

I am doing some basic java program using 'Scanner'. I read Integer, Double and String. I have some issues with using scanner for String with other scanners like int and double. Declaration part: Scanner scan = new Scanner(System.in); int i2; double d2; String s2; Order#1: i2 = scan.nextInt(); d2 = scan.nextDouble(); s2 = scan.nextLine(); Result:

扫描仪读取下一行命令的麻烦

我正在使用'扫描仪'来做一些基本的Java程序。 我读了Integer,Double和String。 我有一些使用扫描仪与其他扫描仪像int和双重字符串的问题。 声明部分: Scanner scan = new Scanner(System.in); int i2; double d2; String s2; 为了#1: i2 = scan.nextInt(); d2 = scan.nextDouble(); s2 = scan.nextLine(); 结果:编译器等待输入i2和d2,但不等待输入s2。 它在s2 = scan

Scanner nextLine Java error

I want a program in java to accept 1.Integer 2.Double 3.String respectively . and display the above . But the problem I m facing is after I enter integer and double its not prompting me to enter String . Its directly display the values . As i searched the soln to this , I got to know that scan.nextLine(); should be used after I accept the double value . can anyone tell me why shou

扫描仪nextLine Java错误

我想用java中的程序来接受 1.Integer 2,双 3.String分别。 并显示上面的内容。 但我面临的问题是在我输入整数后加倍而不提示我输入String。 它直接显示值。 当我搜索到这个soln时,我已经知道了 scan.nextLine(); 应该在我接受double值后使用。 谁能告诉我为什么要用这条线。 我阅读nextline的文档。 但我不明白。 import java.util.Scanner; public class Solution { public static void main(String[

Java Scanner doesn't wait for user input

This question already has an answer here: Scanner is skipping nextLine() after using next() or nextFoo()? 15 answers It's possible that you are calling a method like nextInt() before. Thus a program like this: Scanner scanner = new Scanner(System.in); int pos = scanner.nextInt(); System.out.print("X: "); String x = scanner.nextLine(); System.out.print("Y: "); String y = scanner.nextLin

Java扫描器不会等待用户输入

这个问题在这里已经有了答案: 扫描仪使用next()或nextFoo()后跳过nextLine()? 15个答案 您可能以前调用过像nextInt()这样的方法。 因此一个这样的程序: Scanner scanner = new Scanner(System.in); int pos = scanner.nextInt(); System.out.print("X: "); String x = scanner.nextLine(); System.out.print("Y: "); String y = scanner.nextLine(); 展示你所看到的行为。 问题是nextInt()不会消耗'n'

Both next() and nextLine() not helping to store name with spacing

I am currently using a Scanner to record the user input which is a String and print it out. If the user input is a single name such as Alan, it works fine. If I enter a name with spacing such as Alan Smith, it returns an error saying InputMisMatchException. I read around similar cases here and they advised to use nextLine() instead of next(). It made sense but that doesn't work for me ei

next()和nextLine()都不能以间距存储名称

我正在使用扫描仪来记录用户输入的字符串并将其打印出来。 如果用户输入是一个单独的名称,如Alan,它可以正常工作。 如果我输入带有空格的名称(如Alan Smith),则会返回一个错误,指示InputMisMatchException。 我在这里阅读了类似的案例,他们建议使用nextLine()而不是next()。 它有道理,但这对我也不起作用。 当我使用nextLine()时,它立即跳过我输入名称的步骤,并返回到循环的开始,要求我再次输入选择。

Issues with nextLine();

Possible Duplicate: Scanner issue when using nextLine after nextInt I am trying create a program where it lets the user inputs values into an array using scanner. However, when the program asks for the student's next of kin, it doesn't let the user to input anything and straight away ends the program. Below is the code that I have done: if(index!=-1) { Function.print(

nextLine问题();

可能重复: nextInt后使用nextLine时扫描仪出现问题 我正在尝试创建一个程序,让用户使用扫描仪将值输入到数组中。 但是,当程序要求学生的最近亲属时,它不会让用户输入任何内容并立即结束程序。 以下是我所做的代码: if(index!=-1) { Function.print("Enter full name: "); stdName = input.nextLine(); Function.print("Enter student no.: "); stdNo = input.nextLine();

Weird behaviour with Scanner#nextFloat

Running the following in Eclipse initially caused Scanner to not recognize carriage returns in the console effectively blocking further input: price = sc.nextFloat(); Adding this line before the code causes Scanner to accept 0,23 (french notation) as a float: Locale.setDefault(Locale.US); This is most probably due to regional settings in Windows XP Pro (French/Belgian). When the code is run

Scanner#nextFloat的怪异行为

在Eclipse中运行以下内容最初会导致Scanner无法识别控制台中的回车,从而有效阻止进一步的输入: price = sc.nextFloat(); 在代码之前添加此行使Scanner接受0,23(法语符号)作为浮点数: Locale.setDefault(Locale.US); 这很可能是由于Windows XP Pro(法国/比利时)的区域设置。 当代码再次运行时,仍然接受0,23,输入0.23会导致它抛出java.util.InputMismatchException 。 任何解释为什么会发生这种情况? 也有解决方

How to use Scanner methods when scanning a String?

I have a text file that consists of a few lines of a small list of integers separated by spaces. I was to scan the first line, and do something for each integer that is in the first line. String lineString = textScan.nextLine(); //scans the first line of the text file. Scanner Line = new Scanner(lineString); //scans the String retrieved from the text file. while (Line.hasNextInt())

扫描字符串时如何使用扫描仪方法?

我有一个文本文件,它由几行空格分隔的小整数列组成。 我要扫描第一行,并为第一行中的每个整数执行一些操作。 String lineString = textScan.nextLine(); //scans the first line of the text file. Scanner Line = new Scanner(lineString); //scans the String retrieved from the text file. while (Line.hasNextInt()) { //do stuff } 所以我有一个扫描仪(textScan)扫描文本文件的第一行,然后

Scanner's nextLine(), Only fetching partial

So, using something like: for (int i = 0; i < files.length; i++) { if (!files[i].isDirectory() && files[i].canRead()) { try { Scanner scan = new Scanner(files[i]); System.out.println("Generating Categories for " + files[i].toPath()); while (scan.hasNextLine()) { count++;

扫描仪的nextLine(),仅获取部分

所以,使用像这样的东西: for (int i = 0; i < files.length; i++) { if (!files[i].isDirectory() && files[i].canRead()) { try { Scanner scan = new Scanner(files[i]); System.out.println("Generating Categories for " + files[i].toPath()); while (scan.hasNextLine()) { count++;

Java: Scanner stopping at new line

I am trying to scan through text files and add them to a map, the map and everything is working. However, the scanner seems to be stopping when it comes to a 'enter' in the text file, or a blank line. This is my problem here is my block of code for the scanner/mapper class OneButtonListener implements ActionListener { @Override public void actionPerformed(ActionEvent evt)

Java:扫描仪停在新线上

我正在尝试扫描文本文件并将它们添加到地图,地图和一切正在工作。 但是,当涉及到文本文件中的“输入”时,扫描器似乎停止工作,或者空白行。 这是我的问题 这里是我的扫描仪/映射器的代码块 class OneButtonListener implements ActionListener { @Override public void actionPerformed(ActionEvent evt) { final JFileChooser oneFC = new JFileChooser(); oneFC.showOpenDialog(AnalysisFram

How to remove line breaks from a file in Java?

How can I replace all line breaks from a string in Java in such a way that will work on Windows and Linux (ie no OS specific problems of carriage return/line feed/new line etc.)? I've tried (note readFileAsString is a function that reads a text file into a String): String text = readFileAsString("textfile.txt"); text.replace("n", ""); but this doesn't seem to work. How can this be d

如何从Java文件中删除换行符?

我该如何从Java中的字符串中替换所有换行符,以便在Windows和Linux上运行(即没有OS特定的回车/换行/换行等问题)? 我试过了(注意readFileAsString是一个将文本文件读入字符串的函数): String text = readFileAsString("textfile.txt"); text.replace("n", ""); 但这似乎并不奏效。 如何才能做到这一点? 您需要将text设置为text.replace()的结果: String text = readFileAsString("textfile.txt"); text = text.re