0

I am trying to make a program that works as a logbook. The only problem I'm facing so far is the search function where the user should be able to search for a post, and then it prints it out. I am not sure what I have done wrong, and I have been looking through many posts. For your information the whole logbook is a list while every post is an array in this list. The search function is in case 3. The program is compiling, but the problem is when I search for an old post, then it only prints out System.String[]. I should also add that I want both the title and post connected to the title to be printed out when the user searches for the title of the post.

here is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Loggbok
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            bool running = true;//Ger ett booleskt värde till variabeln     running för att kunna skapa en loop
            List<string[]> loggbok = new List<string[]>();
            // int loggIndex = 0; // Används för att fylla Arrayen
            while (running)//Här skapas loopen
            {

            Console.WriteLine("\nVälkommen till loggboken!");
            Console.WriteLine("\n[1] Skriv ett inlägg");
            Console.WriteLine("[2] Skriv ut alla inlägg");
            Console.WriteLine("[3] Sök inlägg");
            Console.WriteLine("[4] Avsluta loggboken");
            Console.Write("\nVälj: ");

            int option;

            try
            {
                option = Int32.Parse(Console.ReadLine());
            }
            catch
            {
                Console.WriteLine("Fel, du får bara skriva in nummer");
                continue;
            }

            switch (option)
            {
                case 1:
                    string[] logg = new string[2];
                    Console.WriteLine("Ange en Titel");
                    logg[0] = Console.ReadLine();
                    Console.Clear();
                    Console.WriteLine("Ange text");
                    logg[1] = Console.ReadLine();
                    loggbok.Add(logg);
                    break;


                case 2:
                    foreach (string[] item in loggbok)
                    {
                        Console.WriteLine(item[0]);
                        Console.WriteLine(item[1]);
                    }
                    Console.ReadLine();
                    break;




                case 3:

                    Console.WriteLine("Skriv in ett ord du vill söka efter i loggboken");
                    var nyckelord = Console.ReadLine();
                    var entries = loggbok.Where(entry => entry.Contains(nyckelord));
                    foreach (var entry in entries)
                    {
                        Console.WriteLine(entry);
                    }
                    if (entries.Count() == 0)
                    {
                        Console.Write("Din sökning misslyckades...");
                    }
                    break;

                case 4:
                    running = false;
                    break;


            }
        }
    }
}

}

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
mackanmorre
  • 145
  • 1
  • 13
  • Change `Console.WriteLine(entry);` to `Console.WriteLine(entry[0]);` where 0 is the index of the element you want to retrieve in the array – Joshua Hysong Apr 17 '17 at 19:33

3 Answers3

0

but the problem is when I search for an old post, then it only prints out System.String[].

replace this:

Console.WriteLine(entry);

with this:

Console.WriteLine(string.Join(", ", entry));

the solution above uses a delimiter ( "," in this case) to separate each item within the array.

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
0

Based on your description, I think that "entry" in case 3 has the type: "string array" and not the type "string".

Sean Stayns
  • 4,082
  • 5
  • 25
  • 35
0

You state the problem as:

The program is compiling, but the problem is when I search for an old post, then it only prints out System.String[].

Looking at the code, it looks like this is expected, because you are calling Console.WriteLine on a String[]:

foreach (var entry in entries)
{
    // Below, 'entry' is a string array, which does not override the 
    // `ToString` method so it just prints out a description of the object
    Console.WriteLine(entry);  
}

If you want to output the entire array, you could simply loop through the inner array instead, like this:

foreach (var entry in entries)
{
    foreach (var item in entry)
    {
        Console.WriteLine(item);
    }
}

If you want to make your search results Case-Insensitive, you could do something like this:

var entries = loggbok.Where(entry => entry.Any(item =>
    item.IndexOf(nyckelord, StringComparison.OrdinalIgnoreCase) > -1));
Rufus L
  • 36,127
  • 5
  • 30
  • 43