解析数据结构中不同大小的数组 - Java

发布于 2024-10-19 23:57:34 字数 1014 浏览 3 评论 0原文

作为一名新手 Java 程序员,我在尝试分析个人项目的数据时遇到了相当高的障碍。我有一条文字 该文件包含大约 33.5k 数据点,格式如下:

PointNumber:        33530  
Lat:                8.99167773897820e-001  
Lon:                6.20173660875318e+000  
Alt:                0.00000000000000e+000  
NumberOfAccesses:   4  
0    4.80784667215499e+003   4.80872732950073e+003  
0    1.05264215520092e+004   1.05273043378212e+004  
1    1.65167780853593e+004   1.65185840063538e+004  
1    6.52228387069902e+004   6.52246514228552e+004  

最后几行(即以 0 或 1 开头的行)对应于“访问次数”行中的整数。我想通过解析 文件并打印纬度、经度以及仅具有超过 1 次访问的 PointNumbers 的每个访问实例之后的两个值。

我不确定是从扫描仪或分词器开始还是编译模式。哪种技术可以更轻松地仅存储有效的 PointNumber? 直觉告诉我解析文件,将相关值放入对象中:

PointNumber num1 = new PointNumber(lat, lon, accesses[]);

然后循环遍历对象并在访问数组长度>时打印对象的值。 1. 另一方面,对于不符合要求的信息,最好忽略。是否可以通过在解析时检查 NumberOfAccesses 值然后如果该值 <= 1 则跳转到下一个 string.startsWith("PointNumber:") 来完成?

我有一种感觉,这个社区的绝大多数人都会尝试引导我转向 XML 或 YAML,但我真的更喜欢尝试 在 Java 中解决这个问题。任何建议、方向或适用的例子总是非常感激。

As a novice Java programmer, I've run into a rather high hurdle while trying to analyze data for a personal project of mine. I have a text
file with ~33.5k data points in the following format:

PointNumber:        33530  
Lat:                8.99167773897820e-001  
Lon:                6.20173660875318e+000  
Alt:                0.00000000000000e+000  
NumberOfAccesses:   4  
0    4.80784667215499e+003   4.80872732950073e+003  
0    1.05264215520092e+004   1.05273043378212e+004  
1    1.65167780853593e+004   1.65185840063538e+004  
1    6.52228387069902e+004   6.52246514228552e+004  

The final rows, i.e. ones beginning with 0 or 1, correspond to the integer in the Number of Accesses row. I would like to parse through the
file and print the Lat, Lon, and two values following each access instance of only PointNumbers having more than 1 number of accesses.

I'm not sure whether to start with a scanner or tokenizer or to compile a pattern. Which technique makes storing only valid PointNumbers easier?
Intuition tells me to parse through the file, placing relevant values into an object:

PointNumber num1 = new PointNumber(lat, lon, accesses[]);

Then loop through the objects and print the object's values if the accesses array length is > 1. On the other hand, it would be better to disregard the information that does not meet the requirements. Could this be done by checking the NumberOfAccesses value while parsing then jump ahead to the next string.startsWith("PointNumber:") if the value is <= 1?

I have a feeling the overwhelming majority of this community will try steering me towards XML or YAML, but I really prefer trying to
tackle this in Java. Any advice, direction, or applicable examples is always greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风渺 2024-10-26 23:57:34

当您有要解析的输入时,您可以循环执行此操作

// Assuming you have a scanner object named s and pointed to your file:

// PointNumber: 33530
int pointNumber = 0;
while(!s.next().equals("PointNumber:")) {
    pointNumber = s.nextInt();
}

// Lat: 8.99167773897820e-001
double lat = 0.0;
while(!s.next().equals("Lat:")) {
    lat = s.nextDouble();
}

// Lon: 6.20173660875318e+000
double lon = 0.0;
while(!s.next().equals("Lon:")) {
    lon = s.nextDouble();
}

// Alt: 0.00000000000000e+000
double alt = 0.0;
while(!s.next().equals("Alt:")) {
    alt = s.nextDouble();
}

// NumberOfAccesses: 4
int numberOfAccesses = 0;
while(!s.next().equals("NumberOfAccesses:")) {
    numberOfAccesses = s.nextInt();
}

// 0 4.80784667215499e+003 4.80872732950073e+003
// 0 1.05264215520092e+004 1.05273043378212e+004
// 1 1.65167780853593e+004 1.65185840063538e+004
// 1 6.52228387069902e+004 6.52246514228552e+004

// Assuming you have defined an Access class
LinkedList<Access> accesses = new LinkedList<Access>();
for(int i = 0; i < numberOfAccesses; i++) {
    // Assuming this is the constructor for the Access class
    accesses.add(new Access(s.nextInt(), s.nextDouble(), s.nextDouble()));
}

然后您可以将您实际想要的所有数据添加到“PointNumber”列表中,然后打印或从中执行任何操作。

You can loop through this while you have input to parse

// Assuming you have a scanner object named s and pointed to your file:

// PointNumber: 33530
int pointNumber = 0;
while(!s.next().equals("PointNumber:")) {
    pointNumber = s.nextInt();
}

// Lat: 8.99167773897820e-001
double lat = 0.0;
while(!s.next().equals("Lat:")) {
    lat = s.nextDouble();
}

// Lon: 6.20173660875318e+000
double lon = 0.0;
while(!s.next().equals("Lon:")) {
    lon = s.nextDouble();
}

// Alt: 0.00000000000000e+000
double alt = 0.0;
while(!s.next().equals("Alt:")) {
    alt = s.nextDouble();
}

// NumberOfAccesses: 4
int numberOfAccesses = 0;
while(!s.next().equals("NumberOfAccesses:")) {
    numberOfAccesses = s.nextInt();
}

// 0 4.80784667215499e+003 4.80872732950073e+003
// 0 1.05264215520092e+004 1.05273043378212e+004
// 1 1.65167780853593e+004 1.65185840063538e+004
// 1 6.52228387069902e+004 6.52246514228552e+004

// Assuming you have defined an Access class
LinkedList<Access> accesses = new LinkedList<Access>();
for(int i = 0; i < numberOfAccesses; i++) {
    // Assuming this is the constructor for the Access class
    accesses.add(new Access(s.nextInt(), s.nextDouble(), s.nextDouble()));
}

Then you can add all the data you actually want into a list of "PointNumber" and then print or do whatever from it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文