Resolved Create list of numbers to be displayed as string

Paradoxz1

Member
Joined
Feb 8, 2021
Messages
14
Programming Experience
Beginner
Hey guys, I have been looking around for a while now but I can't find anything to help me with my issue. I'm trying to create a list that has weeks of the year in it 1-52 weeks in 1 year and then display the weeks in a formated string. So that every 3 weeks are displayed (Week 1 Week 4 Week 7 Week 10 etc all the day until the end of the year) my question is do I have to create a list with all these numbers in it or can I use some sort of Min / Max value in my list? And also what kind of list would I use, can someone provide an example? Since the numbers are ints but they would need to be displayed as strings?

I found a picture that resembles what I'm looking for but I did not manage to find any help related to the picture:

1613666263011.png


Thank you so much for any feedback on the issue.
 
Solution
Hopefully this will be a learning moment for you as to why "magic numbers" in code are bad. I followed a poor practice of using magic numbers in my code in post #10 and it obviously made things confusing. Let me try rewriting it to be clearer:
C#:
const int ColumnCount = 4;
const int LastColumn = ColumnCount - 1;

static void Main()
{
    for(int i = 1; i <= 52; i += 3)
    {
        var weekName = $"Week {i,2}";
        Console.Write($"{weekName,-10}");
        var columnNumber = i / 3;
        if (columnNumber % ColumnCount == LastColumn)
            Console.WriteLine();
    }
}
A list is one dimensional. What you seem to be looking for there is two dimensional, unless you don't care about wrapping.

Do you really need a list? Or do you just need to print out the week numbers? If the latter the following pseudo code seems to be sufficient for your needs:
C#:
int weekNumber = 1;
while weekNumber less than or equal to 52 do
    print "Week " + weekNumber
    weekNumber = weekNumber + 3
 
A list is one dimensional. What you seem to be looking for there is two dimensional, unless you don't care about wrapping.

Do you really need a list? Or do you just need to print out the week numbers? If the latter the following pseudo code seems to be sufficient for your needs:
C#:
int weekNumber = 1;
while weekNumber less than or equal to 52 do
    print "Week " + weekNumber
    weekNumber = weekNumber + 3
I think the code provided will suffice, the only issue I find now, is not really related to the same is but is more of a formatting issue. But I thought id ask since I have you on the post.
When I now Write my code to the console I get it displayed as following:

Week 1 Week 1 Week 1 Week 1 Week 1
Week 3 Week 3 Week 3 Week 3 Week 3
etc...
How would I go about formating this so it looks more like this:
Week 1 Week 3 Week 7 Week 10
Week 13 Week 16 Week 19 Week 22

And also have the "Week"s all aligned and rather the numbers 1-9 in one row and 10+ bumped one time to the left like so: -->
Week 1
Week 2
Week 10
Week 11

I have been looking at this (String.Format Method (System)) formating doc for help but I feel for every inch I scroll down I get more and more confused.
Edit: Thought Id add what I currently am working with, I know it ain't much but ye

Formating:
var nightsString = String.Format("{0,10}{0,20}{0,30}{0,40}\n", "Week " + nightsNumber);
Console.WriteLine(nightsString);
nightsNumber = nightsNumber + 5;
 
Last edited:
Show us your current code. It's not clear why you are getting:
C#:
Week 1 Week 1 Week 1 Week 1 Week 1
Week 3 Week 3 Week 3 Week 3 Week 3
etc...

The pseudo code to get the format you want is:
C#:
int weekNumber = 1;
while weekNumber less than or equal to 52 do
    print on current line "Week " + weekNumber
    weekNumber = weekNumber + 3
    if (weekNumber divided by 4) modulus 4 is equal to zero
        print a newline

As for putting padding before a number keep on reading: Control Spacing
 
Show us your current code. It's not clear why you are getting:
C#:
Week 1 Week 1 Week 1 Week 1 Week 1
Week 3 Week 3 Week 3 Week 3 Week 3
etc...

The pseudo code to get the format you want is:
C#:
int weekNumber = 1;
while weekNumber less than or equal to 52 do
    print on current line "Week " + weekNumber
    weekNumber = weekNumber + 3
    if (weekNumber divided by 4) modulus 4 is equal to zero
        print a newline

As for putting padding before a number keep on reading: Control Spacing
I edited my previous post but ill put it in again, its not much but ye this is it:

Formating:
int weekNumber = 1;
            while (weekNumber <= 52)
            {
                var weekString = String.Format("{0,10}{0,20}{0,30}{0,40}\n","Week "+ weekNumber);
                Console.WriteLine(weekString);
                //Console.WriteLine("Week " + weekNumber);
                weekNumber = weekNumber + 3;
            }
            }
 
Console.Write() prints out something without a newline. Console.WriteLine() prints out something followed by a newline.

Consider using weekString = String.Format("Week {0,2}", weekNumber).
 
Console.Write() prints out something without a newline. Console.WriteLine() prints out something followed by a newline.

Consider using weekString = String.Format("Week {0,2}", weekNumber).
Thank you that did help me align the numbers corresponding to single-digit and double digits, I have however since your post been trying to get them sorted in an order of 4-week displays per line but I honestly can not get it to work. I find it very hard to find tutorials and or help regarding this issue.
Week 1 Week 4 Week 7 Week 10
Week 11 Week 12 Week 13 Week 14

