Results 1 to 7 of 7
  1. #1
    Daykeem is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default Comparing Arrays in Java

    Hey guys,
    I'm new at Java and I'm completely baffled by this problem.

    This is a single method within a class I'm trying to create.
    I'm trying to compare the variables in the array "YesNo", with the variables in the array "arrayShark", using the for loop and if statements; however, for some reason, they cannot be compared because the array YesNo always returns null. Is there a way to do this effectively?

    (the red portion of the code is where I attempt to compare the arrays)

    Java Code:
      String noLegs, twoLegs, fourLegs;
      String yesNo[] = {noLegs, twoLegs, fourLegs};
    
      public void sharks3() //method
      {
        int sharkPoints = 0;
        
        String arrayShark[] = {"yes", "No", "Yes"};  //predefined answers for the Shark in an array
        for (int i = 0; i < arrayShark.length; i++) //searches the length of the array
        {      
          c.println("print shark attributes: " + arrayShark[i]); //displays the current value of arrayShark
          c.println("print user answers: " + yesNo[i]); //displays the current value of yesNo        
          [COLOR="Red"]if (arrayShark[i].equals(yesNo[i]))[/COLOR] //if the shark array equals the answer generated by the user
          {sharkPoints++; //add 1 point to sharkPoints variable
          }// end if
        }//end for
        c.println("shark points: "+ sharkPoints); //display how many points the array has accumulated
      }//end sharks3

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default

    for some reason, they cannot be compared because the array YesNo always returns null
    In the code you posted, the values of the yesNo array are never initialized to anything (eg noLegs, twoLegs etc... are not initialized)...hence they are null.

  3. #3
    Daykeem is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    I apologize. I mentioned this is a class right? Well theres a main console where the values for the YesNo variables are initialized.

    where the main console initializes those values:
    Java Code:
        c.println("no legs?");
        a.noLegs = c.readString();
        c.println("two legs?");
        a.twoLegs = c.readString();
        c.println("four legs?");
        a.fourLegs = c.readString();

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default

    A good example of the usefulness of an SSCCE that clearly demonstrates the problem

  5. #5
    Daykeem is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    I apologize if my terminology is a bit skewed. I'm still new at java programming.

    The first set of code below is the console where you can run the program from. The second set of code is the class where most of the program is stored. The method "sharks3" is what I'm trying to fix. I'm trying to make the arrays "yesNo", and "arraySharks", compare their String values. yesNo get's its values from the user (located in the first set of code. Note: these values will only be String "yes", or "no"), while the arraySharks are predefined values found in the method.

    The portion i highlighted in red is the part of the code I'm having problems with. I'm trying to compare the indexes of both arrays in order so that if of the same index value of arraySharks and yesNo are a match, then the variable sharkPoints goes up one increment ( sharkPoints++; ).

    The Problem: When the two arrays are compared, the value of yesNo turns up as null (which is clear if you compile these two file together).

    I hope this helps


    Java Code:
    //Console
    import java.awt.*; //import console
    import hsa.Console; // import Colours, Fonts, Shapes, etc
    
    public class console
    {
      static Console c; //The output console
      static Akheem a; //Akheem class
      
      public static void main (String[] args)
      {
        c=new Console();
        a=new Akheem(c);
        
        c.println("no legs?");
        a.noLegs = c.readString();
        c.println("two legs?");
        a.twoLegs = c.readString();
        c.println("four legs?");
        a.fourLegs = c.readString();
        
        a.sharks3();
        
      }
    }
    Java Code:
    //Class
    import hsa.Console;
    import java.awt.*;
    public class Akheem
    {
      //start variables
      protected static Console c;
      String noLegs, twoLegs, fourLegs;
      String yesNo[] = {noLegs, twoLegs, fourLegs}; //is not protected so console can manipulate these variables
      //end variables
      
      public Akheem(Console c)
      {
        this.c = c;
      }//end Akheem constructor
      
      public void sharks3() //trying to shorten the MAIN SHARKS METHOD
      {
        int sharkPoints = 0;
        String arrayShark[] = {"yes", "No", "Yes"};  //predefined answers for the Shark in an array
        for (int i = 0; i < arrayShark.length; i++) //searches the length of the array
        {      
          c.println("print shark attributes: " + arrayShark[i]); //displays the current value of arrayShark
          c.println("print user answers: " + yesNo[i]); //displays the current value of yesNo        
          [COLOR="Red"]if (arrayShark[i].equals(yesNo[i]))[/COLOR] //if the shark array equals the answer generated by the user
          {sharkPoints++; //add 1 point to sharkPoints variable
          }// end if
        }//end for
        c.println("shark points: "+ sharkPoints); //display how many points the array has accumulated
      }//end sharks3
      
    }//end Die class
    Last edited by Daykeem; 01-15-2011 at 01:54 AM.

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default

    which is clear if you compile these two file together
    No its not clear because hsa.Console is not part of J2SE and this is not compilable (and not an SSCCE)

    The problem lies in re-assinging references for the noLegs, etc...which does not inherently change the values in the array. Here is what I meant by an SSCCE, and should clearly demonstrate the problem
    Java Code:
    public class Test {
    
    	String val1, val2, val3;
    	
    	String[] array = {val1, val2, val3};
    	
    	public static void main(String[] args){
    		Test test = new Test();
    		test.val1 = "test";
    		test.val2 = "test2";
    		test.val3 = "test3";
    		for ( int i = 0; i < test.array.length; i++ ){
    			System.out.println(test.array[i]);
    		}
    	}
    	
    }
    Run this and you will see it print nulls. To fix, assign the values of the array directly.

  7. #7
    Daykeem is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    I apologize; it doesn't seem like I read the documentation properly. There was much in there I didn't understand. But I have a concern; I thought I was assigning the values to the array in the "main" console window with this:

    Java Code:
        c.println("no legs?");
        a.noLegs = c.readString();
        c.println("two legs?");
        a.twoLegs = c.readString();
        c.println("four legs?");
        a.fourLegs = c.readString();
    The only difference [to me] is that I'm letting the user input the values.
    Unless the values the user inputs is only making it to the String and is not being passed into the String array values, which is what I assumed would happen.

    Thanks again for the help.
    Last edited by Daykeem; 01-15-2011 at 04:33 AM.

Similar Threads

  1. Comparing arrays
    By mitty in forum New To Java
    Replies: 8
    Last Post: 04-14-2010, 11:55 AM
  2. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM
  3. java arrays
    By miko5054 in forum New To Java
    Replies: 15
    Last Post: 03-18-2010, 05:17 PM
  4. Comparing two Char arrays
    By viperlasson in forum New To Java
    Replies: 3
    Last Post: 01-30-2010, 08:05 AM
  5. comparing arrays..
    By circuspeanuts in forum New To Java
    Replies: 5
    Last Post: 05-25-2009, 07:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •