I was doing the love tester program for my assignment. Then, I've encounted some problems after I run the program.

Here is my code:

public class SEAN_LAB3 
{
    public static void main(String[] args) 
    {
        int i;
        String [] bName = new String [5];
        String [] gName = new String [5];
        char choice;

        System.out.println("*****************************************************");
        System.out.println("This program is used to randomly match boys and girls");
        System.out.println("*****************************************************\n");

        System.out.println("Please enter the names of 5 boys:");
        for (i=0; i<bName.length; i++)
        {
            bName[i] = scan.next();
        }

        System.out.println("Please enter the names of 5 girls:");
        for (i=0; i<gName.length; i++)
        {
            gName[i] = scan.next();
        }

        do
        {
            System.out.println("The result of the matching:");
            for (i=0; i<5; i++)
            {
                System.out.println(bName[(int)Math.random()*5] +" and "+ gName[(int)Math.random()*5]);
            }
            System.out.print("Do you want to re-match? (y/n): ");
            choice = charAt(0);
        } while (choice == 'y');

        System.out.println("Thank you for using the program.");
    }

}

Recommended Answers

All 17 Replies

I've encounted some problems after I run the program

You've been around here long enough to know that's useless as problem description!
EXACTLY what "problems"? Don't expect us to guess!

scan.next() and charAt(0)

Was that a joke?

If you have a compiler or runtime error message post the complete message plus the actual code it refers to. If your code is giving an incorrect result explain what result you get and what the correct result should be.

Scan doesn't seem to be even defined,
charAt is a method in String class, so you have to call it from a string object
choice = charAt(0); choice = STRING.charAt(Int);

The correct output will be shown as this:

The result of the matching:

Steven and Alice

Jonathan and Camilla

Billy and Ellie

William and Mandy

George and Lauren

Do you want to re-match? (y/n): y

Did you fix what I pointed you to ?

I tried, but it's still red wavy line appearing.

Show your current code, cant guess where your problem now is without looking into it

package sean_lab3;

public class SEAN_LAB3 
{
    public static void main(String[] args) 
    {
        int i;
        String [] bName = new String [5];
        String [] gName = new String [5];
        char choice;

        System.out.println("*****************************************************");
        System.out.println("This program is used to randomly match boys and girls");
        System.out.println("*****************************************************\n");

        System.out.println("Please enter the names of 5 boys:");
        for (i=0; i<bName.length; i++)
        {
            bName[i] = scan.next();
        }

        System.out.println("Please enter the names of 5 girls:");
        for (i=0; i<gName.length; i++)
        {
            gName[i] = scan.next();
        }

        do
        {
            System.out.println("The result of the matching:");
            for (i=0; i<5; i++)
            {
                System.out.println(bName[(int)(Math.random()*5)] +" and "+ gName[(int)(Math.random()*5)]);
            }
            System.out.print("Do you want to re-match? (y/n): ");
            choice = STRING.charAt(int);
        } while (choice == 'y');

        System.out.println("Thank you for using the program.");
    }

}

Try to answer this question?
What is scan from this line bName[i] = scan.next();?

To read the boy's name.

Right, but it is supposed to be an object of a class Scanner right? (Atleast I assume so by the name). I am pointing that in your code you don't define anywhere what scan is

New update. The code is now working. But the problem is the names are repeated in a pair. See line 36.

package sean_lab3;

import java.util.*;

public class SEAN_LAB3 
{
    public static void main(String[] args) 
    {
        Scanner scan = new Scanner(System.in);
        int i;
        String [] bName = new String [5];
        String [] gName = new String [5];
        String choice;

        System.out.println("*****************************************************");
        System.out.println("This program is used to randomly match boys and girls");
        System.out.println("*****************************************************\n");

        System.out.println("Please enter the names of 5 boys:");
        for (i=0; i<bName.length; i++)
        {
            bName[i] = scan.next();
        }

        System.out.println("\nPlease enter the names of 5 girls:");
        for (i=0; i<gName.length; i++)
        {
            gName[i] = scan.next();
        }

        do
        {
            System.out.println("\nThe result of the matching:");
            for (i=0; i<5; i++)
            {
                System.out.println(bName[(int)(Math.random()*5)] +" and "+ gName[(int)(Math.random()*5)]);
            }
            System.out.print("Do you want to re-match? (y/n): ");
            choice = scan.next();
        } while (choice == "n" && choice == "N");

        System.out.println("Thank you for using the program.");
    }

}

that is because you don't remove the names already linked from the list to choose from.

for (i=0; i<5; i++)
            {
                System.out.println(bName[(int)(Math.random()*5)] +" and "+ gName[(int)(Math.random()*5)]);
            }

for all you know, the random returns exactly the same each time. since you didn't remove the name after having matched it, it is still a valid result.

Just like for example, I input the names of 5 boys and 5 girls below.
BOYS: Sean, Eric, Mike, Rick, Leon
GIRLS: Sammie, Lily, Tiffany, Priscilla, Celine

The result should be like this:
Sean and Celine
Mike and Sammie
Leon and Tiffany
Rick and Priscilla
Eric and Lily

Is there a way not to repeat it?

yes. there is. either keep a list of already used indices for bName and gName, and don't print doubles, or remove the already used ones from the array (List might be better, then) and get a random then.
this is what I tried to explain in my previous post.

so:

indexB = (int)(Math.random()*5);
indexG = (int)(Math.random()*5);
while (indexB alreadyUsed) renewIndexB;
while (indexG alreadyUsed) renewIndexG;

matchBoyAndGirl;

this is a bit of pseudocode, for the simplest way to adapt your code

another solution is to randomly shuffle one or both of the arrays of names. Then you can simply pair up boy[0] with girl[0] etc

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.