- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个CSV文件。在此文件中,有时它将在一个单元格中的不同行上包含多个项目。
我想使用replaceAll(“ / r / n”,“”);消除此问题。但是我不确定我该在代码中放哪?
public static Scanner getFields(Scanner console) {
File f;
do {
System.out.print("Please give me the Fields: " );
String file = console.nextLine();
f = new File(file);
} while (!f.exists());
Scanner readMe = null;
try {
readMe = new Scanner(f);
} catch (FileNotFoundException e) {
System.err.println("FileNotFoundException: " + e.getMessage());
e.printStackTrace();
}
return readMe;
}
public static void readMe(Scanner fieldScan) {
int row = 0;
fieldScan = fieldScan.replaceAll("\r\n", " ");
while (fieldScan.hasNextLine()) {
row++;
String current = fieldScan.nextLine();
Field currentField = new Field(current, row);
if (!currentField.getRemove()) {
fieldArrayList.add(currentField);
}
}
最佳答案
在读入CSV文件之前,我曾想过如何清理它,并遇到this SO线程,该线程涉及从Scanner对象读取到String变量。通过将扫描仪上的定界符设置为\\Z
(即文件的末尾),则文件的全部内容将作为一个字符串读入。
因此,如果您在达到readMe
方法之前对文件执行清理工作,则该方法应该起作用!请参阅以下有关我建议使用的方法:
public static void cleanseCsvFile(string FilePath) {
File csvFile = new File(FilePath);
String content = new Scanner(csvFile).useDelimiter("\\Z").next();
content = content.replaceAll("(\r\n){1}(?!$)"," ")
FileUtils.writeStringToFile(csvFile, content);
}
readMe
方法之前调用它,则应该可以正常进行:)
关于java - 尝试替换CSV字符串中的/r/n以解决单元格中的换行符,而无需在.hasNextLine()之前使用API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46118831/
我知道我需要使用 hasNextLine() 来推进行 - 但是当我引用 hasNextLine() 时,我不确定为什么它仍然没有退出 while 循环: 我在这里 (sc) 使用扫描仪来读取输入文件
我在使用 Scanner.hasNextLine() 方法时遇到问题。这是我的代码: while(scan.hasNextLine()) { definition = sca
编译此类时遇到问题。我正在阅读一本关于 java 的书,这是练习之一,他们提供了大部分代码。所以这基本上是本书提供的一个库存类。 现在,我遇到的问题是在编译时出现此错误: cannot find sy
假设我有以下文件 Hej Hej Hej A A A A 在 A 行之后换行。 我的问题是代码 while (in.hasNextLine()) { String line = in.next
所以我应该确定文本文件中的行数(包含数字的 100 行),然后创建一个包含行数的数组,但第一个 while 循环用于找出行数文本文件中永远不会存在。第二个循环完全相同,效果很好。请帮帮我! stati
我已经为此工作了几个小时,但自己或在线都找不到问题。我不知道为什么第一个循环确实有效,而第二个循环没有继续经过第一次迭代。 File adjacencies = new File(adjacencie
我正在尝试制作一个拼写检查器,并打算在链表中打开 .txt 文件中的单词,但 hasNextLine() 总是返回 false。 import java.io.File; import java.io
我尝试建立一点服务器-客户端连接。 它们都有一个 Scanner 和一个 PrintWriter,并且它们使用 Socket 的输入和输出流相互写入。 客户端.java: import java.io
首先,我是 Java 的新手,正在尝试完成学校布置的关于创建自动售货机的作业。我的程序将 2 个文件作为 cli 参数,一个用于产品,另一个用于金钱。 对于我的生活,我无法弄清楚为什么代码卡在第 42
我有以下代码: Scanner scanner = new Scanner (file); while (scanner.hasNextLine()){ System.out.println
在我的一项作业中,我试图读取一个文件并逐行打印它。我尝试再次使用 hasNextLine 但没有成功。我试图以某种方式完成它,但我想知道是否有某种方法可以再次浏览我的文件,而无需创建新的扫描仪。 这是
我正在读取这样一个文件的内容: #include #include using namespace std; char line[256]; ifstream infile(filename, i
抱歉,如果这听起来太简单了。我是 Java 的新手。 这是我用来检查 hasNextLine() 的一些简单代码。当我运行它时,我无法让它停止。我想如果你不写任何输入并按下 Enter,你就会跳出 w
我已经包含了 hasNextLine 异常,但我仍然收到 NoSuchElementException 错误 我正在创建一个从文件扫描的回文程序。我可以让它从文件中扫描,但它只返回非回文单词,并返回
我看到的这段代码对我来说看起来很不自然。我通常会使用 hasNextLine() 而不是 boolean 变量 done,如 while 循环中的此代码所示,但现在我很困惑。我的问题是,当需要从控制台
我正在尝试使用输入,但是我遇到了麻烦...如果我使用下面的代码,它还没有结束...所以我处于无限循环中。即使我的输入已到达文件结尾,它也会写类似的内容: ... A number has not be
我试图编写一个从键盘获取输入的读取方法。但输入是一个大文本,需要一次一行进行分析。 risks = new ArrayList(); try{ Scanner sc = new
当我尝试在我的程序上使用scanner.hasNextLine()时,它只是坐在那里并挂起。我想知道是否有任何方法可以解决这个问题或我可以使用任何替代方案。编辑:这背后的原因是我可以从扫描仪中删除所有
我在使用 Scanner.hasNextLine() 时遇到了这个奇怪的问题。我没有使用 Scanner 类,但我习惯使用 Scanner.hasNextLine() 作为循环的条件来连续获取用户的输
这可能更多是因为我对代码不熟悉,但我一直遇到以下问题: 我有一个包含空行的文本文件,以及一个扫描文件的扫描仪。 当我使用 .hasNextLine() 方法时,即使文件中有更多行,它也会返回 fals
我是一名优秀的程序员,十分优秀!