• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 725
  • Last Modified:

If statment & Printing String array without "null" values

hi, experts
I'm trying to equal too arrays values but for some reason if I used ( == ) not working,
the first array contain values "T" and "F" and "null"
the secound array contain values "T" and "F"
the statment like this

if (studentAnswer[row][col]==correctAnswer[answerNo])
          points = points+1;

second , how to print the String 2D array without the "null" value
1 Solution
>>> I'm trying to equal too arrays values
-> I'm trying to compare two array values ...

Are studentAnswer and studentAnswer of the type String?
Then, put you "null" in them or null? Makes a difference!

usmbayAuthor Commented:
yes they are the same type


the print out of the array like this

ABC54102 T   F T F T F T T T F T T F T T F   T F 19 null

DEF56278 T T F T F T T T F T F T F F T T F T T F null null

I want remove the null values!!!
> f (studentAnswer[row][col]==correctAnswer[answerNo])

When you compare two instances using ==, you are actually comparing their memory addresses to see if they are references to the same object

This is true:

  String a = "Christian Cantrell";
  String b = "Christian Cantrell";
  if (a.equals(b)) {


But this is false:

  if (a == b) {
      // Different memory addresses.

so you have to use equals method to check for String's equality
and becareful of null references
if studentAnswer[row][col] == null
saying studentAnswer[row][col].equals(correctAnswer[answerNo];
would throw a NullPointerException

you have to eliminate it by checking null values first

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

if (studentAnswer[row][col]==correctAnswer[answerNo])
          points = points+1;


if (studentAnswer[row][col]!=null && studentAnswer[row][col].equals(correctAnswer[answerNo]))
          points = points+1;

and to print

for(int row=0;row<rowCount;row++)
  for(int col=0;col<colCount;col++)
      System.out.println( "Q "+(row*rowCount+col)+": " + studentAnswer[row][col]);

where (row*rowCount+col) results in the question number.

you need to have rowCount and colCount setup to the number of rows and columns in the sheet.


What's the thing about null values?
>>What's the thing about null values?

The reason i ask is that i'm guessing that null represents an unanswered question. You need to be careful about order.

The Arrays.equals thing may only be useful in the case where you wish to check all answers en masse. You probably mostly  need to check them individually.
usmbayAuthor Commented:
this is the case

if studentAnswer = correctAnswer       points=points+2
 studentAnswer != correctAnswer        points=points-1
studentAnswer = null                          points=points

this one is ok but about the other result
if (studentAnswer[row][col]!=null && studentAnswer[row][col].equals(correctAnswer[answerNo]))


if (studentAnswer[row][col] == null) {
else if (studentAnswer[row][col].equals(correctAnswer[answerNo])) {
      points += 2;
else {
      points -= 1;

>>"Christian Cantrell";

Is that you btw?

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now