我正在做一个简单的任务。我正在为变量分配字符串,整数和双精度型。通过扫描仪读取文本文件来分配变量。扫描仪无法读取双精度。它将引发输入不匹配异常。我已经研究了堆栈溢出的答案,并尝试了以下解决方案:
文本文件的内容如下:
鹅卵石lin石\ n
1 2.2 \ n
这是一行文字\ n
这是我的代码:
public static void main(String[] args) throws FileNotFoundException {
File file = new File("input.txt");
Scanner scanner = new Scanner(file);
String s1 = scanner.next(); // s1 is assigned to Pebbles
String s2 = scanner.next(); // s2 is assigned to Flintstone
int x = scanner.nextInt(); // x is assigned to 1
double y = scanner.nextDouble(); // y is assigned to 2.2
scanner.nextLine(); // Advance scanner to beginning of next line
String s3 = scanner.nextLine();
scanner.close(); // s3 is assigned to "This is a line of text"
System.out.print(y);
}
我如何使扫描仪读取2.2的双份?更改语言环境无效。将小数点更改为逗号也不起作用。