5

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!
3
  • 4
    Edit your question please. Highlight your code and press the Code Sample button Commented Feb 25, 2010 at 6:22
  • As @ItzWarty says, using string.Format is redundant with Console.Write/WriteLine; Commented Feb 25, 2010 at 6:30
  • 1
    while(true) { ... break; ... break; } is absolutely identical in structure, intent, and code quality to 2 conditional gotos and a looping, non-conditional goto. It's spaghetti code, and will have all the problems of spaghetti code when it comes to maintenance, debugging, understandability, and obviousness of correctness. Kurt at least is a beginner, but any experienced coder should have the same apprehension about this control structure pattern as they would for a live grenade thrown into their office. Commented Feb 25, 2010 at 12:40

8 Answers 8

8

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");
Sign up to request clarification or add additional context in comments.

8 Comments

...and output the "Checking data" part outside (above) your loop.
You made it Brian! Exactly that's what i want to achieve. Thank you so much guys. @ Scott, thank you man.
@kurt: Glad I could help. You should accept one of the answers then.
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 found 4. Checking data 1...2...3... Not Found!
-1: Sorry, I can't condone any answer that perpetuates that dreadful while(true)
|
4

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

1 Comment

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
1
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 ") );
}

Comments

1

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;

}

Comments

1

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

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);
}

3 Comments

If you're going to be appending to a string, you should use a StringBuilder.
@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: 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. :)
0

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

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

Comments

0

You have to use

Console.Write()

Comments

-1

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);

2 Comments

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
You're welcome. I have modified my code to fit your request too.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.