tags:

views:

260

answers:

8

I have a litte problem on Console.WriteLine(). I have my while(true) loop that would check the data if exist and it would allow checking the data 3 times. And inside my loop I have this message:

Console.WriteLine(string.format("Checking data {0}",ctr));

Here's my sample code below:

int ctr = 0;

while(true)
{
  ctr += 1;

  Console.WriteLine(string.format("Checking data {0}...",ctr))  

  if(File.Exist(fileName)) 
     break;

  if(ctr > 3)
     break;

}

Let's assume that data was not found.

My current output show like this:

Checking data 1...
Checking data 2...
Checking data 3...

But the correct output that I should want to achieve should looks like this:

Checking data 1...2...3...

I would show only in one line.

Edit:

I forgot: In addition to my problem I want to append "Not Found" and "Found".

Here's the sample Output:

  1. if data found on the First loop output looks like this.

    Checking data 1... Found!

  2. if data found on the Second loop output looks like this.

    Checking data 1...2...Found!

  3. if data found on the Third loop output looks like this.

    Checking data 1...2...3...Found!

AND If data not found

  1. Checking data 1...2...3... Not Found!
+7  A: 

Use Console.Write instead if you don't want the line break. You also need to move the text out of the loop to avoid repeating it. Something like

Console.WriteLine("Checking data");
int ctr = 0;
bool found = false; 

while (ctr++ < 3 && !found) {
   Console.Write(" {0}...", ctr);
   if (File.Exists(fileName)) found = true;
}
Console.WriteLine(found ? "Found" : "Not found");
Brian Rasmussen
...and output the "Checking data" part outside (above) your loop.
Scott Smith
I was editing while you typed your comment :)
Brian Rasmussen
You made it Brian! Exactly that's what i want to achieve. Thank you so much guys. @ Scott, thank you man.
kurt_jackson19
@kurt: Glad I could help. You should accept one of the answers then.
Brian Rasmussen
Hi again guys,I forgot , In addition to my problem I want to append "Not Found" and "Found"Here's the sample Output: 1.if data found on the First loop output looks like this.Checking data 1... Found!2.if data found on the Second loop output looks like this.Checking data 1...2...Found!3.if data found on the Third loop output looks like this.Checking data 1...2...3...Found!AND If data not found4. Checking data 1...2...3... Not Found!
kurt_jackson19
Please leave comment when down voting ...
Brian Rasmussen
-1: Sorry, I can't condone any answer that perpetuates that dreadful `while(true)`
Binary Worrier
@Binary Worrier: I don't like that either, but it wasn't really part of the problem, so I just took the code from the OP.
Brian Rasmussen
@Brian Rasmussen: Yes, that's what I assumed. No excuse IMHO.
Binary Worrier
@Binary Worrier: Well then, I cleaned it up a bit (there was a typo in the original code as well, while(true) wasn't the biggest problem).
Brian Rasmussen
@Brian: This is true mate, downvote removed.
Binary Worrier
@Kurt: updated code based on your comment.
Brian Rasmussen
A: 

Use Console.Write() to avoid appending the new line.

To prevent "Checking data" being printed more than once, move it out of the loop.

Simon Nickerson
A: 

Try this code:

 int ctr = 0;
 string result = "Checking data ";
 while(true) {

     ctr += 1;

     result += ctr.ToString() + "...";

     if(File.Exist(fileName))
     {
         result += "Found";
         break;
     }

     if(ctr > 3)
     {
         result += "Not Found";
         break;
     }
 }
 Console.WriteLine(result);
madatanic
I tried your code Madatanic and it works! thank you man! Those who contributed to my question thank you so much guys for the quick response... Regards,kurt
kurt_jackson19
You're welcome. I have modified my code to fit your request too.
madatanic
+1  A: 

You can output "Checking data" before the while loop. So the code will be like this:

Console.Write("Checking data ")
int ctr = 0;
while(true) {

    ctr += 1;

    Console.Write(string.format("{0}...",ctr))

    if (File.Exist(fileName)) break;

    if (ctr > 3) break;

}
scatman
+3  A: 

Sidenote:
instead of using Console.WriteLine(string.format("Checking data {0}...",ctr));
you could use Console.WriteLine("Checking data {0}...",ctr); which in my opinion, is easier to read

ItzWarty
how is that?Both are same for me except for an extra API call which is an ovehead.Instead the writeln internally does whtt you are inteding to do.i think u r accustomed to string.format which is why u are marketing it:) just kidding
Ravisha
+1  A: 

A better approach (at least I feel so:):) ) with reduced condition check:

Enter code here:

    public static void Main(string[] args)
    {
        int ctr = 0;
        string fileName = args[0];
        string result = "Checking data ";
        do
        {
            ctr += 1;
            result += ctr.ToString() + "...";
        }
        while(!File.Exists(fileName) && ctr <= 3);
        Console.WriteLine(result);
    }
Ravisha
If you're going to be appending to a string, you should use a StringBuilder.
Bernhard Hofmann
@Bernhard Hofmann: In this instance? No, a string builder is not necessary. However no upvote either because building the result string here is completely unecessarry.
Binary Worrier
@Binary Worrier: I just don't like examples that don't show the correct way to do things. It's the MS examples without error or exception handling that leads to so much poor production code. Just like to remind people from time to time. :)
Bernhard Hofmann
A: 

You have to use

Console.Write()
Nasser Hadjloo
+1  A: 
public static void Main(string[] args)
{
    int retries = 0;
    bool success = false;
    int maxRetries = 3;
    string fileName = args[0];

    Console.Write("Checking data ");

    while(!success && retries++ < maxRetries)
    {
        Console.Write("{0}...", retries);
        success = File.Exists(fileName);
    }
    Console.WriteLine(" {0}Found!", (success ? "" : "Not ") );
}
Wedge
+1 for getting rid of that dreadful while(true)
Binary Worrier