0

Possible Duplicate:
How do I compare strings in Java?

Consider the following 2D array of Strings a[5][5],

I store three values in the first three blocks in the array "a". When I print my array, I get the following output.

ABC

null

DEF

null

These values are present in a file and I retrieve the values and store them in an array of strings.

The file ("file.txt")looks like this,

A B C

D E F

Here is my code,

Declaration:

static String [][] a= new String [4][4];
public static String newline = System.getProperty("line.separator");
private static int i,j;

Main code:

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;
                }
            }
        }

Here is how I print my array,

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

My desired output should be,

ABCnullnull

DEFnullnull

Is there a better way to work on this problem??

Community
  • 1
  • 1
user1428900
  • 141
  • 1
  • 2
  • 11

3 Answers3

1

BufferedReader has a readLine() method that will return a string with all the chars preceding the \n or \r. It also returns null at the end of the stream.

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);  
}

This will cope with any number of returns and arbitrary length strings.

Or if you must have each line as and array

        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());
        }

Reading your file should result in a List size of two each containing and array of 5 chars. ['A',' ','B',' ','C'] then ['D',' ','E',' ','F']

Yoztastic
  • 862
  • 2
  • 8
  • 21
  • Its just an alternative to your outer array. List is a generic collection where T can be any class. It cant be int,double,char but it can be char[] or String etc – Yoztastic Sep 11 '12 at 17:37
0

Try

public static String newline = System.getProperty("line.separator");
for (int i=0;i<5;i++)
{
    if (newline.equals(a[i]))
    {
        a[i] = null;
    }
}
Vikdor
  • 23,934
  • 10
  • 61
  • 84
  • when you say does not work are you saying a[i] = null does not get executed? Or Some other code we cant see is not doing what you are expecting? can you show the code you are using to output the values? – Yoztastic Sep 11 '12 at 16:29
  • my code that I use to generate the output is, for (int i=0;i<5;i++) { System.out.print(a[i]); } – user1428900 Sep 11 '12 at 16:32
0

EDITED ANSWER:

From reading your responses and looking at what your expected output is, you may be better off doing something like this...

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");
  }
}

ORIGINAL ANSWER............ Not sure if my comment was sent to you or not. You might just be indexing too far out.

Try

for(int i = 0; i < 4; i++)
   System.print(a[i]);
  • since he has stated that he expects "A B C null null" I think the a[4] is a typo and should be a[5] in the question. If he was indexing to far it would have thrown an exception. – Yoztastic Sep 11 '12 at 16:51
  • I would think so too Yoz, it should throw an exception, but looking at his edited question, he actually has a[4][4]....EDIT: Disregard, I missed his and your comments above...sorry.... – user1661184 Sep 11 '12 at 16:57
  • java.lang.ArrayIndexOutOfBoundsException: 4 – Yoztastic Sep 11 '12 at 17:00
  • Yes yoz, you're correct... the array should be declared as a[5][5] – user1428900 Sep 11 '12 at 17:08
  • You could always just read the entire line in and place it in a single String variable. – user1661184 Sep 11 '12 at 17:19
  • That is possible, but once the array has been populated, I need to use it to perform some computation. To do this computation, I need the strings to be stored in separate array blocks. – user1428900 Sep 11 '12 at 17:22
  • After you print it you could access each element of the string...a[i].substring(int beginingIndex, int endingIndex). Just a brute force workaround in case you need it done immediately...lol – user1661184 Sep 11 '12 at 17:33