在文本文件中的逗号之间提取空白字 [英] Extract blank word between comma in text file
问题描述
我试图获取包含文本文件中的逗号和打印到日志文件之间的空字的记录
例如:
巴黎,美国,西班牙,日本
印度,斯里兰卡,巴基斯坦,孟加拉国
我将在日志中获得输出:巴黎,美国,西班牙,因为下面的逗号之间的空格是我的代码。
来自代码我不明白为什么数组没有检测到null eventHough我已经与(,)分开。
请帮助我。
我尝试过:
while((s = sr.readLine())!= null){
PrintStream p = new PrintStream(Log_file);
String a [] = s.split(,);
//系统。 out.println(s);
//System.out.println(a[5 ]);
for(String k:a)
{
if(k!= null)
{
//System.out.prin TLN(A [1]);
if(a [i] == null){
p.println(s);
I was trying to get the record which contains null word between comma from text file and printing to log file
for example :
paris,, America, Spain, Japan
india,Srilanka,Pakistan,Bangladesh
I will get the output in log: Paris, ,America, Spain, because getting null between comma below is my code .
from code i dont understand why array not detecting null eventhough i have split with (,).
pls help me out .
What I have tried:
while((s = sr.readLine())!= null){
PrintStream p = new PrintStream(Log_file);
String a[]= s.split(",");
// System.out.println(s);
//System.out.println(a[5]);
for(String k:a)
{
if(k !=null)
{
//System.out.println(a[i]);
if(a[i] ==null){
p.println(s);
推荐答案
a
中不会有null
值。如果两个逗号之间没有任何内容,则a
中的结果将是空字符串而不是null
。而不是检查null,检查字符串是否为空,k.isEmpty()
。
There won't benull
values ina
. If there is nothing between two commas, the result ina
will be an empty string rather thannull
. Instead of checking for null, check if the string is empty, withk.isEmpty()
.
这篇关于在文本文件中的逗号之间提取空白字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!