替换存储在字符串数组中的换行符

时间:2012-09-11 16:11:36

标签: java arrays string

  

可能重复:
  How do I compare strings in Java?

考虑下面的字符串数组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;
                }
            }
        }

以下是我打印数组的方法,

        for (int i=0;i<5;i++)
        {
            for (int j=0;j<5;j++)
            {
                System.out.print(a[i][j]);
            }
           System.out.println("");
        }

我想要的输出应该是,

ABCnullnull

DEFnullnull

有没有更好的方法解决这个问题?

3 个答案:

答案 0 :(得分:1)

BufferedReader有一个readLine()方法,它将返回一个字符串,其中包含\ n或\ r \ n之前的所有字符。它还在流的末尾返回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 = 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());
        }

读取文件应该导致List大小为2,每个包含5个字符的数组。 ['A','','B','','C']然后['D','','E','','F']

答案 1 :(得分:0)

尝试

public static String newline = System.getProperty("line.separator");
for (int i=0;i<5;i++)
{
    if (newline.equals(a[i]))
    {
        a[i] = null;
    }
}

答案 2 :(得分:0)

编辑回答:

从阅读您的回复并查看您的预期结果是什么,您最好不要做这样的事情......

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]);
相关问题