Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Function that compares two arrays and stores the result in a third one

  1. #1
    Junior Member
    Join Date
    Jun 2020
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Function that compares two arrays and stores the result in a third one

    Hello, I'm really new to Java. I have to do an exercice where a function compares two String arrays (one randomly generated by the computer and the other chosen by the user. And each time it finds an identical element, it puts it in a third array that's String too. The problem is that the main totally ignore the function and doesn't want to call her. And I got this error message :
    (in thread "main" java.lang.NullPointerException
    at Mastermind_ho.compareColor(Mastermind_ho.java:80)
    at Mastermind_ho.main(Mastermind_ho.java:31))

    I did this (I post the main as well as the function in question, to lighten I'm not posting the random function unless it's necessary). Thank you in adavnce

    --------------- the main
    import java.util.Scanner;
    public class Mastermind_ho {
    static Scanner sc = new Scanner(System.in);
    static String [] TAB_REF_COLORS = {"rouge","jaune","vert","bleu","orange","blanc","v iolet","fuchsia"};
    static int NB_COLORS = 4;
    static int compteur=0;
    static String [] combination;
    static String [] tabJoueur;
    static String [] tabCorrect= {" "," "," "," "};

    public static void main(String[] args) {

    String [] combinaisonSecrete = generateRandomCombination();
    for(int i=0;i<4;i++) {
    System.out.print(combinaisonSecrete[i]+ "|");
    }
    String [] combinaisonJoueur;
    System.out.println();
    do {
    combinaisonJoueur = choixNumerote();
    for(int i=0;i<4;i++) {
    System.out.print(combinaisonJoueur[i]+ "|");
    }
    //??
    }while (compteur<=12 && (12-compteur)>0);

    compareColor (tabJoueur, combination);
    for(int k=0;k<4;k++) {
    System.out.print(tabCorrect[k]+ "|");
    }

    sc.close();
    }

    --------------- the function

    static void compareColor (String [] tabPlayer, String [] tabOrdi) {
    int sizeTj = tabPlayer.length;
    int sizeTm = tabOrdi.length;
    int k=0;
    for (int i=0;i<sizeTj;i++) {
    for(int j=0; j<sizeTm; j++) {
    if (tabOrdi[j]==tabPlayer[i]) {
    System.out.print("Vous avez trouver une couleur");
    tabCorrect[k]=tabOrdi[j];
    k++;
    }
    else {
    continue;
    }
    }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Function that compares two arrays and stores the result in a third one

    (in thread "main" java.lang.NullPointerException
    at Mastermind_ho.compareColor(Mastermind_ho.java:80)
    Look at line 80 and find the variable that has the null value. Then backtrack in the code to find out why that variable does not have a value.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    Make sure the code has proper indentations that make it more readable.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2020
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Function that compares two arrays and stores the result in a third one

    Are you talking about tabPlayer? if that's the case the
    static void compareColor (String [] tabPlayer, String [] tabOrdi
    ) has got 2 arguments and the function in the main that calls it has also 2 arguments compareColor (tabJoueur, combination); and both of them have returned value : return tabJoueur; and return combination. So, I don't understand why they have null value then?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Function that compares two arrays and stores the result in a third one

    The statement you posted is a method declaration. There should not be a NullPointerException there.
    What statement is at line 80?
    What variable in that statement has a null value?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2020
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Function that compares two arrays and stores the result in a third one

    The entire code :

    import java.util.Scanner;
    public class Mastermind_ho {
     
    static Scanner sc = new Scanner(System.in);
    static String [] TAB_REF_COLORS = {"rouge","jaune","vert","bleu","orange","blanc","violet","fuchsia"};
    static int NB_COLORS = 4;
    static int compteur=0;
    static String [] combination;
    static String [] tabJoueur;
    static String [] tabCorrect= {" "," "," "," "};
     
     
    			public static void main(String[] args) {
     
    						String [] combinaisonSecrete = generateRandomCombination();
    						for(int i=0;i<4;i++) {
    							System.out.print(combinaisonSecrete[i]+ "|");
    						}
    						String [] combinaisonJoueur;
    						System.out.println();
    						do {
    							combinaisonJoueur = choixNumerote();
    							for(int i=0;i<4;i++) {
    								System.out.print(combinaisonJoueur[i]+ "|");
    							}
    							//??
    						}while (compteur<=12 && (12-compteur)>0);
     
    						compareColor (tabJoueur, combination);
    					    for(int k=0;k<4;k++) {
    							System.out.print(tabCorrect[k]+ "|");
    						}
     
    						sc.close();
    				}
     
        // Generate random combination of 4 colors 
        static String [] generateRandomCombination() {
            String [] combination = new String[NB_COLORS];
            int currentPosition = 0;
            while(currentPosition!=NB_COLORS) {
                int indexRandom = (int)(Math.random()*TAB_REF_COLORS.length);
                String color = TAB_REF_COLORS[indexRandom];
                if(!isIn(color, combination)) {
                    combination[currentPosition] = color;
                    currentPosition++;
                }
            }
            return combination;
        }
     
        static boolean isIn(String iStringToFind, String [] iTab) {
            int size = iTab.length;
            for(int i=0;i<size;i++) {
                if(iStringToFind.equalsIgnoreCase(iTab[i])) return true;
            }
            return false;
        }
    // fonction pour récupérer le tableau de l'utilisateur
        static String [] choixNumerote() {
            String [] tabJoueur = new String[NB_COLORS];
            for(int i=0;i<NB_COLORS;i++) {
                System.out.println();
                System.out.println("Choisissez une couleur :");
                tabJoueur[i]=sc.nextLine();
            }
            compteur++;
    		System.out.println("Il te reste : " + (12-compteur) + " tentative(s)");
    		if (compteur==12) {
    			System.out.println("La partie est finie. Tu as perdu !");
     
    		}
    		return tabJoueur;
     
        }
     
    // fonction pour faire la comparaison entre les 2 tableaux et stocker les élément en communs
        static void compareColor (String [] tabPlayer, String [] tabOrdi) {
            int sizeTj = tabPlayer.length;
            int sizeTm = tabOrdi.length;
            int k=0;						
                    for (int i=0;i<sizeTj;i++) {
                        for(int j=0; j<sizeTm; j++) {
                            if (tabOrdi[j]==tabPlayer[i]) {
                            System.out.print("Vous avez trouver une couleur");
                            tabCorrect[k]=tabOrdi[j];
                            k++;
                        }
                            else {
                            	continue;
                            }
                        }
                }
     
            }
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Function that compares two arrays and stores the result in a third one

    What statement is at line 80?
    What variable in that statement has a null value?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jun 2020
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Function that compares two arrays and stores the result in a third one

    I make this for the statement :
            int sizeTj = 0;
            sizeTj = tabPlayer.length;
     
            int sizeTm = 0;
            sizeTm = tabOrdi.length;
     
            int k=0;
    but the problem remains. For me there are 2 variables is this statement and both of them are declared and initialisized. I really don't know.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Function that compares two arrays and stores the result in a third one

    Which statement is on line 80? I can not see where you have said what statement is at line 80?
    What variable on line 80 has the null value? Add a print statement just before line 80 that prints the value of all variables used on line 80.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jun 2020
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Function that compares two arrays and stores the result in a third one

    I think I see where the problem is. Just a minute please. Thank you very much

    --- Update ---

    No .. big deception. I've written this :

    static String [] tabPlayer = new String[NB_COLORS];
    static String [] tabOrdi = new String[NB_COLORS];
    .
    But doesn't work. I've even been skeptical about initializing funnction's argument

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Function that compares two arrays and stores the result in a third one

    But doesn't work.
    Please explain what does not work.


    The statement:
    String [] tabOrdi = new String[NB_COLORS];
    defines a String array but does not assign any values to the array's elements. They will be null. The code needs to use assignment statements to put values in the array:
    tabOrdi[0] = "a String";  // assign value to first array element
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Jun 2020
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Function that compares two arrays and stores the result in a third one

    "defines a String array but does not assign any values to the array's elements. " but I am defining the function. In the main, there will be the values of the array's elements, that are given by the previous return tabJoueur; and return combination; .... isn't right ?
    I removed the word static and renamed the variables as suggested by Eclipse.
    static void compareColor (String [] tabPlayer, String [] tabOrdi) {
        	String [] tabPlayer1 = new String[NB_COLORS];
        	String [] tabOrdi1 = new String[NB_COLORS];
    And it seems working ... at least I didn't have the NullPOinterException message anymore.

    Thank you Norm

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Function that compares two arrays and stores the result in a third one

    Your are welcome.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: September 30th, 2014, 09:29 AM
  2. Replies: 2
    Last Post: March 6th, 2014, 05:35 AM
  3. Program to compares two text files!
    By nWeid1 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 8th, 2013, 09:55 AM

Tags for this Thread