If statment & Printing String array without "null" values

Posted on 2006-04-29
Last Modified: 2008-01-16
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
Question by:usmbay
    LVL 24

    Expert Comment

    >>> 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!


    Author Comment

    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!!!
    LVL 14

    Expert Comment

    > 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

    LVL 5

    Accepted Solution

    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.
    LVL 86

    Expert Comment


    What's the thing about null values?
    LVL 86

    Expert Comment

    >>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.

    Author Comment

    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]))

    LVL 86

    Expert Comment


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

    >>"Christian Cantrell";

    Is that you btw?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now