Results 1 to 7 of 7
Thread: Comparing Arrays in Java
- 01-15-2011, 12:02 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 4
- Rep Power
- 0
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
- 01-15-2011, 12:07 AM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
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.for some reason, they cannot be compared because the array YesNo always returns null
- 01-15-2011, 12:11 AM #3
Member
- Join Date
- Jan 2011
- Posts
- 4
- Rep Power
- 0
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();
- 01-15-2011, 12:36 AM #4
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
A good example of the usefulness of an SSCCE that clearly demonstrates the problem
- 01-15-2011, 01:49 AM #5
Member
- Join Date
- Jan 2011
- Posts
- 4
- Rep Power
- 0
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 classLast edited by Daykeem; 01-15-2011 at 01:54 AM.
- 01-15-2011, 02:41 AM #6
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
No its not clear because hsa.Console is not part of J2SE and this is not compilable (and not an SSCCE)which is clear if you compile these two file together
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
Run this and you will see it print nulls. To fix, assign the values of the array directly.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]); } } }
- 01-15-2011, 04:25 AM #7
Member
- Join Date
- Jan 2011
- Posts
- 4
- Rep Power
- 0
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:
The only difference [to me] is that I'm letting the user input the 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();
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
-
Comparing arrays
By mitty in forum New To JavaReplies: 8Last Post: 04-14-2010, 11:55 AM -
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03-23-2010, 01:40 AM -
java arrays
By miko5054 in forum New To JavaReplies: 15Last Post: 03-18-2010, 05:17 PM -
Comparing two Char arrays
By viperlasson in forum New To JavaReplies: 3Last Post: 01-30-2010, 08:05 AM -
comparing arrays..
By circuspeanuts in forum New To JavaReplies: 5Last Post: 05-25-2009, 07:05 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks