Java 如何读取txt文件中的所有双精度?
有一个包含所有替身的档案很容易,但是 在两者之间有一个非双精度, 我不可能抓住所有的人 例如:Java 如何读取txt文件中的所有双精度?,java,io,java.util.scanner,Java,Io,Java.util.scanner,有一个包含所有替身的档案很容易,但是 在两者之间有一个非双精度, 我不可能抓住所有的人 例如: 604.2 609.2 6042 604.4 4234.324 312 gfsdgfreg 6043 604.3 604.2 609.2 6042.0 604.4 4234.324 312.0 输出: 604.2 609.2 6042 604.4 4234.324 312 gfsdgfreg 6043 604.3 604.2 609.2 6042.0 604.4 4234.324 312.0
604.2
609.2
6042
604.4
4234.324
312
gfsdgfreg
6043
604.3
604.2
609.2
6042.0
604.4
4234.324
312.0
输出:
604.2
609.2
6042
604.4
4234.324
312
gfsdgfreg
6043
604.3
604.2
609.2
6042.0
604.4
4234.324
312.0
显然,两个双打不见了。有办法吗
要通过使用hasNextDouble()捕获所有这些内容?
如果你没有得到答复,请提前谢谢。我在某处看到
我可以分析每一个,使其加倍并捕捉
例外,但我真的没有那么先进
我这里有:
import java.util.*;
import java.io.*;
public class Lab11{
public static void main(String[] args)
throws FileNotFoundException{
File nums = new File("file.txt");
int size = arrSize(nums);
double[] phoneNums = copy(nums,size);
for(int i=0;i<phoneNums.length;i++)
System.out.println(phoneNums[i]);
}
public static int arrSize(File f)
throws FileNotFoundException{
int arrSize = 0;
Scanner in = new Scanner(f);
while(in.hasNextDouble()){
arrSize++;
in.next();
}
in.close();
return arrSize;
}
public static double[] copy(File f,int size)
throws FileNotFoundException{
Scanner in = new Scanner(f);
double[] list = new double[size];
int i = 0;
while(in.hasNextDouble()){
list[i++] = in.nextDouble();
}
in.close();
return list;
}
}
import java.util.*;
导入java.io.*;
公共类Lab11{
公共静态void main(字符串[]args)
抛出FileNotFoundException{
File nums=新文件(“File.txt”);
int size=arrSize(nums);
double[]phoneNums=复制(nums,大小);
对于(int i=0;i,因为您正在使用
while(in.hasNextDouble()){
arrSize++;
in.next();
}
一旦达到非双精度,它将停止,并且不会继续。
你必须在循环中的每一行中循环,在这个循环中,使用if
语句来检查你所读的内容是否是双精度的。因为你正在使用
while(in.hasNextDouble()){
arrSize++;
in.next();
}
一旦达到非双精度,它将停止,并且不会继续。
您必须在while
循环中的每一行中不断循环,在该循环中,使用if
语句检查您所读取的内容是否为双精度。我将为您的两个while循环提供以下结构:
while(in.hasNext()) {
if(in.hasNextDouble()) {
// your inner while loop code here
} else {
in.next();
}
}
否则,在非双精度循环的第一个实例之后,您将错过所有内容。我将为您的两个while循环提供以下结构:
while(in.hasNext()) {
if(in.hasNextDouble()) {
// your inner while loop code here
} else {
in.next();
}
}
否则,在第一个非double实例之后,您将错过所有内容。正如Takendark所说,一旦在中的输入中找到非double,hasNextDouble()将计算为false,从而结束循环
下面是一个(希望)更简化的方法的示例:
// create a new list for our doubles
List<Double> doubles = new LinkedList<>();
try {
// open our doubles file reader
BufferedReader reader = Files.newBufferedReader(Paths.get("doubles.txt"), Charset.defaultCharset());
// read our doubles file
String line;
while ((line = reader.readLine()) != null) {
if (line.matches("^[0-9]*(\\.[0-9]+)?$")) {
doubles.add(Double.parseDouble(line));
}
}
// close our doubles file reader
reader.close();
} catch (IOException e) {
e.printStackTrace(); // for the sake of the example
}
// output our doubles
for (Double d : doubles) {
System.out.println("Double: " + d);
}
//为我们的替身创建一个新列表
List double=new LinkedList();
试一试{
//打开我们的Double文件读取器
BufferedReader reader=Files.newBufferedReader(path.get(“doubles.txt”)、Charset.defaultCharset());
//阅读我们的双打档案
弦线;
而((line=reader.readLine())!=null){
if(行匹配(“^[0-9]*(\\.[0-9]+)?$”){
doubles.add(Double.parseDouble(line));
}
}
//关闭我们的Double文件读取器
reader.close();
}捕获(IOE异常){
e、 printStackTrace();//为了示例
}
//输出我们的双打
双人(d:双人){
System.out.println(“双精度:+d”);
}
希望这有助于就像Takendarkk说的那样,一旦在.hasNextDouble()中的输入中发现非双精度,它的计算结果将为false,从而结束循环
下面是一个(希望)更简化的方法的示例:
// create a new list for our doubles
List<Double> doubles = new LinkedList<>();
try {
// open our doubles file reader
BufferedReader reader = Files.newBufferedReader(Paths.get("doubles.txt"), Charset.defaultCharset());
// read our doubles file
String line;
while ((line = reader.readLine()) != null) {
if (line.matches("^[0-9]*(\\.[0-9]+)?$")) {
doubles.add(Double.parseDouble(line));
}
}
// close our doubles file reader
reader.close();
} catch (IOException e) {
e.printStackTrace(); // for the sake of the example
}
// output our doubles
for (Double d : doubles) {
System.out.println("Double: " + d);
}
//为我们的替身创建一个新列表
List double=new LinkedList();
试一试{
//打开我们的Double文件读取器
BufferedReader reader=Files.newBufferedReader(path.get(“doubles.txt”)、Charset.defaultCharset());
//阅读我们的双打档案
弦线;
而((line=reader.readLine())!=null){
if(行匹配(“^[0-9]*(\\.[0-9]+)?$”){
doubles.add(Double.parseDouble(line));
}
}
//关闭我们的Double文件读取器
reader.close();
}捕获(IOE异常){
e、 printStackTrace();//为了示例
}
//输出我们的双打
双人(d:双人){
System.out.println(“双精度:+d”);
}
希望这对.next()中的Else有帮助,否则它将永远循环。@ElliottFrisch,你完全正确。我将修正我的答案。Else在.next()中;否则它将永远循环。@ElliottFrisch,你完全正确。我会修正我的答案。你能解释一下你在行中使用的格式符号吗?匹配的?你能解释一下你在行中使用的格式符号吗?