I tried using Console.Write and WriteLine but the issue still remains I need to try and get 4 Weeks next to each other and after those 4, jump down one line which I can just use WriteLine for and then continue with the next 4 Weeks until I get to the end of the year (Week 52).

If you have any further insight please let me know, if not thank you for all your help and since my original issue was solved ill mark this post as solved.
 
See the pseudo code in post #4. For line 3 to work, you should be using Write() so that you stay on the same line. So that means that you can use WriteLine() for line 6 to print a new line. (The reason why I am presenting pseudo code is because if I actually write C# code, I would be doing your homework for you and completely devaluing any learning that you may gain.)

Any which way, post your code so that we know what you are talking about when you are saying that something isn't working.

(As an aside, there is really no need to quote the post directly above yours, but if it makes you happy so be it.)
 
Thanks, it was never my intention to get anyone to do the work for me, I'm just having issues getting it in the format I want. I did try using Write instead of WriteLine but I'm not getting the results I want. I have been looking around and from what I can tell I need to add an integer for columns and use a if statement to do a WriteLine and then use Write for my "weekString".
This is what I'm currently getting:

1613741744917.png

The picture above is connected to the code bellow:

C#:
  while (weekNumber <= 52)
            {
                //var weekString = String.Format("Week {0,2} ", weekNumber);
                //var weekString = String.Format("{0,10}{0,20}{0,30}{0,40}\n","Week "+ weekNumber);
                //Console.Write(weekString);
                // weekNumber = weekNumber + 3;

                for (int i = 1; i <= 52; i += 3)
                {
                    int p = 0;
                    int columns = 4;

                    Console.Write("Week {0,2} ", i);
                    weekNumber = weekNumber + 3;

                    p++;
                    if ((p % columns == 0) && (p >= columns))
                        Console.WriteLine();
                }
            }

I have another part similar to the one above where I tried different code and get this result:
1613742070953.png

This is more along the lines of what I need, 4 colums but instead of the week repetaing itself in the same row I need the next week (4 diffrent weeks in the same row) 6 11 16 26 then jump down one row and continue with 31 36 41 46 and so on.

Code used for the imagie above is:

C#:
Console.WriteLine("This is the nights schedule!");
            int nightsNumber = 6;

            while (nightsNumber <= 52)
            {
                var nightsString = String.Format("{0,10}{0,20}{0,30}{0,40}\n", "Week " + nightsNumber);
                Console.WriteLine(nightsString);
                nightsNumber = nightsNumber + 5;
            }

As you can see I kinda need a combination of both as the last picture gets me the right structure but not the right content whilst the first picture gets me what I need in content but not structure. I think the problem lies somewhere in the for loop at line 8 or in the if statement bellow but I'm not sure how I should go about fixing it. Any insight would be greatly appreciated. And again I'm sorry if I came out as someone who just wanted answers, it's simply not true I want to learn and get better but I feel I learn a lot better when asking and communicating with people who know what they are doing and currently this is the only place I have found that, I am not able to get this kind of help anywhere.

Let me know if there was anything I missed that u guys need to know in order to help me with this issue ill try my best to include everything but as I said I'm new to this and don't know exactly how much and what code u want me to put in here.
 
You are obviously putting in the effort, so here's a freebie:
C#:
for(int i = 1; i <= 52; i += 3)
{
    var weekName = $"Week {i,2}";
    Console.Write($"{weekName,-10}");
    if ((i / 3) % 4 == 3)
        Console.WriteLine();
}
 
Thank you Skydiver. I just have one question regarding line 5, I'm not quite sure I have 100% understood, I changed the code to be suited for my second part where I increase the weeks every 5 instead of the previous 3.


C#:
for (int i = 6; i <=52; i += 5)
            {
                var nightsString = $"Week {i,2}";
                Console.Write($"{nightsString,-10}");
                if ((i / 5) % 11 == 5)
                    Console.WriteLine();
            }

Here I wasn't quite sure how to change the if statement but I think I got it working. I read this "For example 11/5 = 2 1/5, the whole part is 2 (2*5 = 10) and then there 1 left which is not divisible by 5. The value 1 is the remainder and thus 11 % 5 = 1. " to try and get a better understanding but it just confused me more from what I can tell if I were to implement this in my code I would go and do ( 6/5=1.2 and the whole part then be 1 (1*5=5) 1 left from 6 and so then 6 % 5 = 1? yet the code I'm writing (6/5)%11==5 I guess my question is what does this part with %11==5 mean. If you could somehow understand what I mean. I would greatly appreciate some help other than that you have been extremely helpful and I have learnt a lot so thanks! (I changed to code to that because it logically made sense after looking at the code u gave me but I'm not quite sure what exactly it does)
 
Hopefully this will be a learning moment for you as to why "magic numbers" in code are bad. I followed a poor practice of using magic numbers in my code in post #10 and it obviously made things confusing. Let me try rewriting it to be clearer:
C#:
const int ColumnCount = 4;
const int LastColumn = ColumnCount - 1;

static void Main()
{
    for(int i = 1; i <= 52; i += 3)
    {
        var weekName = $"Week {i,2}";
        Console.Write($"{weekName,-10}");
        var columnNumber = i / 3;
        if (columnNumber % ColumnCount == LastColumn)
            Console.WriteLine();
    }
}
 
Solution
Back
Top Bottom