Lauren Lauren - 1 month ago 7
Vb.net Question

How do I check if two string arrays are equal?

I'm trying to make a classification program for different animals. Below is my code (with only one animal as an example). The program is meant to take in user inputs of their answers to each question and then add them to an array (answerList). In this example, if

answerList
(the array containing all the user's inputs) is equal to the
Horse
array, the user will be told their animal is a horse.

However, the last
four lines
of code doesn't work. It produces no final output. Is there anyway to get my below code to work?

Sub Main()
Dim aAquatic, aInsect, aTentacles, aBird, aFlippers, aFly, aHoney, aLegs, aFeline, aStripes, aDomestic, aMilk, aWool, aSnout
Dim Horse = {“n”, “n”, “n”, “n”, “n,”, "n", “n”, “n”, “n”, “n”, “n”, “n”, “n”, “n”}
Dim animalArray = {Horse, Cow, Sheep, Pig, Dog, Cat, Lion, Tiger, Dolphin, Seal, Penguin, Ostrich, Sparrow, Bee, Wasp, Termite, Octopus}
Console.WriteLine("Welcome to the classification. Choose one of these animals and I will attempt to guess it: horse, cow, sheep, pig, dog, cat, lion, tiger, dolphin, seal, penguin, ostrich, sparrow, bee, wasp, termite, octopus. Click enter to continue.")
Console.ReadLine()
Console.WriteLine("Is your animal aquatic? (y or n)")
aAquatic = CStr(Console.ReadLine())
If aAquatic = "y" Then
answerList.Add("y")
ElseIf aAquatic = "n" Then
answerList.Add("n")
End If
Console.WriteLine("Is your animal an insect? (y or n)")
aInsect = CStr(Console.ReadLine())
If aInsect = "y" Then
answerList.Add("y")
ElseIf aInsect = "n" Then
answerList.Add("n")
End If
Console.WriteLine("Does your animal have tentacles? (y or n)")
aTentacles = CStr(Console.ReadLine())
If aTentacles = "y" Then
answerList.Add("y")
ElseIf aTentacles = "n" Then
answerList.Add("n")
End If
Console.WriteLine("Is your animal a bird? (y or n)")
aBird = CStr(Console.ReadLine())
If aBird = "y" Then
answerList.Add("y")
ElseIf aBird = "n" Then
answerList.Add("n")
End If
Console.WriteLine("Does your animal have flippers? (y or n)")
aFlippers = CStr(Console.ReadLine())
If aFlippers = "y" Then
answerList.Add("y")
ElseIf aFlippers = "n" Then
answerList.Add("n")
End If
Console.WriteLine("Can your animal fly? (y or n)")
aFly = CStr(Console.ReadLine())
If aFly = "y" Then
answerList.Add("y")
ElseIf aFly = "n" Then
answerList.Add("n")
End If
Console.WriteLine("Can your animal make honey? (y or n)")
aHoney = CStr(Console.ReadLine())
If aHoney = "y" Then
answerList.Add("y")
ElseIf aHoney = "n" Then
answerList.Add("n")
End If
Console.WriteLine("Does your animal have two legs? (y or n)")
aLegs = CStr(Console.ReadLine())
If aLegs = "y" Then
answerList.Add("y")
ElseIf aLegs = "n" Then
answerList.Add("n")
End If
Console.WriteLine("Is your animal a feline? (y or n)")
aFeline = CStr(Console.ReadLine())
If aFeline = "y" Then
answerList.Add("y")
ElseIf aFeline = "n" Then
answerList.Add("n")
End If
Console.WriteLine("Does your animal have stripes? (y or n)")
aStripes = CStr(Console.ReadLine())
If aStripes = "y" Then
answerList.Add("y")
ElseIf aStripes = "n" Then
answerList.Add("n")
End If
Console.WriteLine("Is your animal a domestic pet? (y or n)")
aDomestic = CStr(Console.ReadLine())
If aDomestic = "y" Then
answerList.Add("y")
ElseIf aDomestic = "n" Then
answerList.Add("n")
End If
Console.WriteLine("Does your animal produce milk we can drink? (y or n)")
aMilk = CStr(Console.ReadLine())
If aMilk = "y" Then
answerList.Add("y")
ElseIf aMilk = "n" Then
answerList.Add("n")
End If
Console.WriteLine("Does your animal have wool? (y or n)")
aWool = CStr(Console.ReadLine())
If aWool = "y" Then
answerList.Add("y")
ElseIf aWool = "n" Then
answerList.Add("n")
End If
Console.WriteLine("Does your animal have a snout? (y or n)")
aSnout = CStr(Console.ReadLine())
If aSnout = "y" Then
answerList.Add("y")
ElseIf aSnout = "n" Then
answerList.Add("n")
End If
Console.ReadKey()

String.Join(", ", answerList)
String.Join(", ", Horse)
If answerList Is Horse Then
Console.WriteLine("Your animal is a horse.")
End If

Answer

Actually I´m not verry familiar with VB.Net, but shouldn´t it be:

dim s1 as String = String.Join(", ", answerList)
dim s2 as String = String.Join(", ", Horse)
If s1 = s2 Then
  Console.WriteLine("Your animal is a horse.")
End If

And also I can´t find the declaration of answerList:

dim answerList as new List(Of String)

Hope I could help.

Comments