Can i show any other word instead of "null" in an array ?

Recommended Answers

All 17 Replies

Sure, if it's an array of Strings. What sort of array have you got?

i got an array of Strings but i didn't stored value at [1][5] = [5][6] = [7][2] = [1][2]

the about is fine but i want to replace the word "null" in the output with the word "No"

Get the value from the array into a String variable. Test if it is null and if so, set it to "No". Then pass that String to who ever wants the contents of the array at those index values.

i didn't understand you..! i tried using if condition and replacing the new word using System.out.println ...but it is not working

it is not working

Can you explain?
I can NOT make comments on your code unless you post it.

String test = anArray[1][5];  // get the element from the array
if(test == null)              // test if the element is null 
  test = "No";                // change the value returned to "No"

Please clarify: Are you looking to change the underlying data, or simply the way you present it?
That is, do you want unassigned cells in the array to contain the value NULL or "No"?

If you just want to change the output, when you're looping over the array to print it, your println will be something like

System.out.println(cell==NULL ? "No" : cell);

If you want to change the actual underlying values, you'd do a similar thing but with an assignment:

cell = (cell==NULL ? "No" : cell);

Can you explain?
I can NOT make comments on your code unless you post it.

String test = anArray[1][5];  // get the element from the array
if(test == null)              // test if the element is null 
  test = "No";                // change the value returned to "No"

i tried this

String test = anArray[1][5];  // get the element from the array
if(test == null)              // test if the element is null 
  test = "No";                // change the value returned to "No"

but it is not working,
here is mY program c0de...

class Test {
 enum Days {Monday,Tuesday,Wednesday,Thursday,Friday}
 enum Periods {Islam,Java,AutoCAD,Elec,DIFF_EQ,English}
 public static void main (String [] args)
 {
  int i,j;
  String DnP [][] = new String [9][5];

    for (Days dys : Days.values())
     {
      DnP [0][dys.ordinal()] = dys.name();
      System.out.print (DnP[0][dys.ordinal()] + "\t      ");
     }
    System.out.println ("\n");
    Periods Islam = Periods.Islam;
    Periods Java = Periods.Java;
    Periods AutoCAD = Periods.AutoCAD;
    Periods Elec = Periods.Elec;
    Periods DIFF_EQ = Periods.DIFF_EQ;
    Periods English = Periods.English;
    DnP[2][0] = DnP [3][4] = Islam.name();
    DnP[5][1] = DnP [7][3] = DnP [5][4] = English.name();
    DnP[3][0] = DnP [4][0] = DnP [4][1] = DnP [4][4] = DIFF_EQ.name();
    DnP[2][1] = DnP [3][1] = DnP [1][2] = DnP [2][2] = DnP [3][2] = DnP [6][3]= Elec.name();
    DnP[6][1] = DnP [4][2] = DnP [5][2] = DnP [6][2] = DnP [4][3] = DnP [5][3]= Java.name();
    DnP[1][3] = DnP [2][3] = DnP [3][3] = AutoCAD.name();
    String test = DnP[9][5];
    if(test==null) // test if the element is null
    test = "No"; // change the value returned to "No"
    for (i=1; i<8; i++)
    {
      for (j=0;j<5;j++)
      System.out.print (DnP[i][j] + "\t\t");
      System.out.println ("");
    }
 }
}

the error is:

Monday Tuesday Wednesday Thursday Friday

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9
at Test.main(Test.java:27)

I want to change the output of the program and want to print the word "NO" for unassigned cell

String DnP [][] = new String [9][5];  
    ....
  String test = DnP[9][5];

What is the maximum index value for the first dimension of the DnP array?

What is the value of the index causing this: ArrayIndexOutOfBoundsException: 9

Change this line to test for null before printing the value of an array elememt:

System.out.print (DnP[i][j] + "\t\t");
String temp = DnP[i][j];
  // Here test the value of temp and change it if you don't like it 
  System.out.print (temp + "\t\t"); // print out the desired value

getting these errors when i replaced

System.out.println (DnP[i][j] + "\t\t");

with this

String temp = DnP[i][j];
  // Here test the value of temp and change it if you don't like it 
  System.out.print (temp + "\t\t"); // print out the desired value

Test.java:46: not a statement
String temp = DnP[j];
^
Test.java:46: ';' expected
String temp = DnP[j];
^
2 errors

Please show all of the code in front of( lines 1-45) where the errors occurred.

for (i=1; i<8; i++)
    {
      for (j=0;j<5;j++)

      String temp = DnP[i][j];
    System.out.print (temp + "\t\t"); // print out the desired value
      System.out.println ("");
    }

What happened to lines 1-45 for the error that you posted earlier?
The error message showed that the error occured on line 46

Test.java:46: not a statement

This last code you posted only has 8 lines.

?????

Sorry...my Mistake i posted the error of another program that is using more line,

here is the error,

Test.java:30: not a statement
String temp = DnP[j];
^
Test.java:30: ';' expected
String temp = DnP[j];
^
2 errors

and the code

class Test {
 enum Days {Monday,Tuesday,Wednesday,Thursday,Friday}
 enum Periods {Islam,Java,AutoCAD,Elec,DIFF_EQ,English}
 public static void main (String [] args)
 {
  int i,j;
  String DnP [][] = new String [9][5];

    for (Days dys : Days.values())
     {
      DnP [0][dys.ordinal()] = dys.name();
      System.out.print (DnP[0][dys.ordinal()] + "\t      ");
     }
    System.out.println ("\n");
    Periods Islam = Periods.Islam;
    Periods Java = Periods.Java;
    Periods AutoCAD = Periods.AutoCAD;
    Periods Elec = Periods.Elec;
    Periods DIFF_EQ = Periods.DIFF_EQ;
    Periods English = Periods.English;
    DnP[2][0] = DnP [3][4] = Islam.name();
    DnP[5][1] = DnP [7][3] = DnP [5][4] = English.name();
    DnP[3][0] = DnP [4][0] = DnP [4][1] = DnP [4][4] = DIFF_EQ.name();
    DnP[2][1] = DnP [3][1] = DnP [1][2] = DnP [2][2] = DnP [3][2] = DnP [6][3]= Elec.name();
    DnP[6][1] = DnP [4][2] = DnP [5][2] = DnP [6][2] = DnP [4][3] = DnP [5][3]= Java.name();
    DnP[1][3] = DnP [2][3] = DnP [3][3] = AutoCAD.name();
    for (i=1; i<8; i++)
    {
      for (j=0;j<5;j++)
       String temp = DnP[i][j];
  // Here test the value of temp and change it if you don't like it 
  System.out.print (temp + "\t\t"); // print out the desired value
      System.out.println ("");
    }
 }
}

Try adding {} around the code for the if(j=) statement without them.

hey its working when writing inside {} :) , but can you give me the reason why it is not working when writing without {} ?

The variable temp was defined inside the scope of the for() statement: lines 29 & 30.
The variable temp on line 32 was outside that scope.

thanks alot :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.