question

PlaystationGamer-8084 avatar image
0 Votes"
PlaystationGamer-8084 asked PlaystationGamer-8084 commented

Join two different string arrays in C#

Hi. I'd like to see if I can join two different string arrays together.

I have these two strings:
string[] AdditionalExtras = new[] { "1 - 16” Alloys", "2 - Privacy Glass", "3 - Spare Wheel", "4 - Parking Pack", "5 - SYNC 3 with Navigation", "6 - Winter Pack", "7 - Floor mats", "8 - Boot liner", "9 - Dashboard camera", "10 - Pet travel mat" }; // Any additional extras will be displayed in the array
string[] ExtrasCost = new[] { "£300", "£250", "£150", "£600", "£600", "£350", "£70", "£61.69", "£284.36", "£264.50" }; // Cost of each extra item

And, I would like them to be displayed like this (if possible):
16” Alloys - £300
Privacy Glass - £250

The prices are already laid out to correspond with each number within the 'AdditionalExtras' array, if that makes sense. So, item 1 will cost £300, item 2 will cost £250 and so on...

To clarify: If the user enters 1 and 2, I would only like those items to be displayed, not the whole list!

I did try to work out a solution on my own, however, nothing I could come with seems to work (my screenshots below should show this). I also looked online for some solutions, but didn't find anything that would've helped me.

Note: I have asked a previous question based on the first array, which can be found here, so I have got the first array working and display how I want to (my code is pretty similar to the answer that was provided).

199967-screenshot-2022-05-08-113410.png

199983-screenshot-2022-05-08-113633.png

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JackJJun-MSFT avatar image
1 Vote"
JackJJun-MSFT answered PlaystationGamer-8084 commented

@PlaystationGamer-8084, Welcome to Microsoft Q&A, based on my test, only a few changes will get what you wanted.

Please change the following code:

 while (true)
             {
                 Console.WriteLine("Please select any of the extras that you would like to purchase!");
    
                 int ExtraSelection = Convert.ToInt32(Console.ReadLine());
    
                 if (dic.ContainsKey(ExtraSelection))
                 {
                     list.Add(dic[ExtraSelection]+" ---- "+ ExtrasCost[ExtraSelection-1]);// here you need to add
                 }
                 else
                 {
                     Console.WriteLine("Please input the correct number");
                 }
                 ConsoleKeyInfo c = Console.ReadKey();
                 if (c.Key == ConsoleKey.Spacebar)
                 {
                     break;
                 }
    
             }

Tested result:
200234-image.png


Best Regards,
Jack




If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



image.png (20.5 KiB)
image.png (16.1 KiB)
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi, Jack.

I have just tested this and, although it does work how I'd like it to, the prices are incorrect (see my screenshot below), and I've tried to fix this (mainly by trying to add the separate array index), but nothing I've done works. By the looks of my testing, the first price is completely ignored, which would mean that if option 10 was selected, there would be no price for it.

Basically, the price in the 'ExtrasCost' array should correspond to the numbers in the 'AdditionalExtras' array. To help, here are the extras with their correct prices:

16” Alloys ---- £300
Privacy Glass ---- £250
Spare Wheel ---- £150
Parking Pack ---- £600
SYNC 3 with Navigation ---- £600
Winter Pack ---- £350
Floor mats ---- £70
Boot liner ---- £61.69
Dashboard camera ---- £284.36
Pet travel mat ---- £264.50

Maybe I'm just missing something really obvious, which is most likely the case.

Regards

200252-screenshot-2022-05-09-095927.png

0 Votes 0 ·
JackJJun-MSFT avatar image JackJJun-MSFT PlaystationGamer-8084 ·

@PlaystationGamer-8084, thanks for the feedback, I forget that the arr index should be ExtraSelection-1. Now I have update code example and you could have a check.
,

0 Votes 0 ·

Thank you, this has worked a treat!

0 Votes 0 ·
Viorel-1 avatar image
0 Votes"
Viorel-1 answered PlaystationGamer-8084 edited

Try this code:

 var result = AdditionalExtras.Zip( ExtrasCost, ( e, c ) => $"{e} - {c}" );
    
 foreach( var r in result )
 {
     Console.WriteLine( r );
 }

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@Viorel-1

Hi, I've tried this and it just displays the whole array - see screenshot below, (but the way it is displayed is how I'd like it), even if, for example, only 2 of the extras are entered. I would like it to only display the extras that are entered into the new list ('ExtrasList').

So, in that screenshot, since options 1 and 2 were entered, only those options should appear in the purchase list. Does that make sense? Is that possible?

199969-screenshot-2022-05-08-132617.png

0 Votes 0 ·
karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered PlaystationGamer-8084 commented

Using arrays is really not the path to take, instead use classes/models e.g. the following is a conceptual starter to expand on as needed. Data should not be stored in code but instead either comma-delimited text file, json file or database.

 public class Operations
 {
     public static List<Extra> AdditionalItems = new();
     public static string FileName => "TODO";
     public static void Add(Extra sender)
     {
         AdditionalItems.Add(sender);
     }
     public static void Remove(Extra sender)
     {
         AdditionalItems.Remove(sender);
     }
    
     public static void Clear()
     {
         AdditionalItems.Clear();
     }
    
     public static void Save()
     {
         // save AdditionalItems if needed using FileName
     }
    
     public static void ReadFromFile()
     {
         // read from FileName into AdditionalItems
     }
 }
    
 public class Extra
 {
     public int Id { get; set; }
     public string ItemName { get; set; }
     public decimal Cost { get; set; }
     public int Quantity { get; set; }
     public override string ToString() => ItemName;
    
 }


In regards to the menu check out Spectre.Console which provides a wealth of methods to work with console projects.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi there.

I appreciate the help, however, I don't really want to be using classes and models for this program that I am creating. I also don't want to be using libraries to edit things to do with the console.

Throughout the project I have been creating, I have used a mixture of arrays, lists and case statements to get everything working up to this point.

The previous answer does provide the output the way I'd like, however, it displays the whole list, when in fact it should take into account what numbers the user has input, and only display those items.

Maybe doing it as an array isn't the best way, but, due to the fact that I'm still far from an efficient coder, and I have a lot of lines currently, it just saves that little space.

But regardless, I thank you for the suggestion.

0 Votes 0 ·