C
C#2y ago
Joriku

❔ .NET Console app, issue with array

So, I tried my best to comment on the issue I am having.. As of now, I have no idea what to do, but I'll continue looking into this.. Please, explain what I could do and a path for solving it and not directly writing the solvation for me
12 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Joriku
JorikuOP2y ago
Here, so I am trying to check if the array list called objectsVector is empty, and if so. Print out the message or a message saying it's currently empty. If it contains any userInputs then print it/them out
else if(userInput == 2)
{
// Print out the names inside array stored by userInput using foreach
Console.Clear();
Console.WriteLine("Current objects inside the backpack are:");
Thread.Sleep(500);
foreach (var items in objectsVector)
// Issue: checking if vector is empty, then display slot is empty.
if(objectsVector == null)
{
Console.WriteLine("Slot is currently empty.");
}
else
{
// Issue, printing out message 5 times
Console.WriteLine("Current items inside the backpack are:");
Console.WriteLine(items);
}
}
else if(userInput == 2)
{
// Print out the names inside array stored by userInput using foreach
Console.Clear();
Console.WriteLine("Current objects inside the backpack are:");
Thread.Sleep(500);
foreach (var items in objectsVector)
// Issue: checking if vector is empty, then display slot is empty.
if(objectsVector == null)
{
Console.WriteLine("Slot is currently empty.");
}
else
{
// Issue, printing out message 5 times
Console.WriteLine("Current items inside the backpack are:");
Console.WriteLine(items);
}
}
Forgot to reply to the message itself
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Joriku
JorikuOP2y ago
Any hint would be Laugh
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Joriku
JorikuOP2y ago
Joriku
JorikuOP2y ago
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Vektorer_och_sökning
{
internal class Program
{
static void Main(string[] args)
{
// Array backpack, storing up to 5 objects. Written by Anthony Persson. NTI Skolan.
string[] objectsVector = new string[5];

bool loop = true;

while(loop)
{
Console.WriteLine("Updated backpack");
Console.WriteLine("-----------------");
Console.WriteLine("[1] Add");
Console.WriteLine("[2] Inspect backpack");
Console.WriteLine("[3] Search object");
Console.WriteLine("[4] Exit");

if (Int32.TryParse(Console.ReadLine(), out int userMenuInput))
{
switch (userMenuInput)
{
case 1:
// Add objects into array
for(int i = 0; i < 5; i++)
{
Console.WriteLine("Name an object: ");
objectsVector[i] = Console.ReadLine();
Console.WriteLine("-----------------");
Console.WriteLine("Object has been added inside Index: " + i);
Console.WriteLine("-----------------");
}
break;
case 2:
// Inspect current items, display added items
foreach(var item in objectsVector)
{
// Issue, can't find a way to display text: no items if no items exists
Console.WriteLine(item);
}
break;
case 3:
// Search for stored/added word
Console.Clear();
Thread.Sleep(250);
Console.WriteLine("This is a search function, please. Tell me what you're looking for:");

// Prevent a non-string crash issue
string searchWord = Console.ReadLine();

bool foundWord = false;

for (int i = 0; i < objectsVector.Length; i++)
{
if (searchWord.ToUpper() == objectsVector[i].ToUpper())
{
foundWord = true;
Console.WriteLine("The name exists inside the vector at index " + i + "!");
}
}

if (foundWord == false)
{
Console.WriteLine("Word was not found.");
}
break;
case 4:
// Exit application
loop = false;
break;
}
}
else
{
// Wrong input
Console.Clear();
Console.WriteLine("Sorry, I did not understand.");
Console.WriteLine("----------------------------");
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Vektorer_och_sökning
{
internal class Program
{
static void Main(string[] args)
{
// Array backpack, storing up to 5 objects. Written by Anthony Persson. NTI Skolan.
string[] objectsVector = new string[5];

bool loop = true;

while(loop)
{
Console.WriteLine("Updated backpack");
Console.WriteLine("-----------------");
Console.WriteLine("[1] Add");
Console.WriteLine("[2] Inspect backpack");
Console.WriteLine("[3] Search object");
Console.WriteLine("[4] Exit");

if (Int32.TryParse(Console.ReadLine(), out int userMenuInput))
{
switch (userMenuInput)
{
case 1:
// Add objects into array
for(int i = 0; i < 5; i++)
{
Console.WriteLine("Name an object: ");
objectsVector[i] = Console.ReadLine();
Console.WriteLine("-----------------");
Console.WriteLine("Object has been added inside Index: " + i);
Console.WriteLine("-----------------");
}
break;
case 2:
// Inspect current items, display added items
foreach(var item in objectsVector)
{
// Issue, can't find a way to display text: no items if no items exists
Console.WriteLine(item);
}
break;
case 3:
// Search for stored/added word
Console.Clear();
Thread.Sleep(250);
Console.WriteLine("This is a search function, please. Tell me what you're looking for:");

// Prevent a non-string crash issue
string searchWord = Console.ReadLine();

bool foundWord = false;

for (int i = 0; i < objectsVector.Length; i++)
{
if (searchWord.ToUpper() == objectsVector[i].ToUpper())
{
foundWord = true;
Console.WriteLine("The name exists inside the vector at index " + i + "!");
}
}

if (foundWord == false)
{
Console.WriteLine("Word was not found.");
}
break;
case 4:
// Exit application
loop = false;
break;
}
}
else
{
// Wrong input
Console.Clear();
Console.WriteLine("Sorry, I did not understand.");
Console.WriteLine("----------------------------");
}
}
}
}
}
Update, skipping a check for it.. Got a question: TryParse, try catch or whatever, how can I write it here. Finding no solution
// Prevent a non-string crash issue
string searchWord = Console.ReadLine();
// Prevent a non-string crash issue
string searchWord = Console.ReadLine();
phaseshift
phaseshift2y ago
How does anything go wrong with that?
Joriku
JorikuOP2y ago
I've been working on it for 11 hours straight, for some reason and somehow. I managed to make it crash using that.. then after I gave up, I simply reverted it to this and it.. well, it worked.. harold
Buddy
Buddy2y ago
ReadLine() can return null if someone pressed CTRL + Z followed by enter But other than that, it won't return null. Or at least shouldn't.
Haze.
Haze.2y ago
Why are you checking if the item length is greater than 0? Are you not trying to check the array length?
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server