Java 替换存储在字符串数组中的换行符
可能重复:Java 替换存储在字符串数组中的换行符,java,arrays,string,Java,Arrays,String,可能重复: 考虑以下二维字符串数组a[5][5] 我在数组“a”的前三个块中存储了三个值。 当我打印数组时,我得到以下输出 ABC null DEF null 这些值存在于文件中,我检索这些值并将它们存储在字符串数组中 文件(“file.txt”)如下所示 A B C D E F 这是我的密码 声明: static String [][] a= new String [4][4]; public static String newline = System.getProperty(
考虑以下二维字符串数组a[5][5] 我在数组“a”的前三个块中存储了三个值。 当我打印数组时,我得到以下输出
ABC
null
DEF
null
这些值存在于文件中,我检索这些值并将它们存储在字符串数组中
文件(“file.txt”)如下所示
A B C
D E F
这是我的密码
声明:
static String [][] a= new String [4][4];
public static String newline = System.getProperty("line.separator");
private static int i,j;
主要代码:
i=j=0;
FileInputStream fin;
fin = new FileInputStream("file.txt");
DataInputStream in = new DataInputStream (fin);
BufferedReader br = new BufferedReader (new InputStreamReader (in));
while((c = (char)br.read()) != (char)-1)
{
if (c != ' ' && c != (char)'\n')
{
a[i][j] = Character.toString(c);
j++;
}
else if (c == '\n')
{
i++;
j = 0;
}
}
for (int i=0;i<5;i++)
{
for (int j=0;j<5;j++)
{
if (newline.equals(a[i][j]))
{
mainArray[i][j] = null;
}
}
}
是否有更好的方法解决此问题???BufferedReader有一个readLine()方法,该方法将返回一个字符串,其中所有字符位于\n或\r之前。它还在流的末尾返回null
FileInputStream fin;
fin = new FileInputStream("file.txt");
DataInputStream in = new DataInputStream (fin);
BufferedReader br = new BufferedReader (new InputStreamReader (in));
List<String> list = new ArrayList<String>();
String line;
while ((line= br.readLine())!=null)
{
list.add(line);
}
FileInputStream-fin;
fin=新文件输入流(“file.txt”);
DataInputStream in=新的DataInputStream(fin);
BufferedReader br=新的BufferedReader(新的InputStreamReader(in));
列表=新的ArrayList();
弦线;
而((line=br.readLine())!=null)
{
列表。添加(行);
}
这将处理任意数量的返回和任意长度的字符串
或者如果必须将每一行作为和数组
FileInputStream fin;
fin = new FileInputStream("file.txt");
DataInputStream in = new DataInputStream(fin);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
List<char[]> list = new ArrayList<char[]>();
String line;
while ((line = br.readLine()) != null) {
if(!line.isEmpty()) list.add(line.toCharArray());
}
FileInputStream-fin;
fin=新文件输入流(“file.txt”);
DataInputStream in=新的DataInputStream(fin);
BufferedReader br=新的BufferedReader(新的InputStreamReader(in));
列表=新的ArrayList();
弦线;
而((line=br.readLine())!=null){
if(!line.isEmpty())list.add(line.tocharray());
}
读取文件时,列表大小应为两个,每个列表包含5个字符。然后再试试看
public static String newline = System.getProperty("line.separator");
for (int i=0;i<5;i++)
{
if (newline.equals(a[i]))
{
a[i] = null;
}
}
公共静态字符串newline=System.getProperty(“line.separator”);
对于(int i=0;i编辑的答案:
通过阅读你的回答,看看你期望的结果是什么,你可能会做这样的事情更好
pseudo-code
read entire line into String array index
Before printing, check length of String (a[i].length)
If length is less than 5, add 'null' to the end of the String for every character less than 5
Thus:
if(a[i].length < 5)
{
int index = 5 - a[i].length;
for( ; index > 0; index --)
{
a[i].concat("null");
}
}
伪代码
将整行读取到字符串数组索引中
打印前,检查字符串的长度(a[i].长度)
如果长度小于5,则为每个小于5的字符在字符串末尾添加“null”
因此:
if(a[i]。长度<5)
{
int index=5-a[i]。长度;
对于(;索引>0;索引--)
{
a[i].concat(“空”);
}
}
原始答案。。。。。。。。。。。。
不确定我的评论是否发送给你。你可能只是索引太远了
试一试
for(int i=0;i<4;i++)
系统打印(a[i]);
这可能会有帮助:使用“\n”.equals(a[i])
代替。切勿将字符串与=
进行比较。空值出现在下一行,因为出现了换行符“否,空值
:)尝试了“\n”.equals(a[i]),因此打印出来,这不起作用。我想要的数组输出应该是B C null,而不是我在问题中提到的那个。是的,在数组中存储值之前,我故意在整个数组中存储null。当你说不工作时,你是说[I]=null不能执行吗?或者其他一些我们看不到的代码没有达到您的期望?你能展示你用来输出值的代码吗?我用来生成输出的代码是,例如(int I=0;因为他说他期望“A B C null null”,我认为A[4]是一个输入错误,应该是A[5]在问题中。如果他索引到far,它会抛出一个异常。我也这么认为Yoz,它应该抛出一个异常,但看看他编辑的问题,他实际上有一个[4][4]…编辑:忽略,我错过了他和你上面的评论…抱歉…java.lang.ArrayIndexOutOfBoundsException:4Yes yoz,你是对的…数组应声明为[5][5]您可以随时读取整行并将其放入单个字符串变量中。这只是外部数组的替代。List是一个通用集合,其中T可以是任何类。它不能是int、double、char,但可以是char[]或String等
public static String newline = System.getProperty("line.separator");
for (int i=0;i<5;i++)
{
if (newline.equals(a[i]))
{
a[i] = null;
}
}
pseudo-code
read entire line into String array index
Before printing, check length of String (a[i].length)
If length is less than 5, add 'null' to the end of the String for every character less than 5
Thus:
if(a[i].length < 5)
{
int index = 5 - a[i].length;
for( ; index > 0; index --)
{
a[i].concat("null");
}
}
for(int i = 0; i < 4; i++)
System.print(a[i]);