KotoriItsukaa
KotoriItsukaa

Reputation: 15

How can I change this code so that it will stop looping but display the same thing?

I'm trying to code my program so that when the array compartment entered is occupied , it will tell the user which compartments is empty . However , I have no idea how to change the code so that it will stop looping yet display the same things . This is my code :

                for (int i = 0; i < compartmentno.Length; i++)
                {
                    if (compartmentno[i] == 0)
                    {
                        MessageBox.Show("Available compartments are" + (i + 1).ToString());
                    }
                }

How do I make it so that the program just shows a messagebox with all the available compartments instead of showing 10 messagebox ? Thanks in advance for the help !

Upvotes: 1

Views: 60

Answers (6)

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

You can use LINQ for it:

int availableCompartments = compartmentno.Count(x=> x == 0);

MessageBox.Show("Available compartments are:" + availableCompartments.ToString());

WORKING FIDDLE EXAMPLE

Here is with Apartment Numbers :

public static void Main()
{

  int [] compartmentNo = { 1,3,5,6,7,8,9,0,4,0};


  var availableCompartments = compartmentNo
                             .Select((value, index) => new {index, value})
                             .Where(x=> x.value == 0)
                             .Select(x=>x.index);

  int Count = availableCompartments.Count();

  string Values = String.Join(",",availableCompartments);

  Console.WriteLine(String.Format("No of Aparments : {0} and aprtments No : {1}",Count,Values));


}

UPDATED FIDDLE DEMO

Upvotes: 2

Peter
Peter

Reputation: 7804

Do it in one line of code with this. :)

It uses the not so known about indexer of the Linq Select statement

MessageBox.Show(String.Format("Available compartments are {0}", String.Join(", ", compartmentno.Select((s, i) => new {i, s}).Where(p=>p.s == 0).Select(p=>p.i +1))));

Upvotes: 0

Mat J
Mat J

Reputation: 5632

You can use linq for that.

var avaiableItems= string.Join(",", compartmentno.Where(x => x == 0).Select((item, index) => index+1));
MessageBox.Show(string.Format("Available compartments are {0}",avaiableItems));

Upvotes: 0

Piyush Parashar
Piyush Parashar

Reputation: 874

You can add all the compartments in one string and show message outside of the loop.

using System.Text;

StringBuilder compartments = new StringBuilder();
for (int i = 0; i < compartmentno.Length; )
                {
                    if (compartmentno[i] == 0)
                    {
                        compartments.Append(++i.ToString() + ", ");

                    }
                }

MessageBox.Show("Available compartments are" + compartments.ToString());

Upvotes: 1

Manwal
Manwal

Reputation: 23816

Note: You have to use same loop for this but instead of showing message every time you can show it once after manipulation.

Use following code:

int avli = 0;
for (int i = 0; i < compartmentno.Length; i++)
{
    if (compartmentno[i] == 0)
    {
        avli++;
    }
}

MessageBox.Show("Available compartments are" + avli.ToString());

Upvotes: 0

Greg the Incredulous
Greg the Incredulous

Reputation: 1836

Try this:

int availableCompartments = 0;
for (int i = 0; i < compartmentno.Length; i++)
                {
                    if (compartmentno[i] == 0)
                    {
                        availableCompartments++;
                    }
                }
MessageBox.Show("Available compartments are " + availableCompartments.ToString());

Note that this will only show you how many compartments are available, not which ones.

Upvotes: 0

Related Questions