0

I am new at C# and I´m having problem to understand what I am doing wrong in this code (case 3)? I have made some changes to the code, but now it wont find the posts, only the title. At least its only displaying the title. I am very greatful for any help I can get since ive been working on this for days now and it really drivs me crazy.

         List<String[]> minBlogg = new List<String[]>();

         //Deklarerar en bool som är true, blir den false, kommer while loop nedan brytas
         bool isRunning = true;

         /*1.Vill bara att Välkommen ska skrivas ut en gång och sätter den därför utanför loop
           2.n för ny rad, t för tabb*/
         Console.WriteLine("\n\tWelcome! ");

         //Deklarerar While. SÅ LÄNGE bool är true kommer while loop fortsätta.
         while (isRunning)
         { 

           /*1.n för ny rad, t för tabb
             2.Texten nedan skrivs ut som menyval*/
             Console.WriteLine("\n\tMake a choice 1-5: ");
             Console.WriteLine("\n\t[1]Create a new post");
             Console.WriteLine("\t[2]Show saved posts");
             Console.WriteLine("\t[3]Search for a post");
             Console.WriteLine("\t[4]Delete all posts");
             Console.WriteLine("\t[5]End program");

           /*1.Deklarerar en int för menyval
             2.Deklarerar en if, för att TryParse ska exekveras om användare skriver annat än 1-5
             3.TryParse returnerar bool true om TryParse lyckas.*/
             int menyVal;
             Int32.TryParse(Console.ReadLine(), out menyVal);

             //Deklarerar en switch för menyVal.
             switch (menyVal)
             {
                 case 1:
                     /*1.Skapar en case 1: start på första kodblocket i switchen
                       2.Initiering av strängvektor med 3 element, platser för: titel, inlägg, datum / tid.
                       3.Deklarerar strängvektorn efter whileloop för att jag vill att användare ska kunna spara flera inlägg,utan att det gamla inlägget skrivs över. Array loopas.
                       4.Skriver ut instruktion till användare.
                       5.Sparar titel i index 0 (första elementet)
                       6.Endast till för att göra texten mer lättöverskådlig
                       6.1.t för tabb n för ny rad*/
                     String[] Inlägg = new String[3];
                     Console.Write("\tEnter a title for your post: ");
                     Inlägg[0] = Console.ReadLine();
                     Console.WriteLine("\n\t-------------------\n\t");

                     /*1.Skriver ut instruktion till användare
                       1.1.Skriver ut instruktion till användare
                       2.Sparar inlägget i index 1 (andra elementet)*/
                     Console.WriteLine("\tWrite a post and press enter to save: ");

                     Inlägg[1] = Console.ReadLine();

                     //1.Sparar datum/tid i index 2 (tredje elementet)
                     Inlägg[2] = DateTime.Now.ToString();

                     /*1.Sparar titel,inlägg,tid i minBlogg med Addmetoden
                       2.Endast till för att göra texten mer lättöverskådlig
                       2.1.Skriver ut information till användaren
                       3.break; Slut på detta kodblock*/
                     minBlogg.Add(Inlägg);
                     Console.WriteLine("\n\t-------------------\n\t");
                     Console.WriteLine("Your post is now saved!");
                     break;

                 case 2:
                     /*1.case 2: start på andra kodblocket i switchen
                       2.Skriver ut information till användaren.
                       3.Console.WriteLine(----); Gör det mer överskådligt att läsa inläggen.
                        Nedan är dina skrivna inlägg
                          ----------------------
                         Datum/Tid: Titel: Inlägg
                       4.Foreach loopar igenom alla sparade inlägg i minBlogg*/
                     Console.WriteLine("\n Your posts: \n");
                     Console.WriteLine("\n\t-------------------\n\t");
                     foreach (string[] dittInlägg in minBlogg)

                         /*1.Skriver ut alla arrays: Datum/Tid: Titel: Inlägg: Lagt array 2 först eftersom jag vill ha ut datum och klockslag först.
                           2.break; Avslutar detta kodblock */
                         Console.WriteLine("\n Puplicerat: " + dittInlägg[2] + " \n Titel - " + dittInlägg[0] + " \n Inlägg - " + dittInlägg[1] + "\n");

                     break;

                 case 3:

                
                     //Ber användaren att göra en sökning.
                     Console.WriteLine("Search on title: \n");
                     //Skapar en ny sträng variabel som blir sökningen som matas in av användaren.
                     string sökord = Console.ReadLine();
                     //skapar ytterligare en bool för att göra det enklare för programmet att söka i listan efter det inmatade sökordet.
                     bool sökning = false;
                     //For loopen hjälper oss att bläddra genom varje element.

                     for (int i = 0; i < minBlogg.Count; i++)
                     {   // Använder mig utav en jämförelseoperator.
                         // Skapar en extra for loop för att loopa min inre array och göra det möjligt att hitta inlägg i alla positioner.
                         for (int j = 0; j < minBlogg[i].Length; j++)
                         {
                             if (minBlogg[i][j].ToUpper() == sökord.ToUpper())
                             {
                                 // Matchar sökordet och listan så får användaren följande meddelande utskrivet på skärmen.
                                 Console.WriteLine("We found this: " + minBlogg[i][0], minBlogg[i][1]);
                                 Console.WriteLine("\nMake a new choice in the menu:");
                                 Console.WriteLine("\n" + "_______________________________________" + "\n\t");
                                 sökning = true;

                             }
                             
                         }

                     }
                     

                     //Om sökordet inte hittas skrivs följande ut i konsollfönster.
                     //break slut på kodblock i kodblock
                     if (!sökning)
                     {
                         Console.WriteLine("\nWe couldn´t find your post, please try again. " + "\n");
                         break;
                     }
                     //slut på hela kodblocket
                     break;

  • how does the minBlogg array look like? – S. Stumm Dec 22 '21 at 01:30
  • 1
    If you'd supply a code sample where *everyone* could read the "We could not find your post" part I think you'd get a bit more help – riffnl Dec 22 '21 at 01:31
  • I have translated the text in all console.WriteLines in case 3: so that everyone can read it, and I posted the whole code. – user17735694 Dec 22 '21 at 01:58
  • If minBlogg is populated this should work. Could you share example data where it fails? Also maybe want to check if the title contains the searchword instead of comparing it directly? e.g. `if(minBlogg[i][0].ToUpper().Contains(sökord.ToUpper())` – S. Stumm Dec 22 '21 at 02:11

2 Answers2

0

I have tried to run your code and it's working as expected. we need to add couple of posts and try to search using titles. it is showing the search records. if i try to give invalid search data, it's showing like "we couldn't find post".

sample output of given code

0

It looks like the issue is with this line below;

Console.WriteLine("We found this: " + minBlogg[i][0], minBlogg[i][1]);

You are adding the title (minBlogg[i][0]) to the value but passing the post (minBlogg[i][1]) as a parameter. This Console.WriteLine overload uses composite formatting feature and needs a formatted value to be able to include the given parameters in the output. You can read more about the Console.WriteLine here, and also about the Composite Formatting here.

aozogul
  • 571
  • 5
  • 8