xxx_zzzz
xxx_zzzz

Reputation: 117

Problems with console output in C#

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!

Upvotes: 5

Views: 529

Answers (8)

Ravisha
Ravisha

Reputation: 3343

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

Upvotes: 1

madatanic
madatanic

Reputation: 1790

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

Upvotes: -1

scatman
scatman

Reputation: 14555

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;

}

Upvotes: 1

Brian Rasmussen
Brian Rasmussen

Reputation: 116401

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

Upvotes: 8

Wedge
Wedge

Reputation: 19805

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

Upvotes: 1

Nasser Hadjloo
Nasser Hadjloo

Reputation: 12610

You have to use

Console.Write()

Upvotes: 0

Warty
Warty

Reputation: 7395

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

Upvotes: 4

Simon Nickerson
Simon Nickerson

Reputation: 43159

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

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

Upvotes: 0

Related Questions