所以在过去的一周里,我有一个作业,我在这个作业中必须做的一件事是,从一个文本文件中读取格式化的数据。我所说的格式化是这样的:
{
Marsha 1234 Florida 1268
Jane 1523 Texas 4456
Mark 7253 Georgia 1234
}
(注意:这只是一个示例。不是我作业中的实际数据。)
现在我一直在试着自己解决这个问题。我尝试将每一行作为字符串读取,并使用.substring()
获取字符串的某些部分,将其放入数组中,然后从数组中获取该字符串的索引,并将其打印到屏幕上。现在我已经尝试了这个想法的几个不同的变体,但它就是不起作用。它要么以错误结束,要么以一种奇怪的方式输出数据。现在作业明天就要交了,我不知道该怎么办。如果有人能在这件事上为我提供一些帮助,我将不胜感激。
发布于 2018-10-11 17:20:51
对于您给出的示例,使用正则表达式模式\s+
拆分代码行是可行的:
String s = "Marsha 1234 Florida 1268";
s.split("\\s+");
结果数组中包含4个元素“玛莎”、"1234“、”佛罗里达“和"1268”。
我使用的模式匹配一个或多个空格字符-有关详细信息和其他选项,请参阅The JavaDocs of Pattern
。
另一种方法是定义您的线路需要作为整体匹配的模式,并捕获您感兴趣的组:
String s = "Marsha 1234 Florida 1268";
Pattern pattern = Pattern.compile("(\\w+)\\s+(\\d+)\\s+(\\w+)\\s+(\\d+)");
Matcher matcher = pattern.matcher(s);
if (!matcher.matches())
throw new IllegalArgumentException("line does not match the expected pattern"); //or do whatever else is appropriate for your use case
String name = matcher.group(1);
String id = matcher.group(2);
String state = matcher.group(3);
String whatever = matcher.group(4);
此模式要求第二组和第四组仅由数字组成。
但是请注意,如果您的数据也可以包含空格,那么这两种方法都将失效-在这种情况下,您需要不同的模式。
发布于 2018-10-11 17:28:32
首先你必须知道你的文件的格式。就像你的例子,如果它以{开头,以}结尾。数据的分隔符是什么?例如,分隔符可以是分号、空格等。了解了这一点,你就可以开始构建应用程序了。在您的示例中,我将编写类似这样的代码:
public class MainClass
{
public static void main(String[] args)
{
String s = "{\r\n"+
"Marsha 1234 Florida 1268\r\n" +
"Jane 1523 Texas 4456\r\n" +
"Mark 7253 Georgia 1234\r\n"+
"}\r\n";
String[] rows = s.split("\r\n");
//Here we will keep evertihing without the first and the last row
List<String> importantRows = new ArrayList<>(rows.length-2);
//lets assume that we do not need the first and the last row
for(int i=0; i<rows.length; i++)
{
//String r = rows[i];
//System.out.println(r);
if(i>0 && i<rows.length)
{
importantRows.add(rows[i]);
}
}
List<String> importantWords = new ArrayList<>(rows.length-2);
//Now lets split every 'word' from row
for(String rowImportantData : importantRows)
{
String[] oneRowData = rowImportantData.split(" ");
//Here we will have one row like: [Marsha][ ][ ][ ][1234][ ][ ][ ][Florida][ ][ ][1268]
// We need to remove the whitespace. This happen because there is more
//then one whitespace one after another. You can use some regex or another approach
// but I will show you this because you can have data that you do not need and you want to remove it.
for(String data : oneRowData)
{
if(!data.trim().isEmpty())
{
importantWords.add(data);
}
//System.out.println(data);
}
}
//Now we have the words.
//You must know the rules that apply for this data. Let's assume from your example that you have (Name Number) group
//If we want to print every group (Name Number) and we have in this state list with [Name][Number][Name][Number]....
//Then we can print it this way
for(int i=0; i<importantWords.size()-1; i=i+2)
{
System.out.println(importantWords.get(i) + " " + importantWords.get(i+1));
}
}
}
这只是一个例子。你可以用很多不同的方式制作你的应用程序。重要的是,你要知道你想要处理的信息的初始状态是什么,以及你想要实现的结果是什么。
祝好运!
发布于 2018-10-11 18:45:59
有许多不同的方法可以用来读取这个格式化的文件。我建议您首先以字符串列表的形式从文本中提取相关数据,然后将行分割为字段。这是一个如何使用您给出的数据样本来完成此操作的示例:
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class CustomTextReader {
public static void main(String[] args) {
String text =
"Marsha 1234 Florida 1268\r\n" +
"Jane 1523 Texas 4456\r\n" +
"Mark 7253 Georgia 1234";
//Extract the relevant data from the text as a list of arrays
// in which each array is a line, and each element is a field.
List<String[]> data = getData(text);
//Just printing the results
print(data);
}
private static List<String[]> getData(String text) {
//1. Separate content into lines.
return Arrays.stream(text.split("\r\n"))
//2. Separate lines into fields.
.map(s -> s.split("\\s{2,}"))
.collect(Collectors.toList());
}
private static void print(List<String[]> data) {
data.forEach(line -> {
for(String field : line) {
System.out.print(field + " | ");
}
System.out.println();
});
}
}
重要的是要知道数据在格式方面会有什么变化。如果您知道字段中不包含空格,则可以在步骤2中使用" "
或\\s{2,}
作为拆分字符串的模式。"North Carolina"),最好使用另一个像\\s{2,}
这样的正则表达式(这就是我在上面的示例中所做的)。我希望我能帮到你!
https://stackoverflow.com/questions/52755983
复制相似问题