如何在Java中跳过第一个int将文件逐行读取到数组?
我试图用Java读取一个文件并将其存储到数组中。现在的问题是该文件具有以下结构。我将在下面给出两个示例文件: 输入文件结构如何在Java中跳过第一个int将文件逐行读取到数组?,java,arrays,Java,Arrays,我试图用Java读取一个文件并将其存储到数组中。现在的问题是该文件具有以下结构。我将在下面给出两个示例文件: 输入文件结构 <number of lines> <line number> <value> . . . <line number> <value> input2.txt 5 1 34 2 19 3 43 4 62 5 36 4 1 10.3430423 2 -34.234923 3 -100.39292 4 22 如您
<number of lines>
<line number> <value>
.
.
.
<line number> <value>
input2.txt
5
1 34
2 19
3 43
4 62
5 36
4
1 10.3430423
2 -34.234923
3 -100.39292
4 22
如您所见,文件以行数开始(例如4或5)。在我的正常输入文本中,有超过100000行
所以我的代码基本上获取用户输入,打开文件,创建一个数组大小和一个该大小的数组。现在我被困在阅读下一行并将值添加到我的元素数组中。行号不应添加到数组中。现在您可以看到,我的元素数组被声明为字符串。是否可以实际读取文件、获取值的类型并创建该类型的数组?我认为它可以节省从字符串转换为int、double或floating
下面是我的代码:
public static void main(String args[]) throws NumberFormatException, IOException{
String inFile; //Input file name.
int filterSize; //Filter size (odd integer >= 3).
String outFile; //Output file name.
int arraySize;
String[] elements;
int index = 0;
//Scanner to take input file name, filter size and output file name.
Scanner keyboardInput = new Scanner(System.in);
System.out.println("Enter your keyboard input as follows: <data file name> <filter size(odd int >= 3> <output file name>");
//Assigning values to variables.
inFile = keyboardInput.next();
filterSize = keyboardInput.nextInt();
outFile = keyboardInput.next();
//Reading file into array using BufferReader
BufferedReader fileInput = new BufferedReader(new FileReader(inFile));
arraySize = Integer.parseInt(fileInput.readLine()); //Get Array Size
elements = new String[arraySize];
while(fileInput.readLine() != null){
elements[index] = fileInput.readLine();
index += 1;
}
}
publicstaticvoidmain(字符串args[])抛出NumberFormatException、IOException{
字符串填充;//输入文件名。
int filterSize;//过滤器大小(奇数整数>=3)。
字符串输出文件;//输出文件名。
内部阵列化;
字符串[]元素;
int指数=0;
//扫描仪获取输入文件名、过滤器大小和输出文件名。
扫描仪键盘输入=新扫描仪(System.in);
System.out.println(“按如下方式输入键盘输入:=3>”;
//为变量赋值。
infle=keyboardInput.next();
filterSize=keyboardInput.nextInt();
outFile=keyboardInput.next();
//使用BufferReader将文件读入数组
BufferedReader fileInput=新的BufferedReader(新文件读取器(infle));
arraySize=Integer.parseInt(fileInput.readLine());//获取数组大小
元素=新字符串[arraySize];
while(fileInput.readLine()!=null){
元素[index]=fileInput.readLine();
指数+=1;
}
}
感谢您的帮助。谢谢
读这个号码,不要用它
File File=new文件(“input1.txt”);
扫描仪输入=新扫描仪(文件);
int lines=in.nextInt();
int[]a=新的int[行];
对于(int i=0;i
您还可以在#hasNextLine()中使用
读这个号码,不要用它
File File=new文件(“input1.txt”);
扫描仪输入=新扫描仪(文件);
int lines=in.nextInt();
int[]a=新的int[行];
对于(int i=0;i
您还可以在#hasNextLine()中使用
读这个号码,不要用它
File File=new文件(“input1.txt”);
扫描仪输入=新扫描仪(文件);
int lines=in.nextInt();
int[]a=新的int[行];
对于(int i=0;i
您还可以在#hasNextLine()中使用
读这个号码,不要用它
File File=new文件(“input1.txt”);
扫描仪输入=新扫描仪(文件);
int lines=in.nextInt();
int[]a=新的int[行];
对于(int i=0;i
您也可以在#hasNextLine()中使用。尝试这样做:
Scanner sc = new Scanner(new File(inFile));
arraySize = sc.nextInt();
elements = new String[arraySize];
while(sc.hasNext())
{
sc.nextInt();
elements[index] = sc.next();
index += 1;
}
您可以创建新的扫描仪,然后读取整数、布尔值等,而无需进行任何转换。因为你们不需要一行当前的数字,你们只要读那个数字就可以了。你不需要把它保存在任何地方。然后是必须保存在elements[index]
的下一个数字/字符串。就这样试着这样做:
Scanner sc = new Scanner(new File(inFile));
arraySize = sc.nextInt();
elements = new String[arraySize];
while(sc.hasNext())
{
sc.nextInt();
elements[index] = sc.next();
index += 1;
}
您可以创建新的扫描仪,然后读取整数、布尔值等,而无需进行任何转换。因为你们不需要一行当前的数字,你们只要读那个数字就可以了。你不需要把它保存在任何地方。然后是必须保存在elements[index]
的下一个数字/字符串。就这样试着这样做:
Scanner sc = new Scanner(new File(inFile));
arraySize = sc.nextInt();
elements = new String[arraySize];
while(sc.hasNext())
{
sc.nextInt();
elements[index] = sc.next();
index += 1;
}
您可以创建新的扫描仪,然后读取整数、布尔值等,而无需进行任何转换。因为你们不需要一行当前的数字,你们只要读那个数字就可以了。你不需要把它保存在任何地方。然后是必须保存在elements[index]
的下一个数字/字符串。就这样试着这样做:
Scanner sc = new Scanner(new File(inFile));
arraySize = sc.nextInt();
elements = new String[arraySize];
while(sc.hasNext())
{
sc.nextInt();
elements[index] = sc.next();
index += 1;
}
您可以创建新的扫描仪,然后读取整数、布尔值等,而无需进行任何转换。因为你们不需要一行当前的数字,你们只要读那个数字就可以了。你不需要把它保存在任何地方。然后是必须保存在elements[index]
的下一个数字/字符串。就是这样这里真正的问题是如何去掉行号,并获得除此之外的值
double[] array = .... // initialize your array
int v = 0;
while(file.readLine() != null){
// your array
array[v++] = Double.parseDouble(file.readLine().split(" ")[1]);
}
这里真正的问题是如何去掉行号,并获得除此之外的值
double[] array = .... // initialize your array
int v = 0;
while(file.readLine() != null){
// your array
array[v++] = Double.parseDouble(file.readLine().split(" ")[1]);
}
这里真正的问题是如何去掉行号,并获得除此之外的值
double[] array = .... // initialize your array
int v = 0;
while(file.readLine() != null){
// your array
array[v++] = Double.parseDouble(file.readLine().split(" ")[1]);
}
这里真正的问题是如何去掉行号,并获得除此之外的值
double[] array = .... // initialize your array
int v = 0;
while(file.readLine() != null){
// your array
array[v++] = Double.parseDouble(file.readLine().split(" ")[1]);
}
以流为基础:
Files.lines(pathToFile).skip(1) // skip the line counter. Don't trust input
.map(line -> line.replaceFirst("\\d+", "")) // remove digits at the start
.collect(Collectors.toList()); // collect it into a list
您可以使用.toArray()
将其存储到数组中
但实际上,您应该通过尝试使用资源来实现这一点:
try (Stream<String> lines = Files.lines(pathToFile).skip(1)) {
elements = lines.map(line -> line.replaceFirst("\\d", "")).collect(Collectors.toList());
} catch (IOException e) {
// sensible handling...
}
try(流行=文件。行(pathToFile)。跳过(1)){
elements=lines.map(line->line.replaceFirst(“\\d”和“”).collect(Collectors.toList());
}捕获(IOE异常){
//明智的处理。。。
}
以流为基础:
Files.lines(pathToFile).skip(1) // skip the line counter. Don't trust input
.map(line -> line.replaceFirst("\\d+", "")) // remove digits at the start
.collect(Collectors.toList()); // collect it into a list
您可以使用.toArray()
将其存储到数组中
但实际上,您应该通过尝试使用资源来实现这一点:
try (Stream<String> lines = Files.lines(pathToFile).skip(1)) {
elements = lines.map(line -> line.replaceFirst("\\d", "")).collect(Collectors.toList());
} catch (IOException e) {
// sensible handling...
}
try(流行=文件。行(pathToFile)。跳过(1)){
元素=