Note that there are some explanatory texts on larger screens.

plurals
  1. POReplace a newline character stored in an array of Strings
    text
    copied!<blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java">How do I compare strings in Java?</a> </p> </blockquote> <p>Consider the following 2D array of Strings a[5][5],</p> <p>I store three values in the first three blocks in the array "a". When I print my array, I get the following output.</p> <pre><code>ABC null DEF null </code></pre> <p>These values are present in a file and I retrieve the values and store them in an array of strings.</p> <p>The file ("file.txt")looks like this,</p> <pre><code>A B C D E F </code></pre> <p>Here is my code,</p> <p>Declaration:</p> <pre><code>static String [][] a= new String [4][4]; public static String newline = System.getProperty("line.separator"); private static int i,j; </code></pre> <p>Main code:</p> <pre><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 != ' ' &amp;&amp; c != (char)'\n') { a[i][j] = Character.toString(c); j++; } else if (c == '\n') { i++; j = 0; } } for (int i=0;i&lt;5;i++) { for (int j=0;j&lt;5;j++) { if (newline.equals(a[i][j])) { mainArray[i][j] = null; } } } </code></pre> <p>Here is how I print my array,</p> <pre><code> for (int i=0;i&lt;5;i++) { for (int j=0;j&lt;5;j++) { System.out.print(a[i][j]); } System.out.println(""); } </code></pre> <p>My desired output should be,</p> <pre><code>ABCnullnull DEFnullnull </code></pre> <p>Is there a better way to work on this problem??</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload