Thank you. This solved the issue.Read the documentation...
![]()
Divide strings using String.Split - C#
The Split method returns an array of strings split from a set of delimiters. It's an easy way to extract substrings from a string.docs.microsoft.com
The program works correctly now though I need to improve the presentation of the runtime options to make it easy for the end users. using BREAKPOINTS with DEBUGGER saved my life.Thank you. This solved the issue.
string[] args = {"", "", "" };
args[0] = "foo";
args[1] = "fi";
args[2] = "fum";
static void Main(string[] args)
{
String inputImagePath = "";
String outputImagePath = "";
int imageWidth = 0;
int imageHeight = 0;
int redThreshold = -1;
int greenThreshold = -1;
int blueThreshold = -1;
Console.WriteLine("Welcome to Binary Image Maker");
Console.WriteLine("\nUse following command for help:");
Console.WriteLine("dotnet ImageBinarizerApp -help");
args = new String[] { Console.ReadLine() };
//Test if necessary input arguments were supplied.
if (args.Length < 8)
{
if (args.Length == 1 && args[0].Equals("-help"))
{
Console.WriteLine("\nHelp:");
Console.WriteLine("\nPass the arguments as following:");
Console.WriteLine("\nExample with automatic RGB:\ndotnet ImageBinarizerApp --input-image c:\\a.png --output-image c:\\out.txt -width 32 -height 32");
Console.WriteLine("\nExample with explicit RGB:\ndotnet ImageBinarizerApp --input-image c:\\a.png --output-image c:\\out.txt -width 32 -height 32 -red 100 -green 100 -blue 100");
}
else
{
Console.WriteLine("\nError: All necessary arguments are not passed. Please pass the arguments first.");
Console.WriteLine("\nPress any key to exit the application.");
Console.ReadLine();
return;
}
args = new String[] { Console.ReadLine() };
Thank you so much for the help. I really appreciate. Honestly, I am new to programming. I am yet to resolve the issue raised on #24. Am going through the documentation to get a better understanding. I used the debugger as advised and It helped me to properly re-assign the scope of the if-else construct.Can you please post your current updated code?
Let this be a lesson, that the documentation is there for a reason. Let it also be noted that you should not be re-initialising variables with the new operator because doing so changes the instance in which you were previously using. You also need to understand why this was an issue, which you've completely avoided answering or explaining in your latest replies. You also didn't need to use split() but I understand why Skydiver linked you it, as we discussed this offline. What you should have been doing was setting the parameters of your string[] args like so :
I don't know how you didn't snag any of this from previous examples. However, if you resolved this issue, you should address post 24 next.C#:string[] args = {"", "", "" }; args[0] = "foo"; args[1] = "fi"; args[2] = "fum";
You should take note of the highlighted lines in the spoiler of your code.C#:static void Main(string[] args) { String inputImagePath = ""; String outputImagePath = ""; int imageWidth = 0; int imageHeight = 0; int redThreshold = -1; int greenThreshold = -1; int blueThreshold = -1; Console.WriteLine("Welcome to Binary Image Maker"); Console.WriteLine("\nUse following command for help:"); Console.WriteLine("dotnet ImageBinarizerApp -help"); args = new String[] { Console.ReadLine() }; //Test if necessary input arguments were supplied. if (args.Length < 8) { if (args.Length == 1 && args[0].Equals("-help")) { Console.WriteLine("\nHelp:"); Console.WriteLine("\nPass the arguments as following:"); Console.WriteLine("\nExample with automatic RGB:\ndotnet ImageBinarizerApp --input-image c:\\a.png --output-image c:\\out.txt -width 32 -height 32"); Console.WriteLine("\nExample with explicit RGB:\ndotnet ImageBinarizerApp --input-image c:\\a.png --output-image c:\\out.txt -width 32 -height 32 -red 100 -green 100 -blue 100"); } else { Console.WriteLine("\nError: All necessary arguments are not passed. Please pass the arguments first."); Console.WriteLine("\nPress any key to exit the application."); Console.ReadLine(); return; } args = new String[] { Console.ReadLine() };
namespace BinaryImage_Console
{
/// <summary>
/// Program Class of Console App
/// </summary>
class Program
{
/// <summary>
/// Main entry point for Program
/// </summary>
/// <param name="args">Argument of main method</param>
static void Main(string[] args)
{
Console.WriteLine("Welcome to Binary Image Maker");
Console.WriteLine("\nUse following command for help:");
Console.WriteLine("-help");
args = new String[] { Console.ReadLine() };
//Test if necessary input arguments were supplied.
if (args.Length == 1 && args[0].Equals("-help"))
{
Console.WriteLine("\nHelp:");
Console.WriteLine("\nPass the arguments as following:");
Console.WriteLine("\nExample with automatic RGB:\n--input-image c:\\a.png --output-image c:\\Images\\out.txt -width 32 -height 32");
Console.WriteLine("\nExample with explicit RGB:\n--input-image c:\\a.png --output-image c:\\Images\\out.txt -width 32 -height 32 -red 100 -green 100 -blue 100");
}
else
{
Console.WriteLine("\nError: All necessary arguments are not passed. Please pass the arguments first.");
Console.WriteLine("\nPress any key to exit the application.");
Console.ReadLine();
return;
}
string inputImagePath =" ";
string outputImagePath =" ";
int imageWidth = 0;
int imageHeight = 0;
int redThreshold = -1;
int greenThreshold = -1;
int blueThreshold = -1;
string readline = Console.ReadLine();
string[] args1 = readline.Split(' ');
if (args1.Length == 8)
{
if (args1[0].Equals("--input-image") && File.Exists(args1[1]))
{
inputImagePath = args1[1];
}
else
{
Console.WriteLine("\nError: Input file doesn't exist.");
Console.WriteLine("\nPress any key to exit the application.");
Console.ReadLine();
return;
}
int separatorIndex = args1[3].LastIndexOf(Path.DirectorySeparatorChar);
if (args1[2].Equals("--output-image") && separatorIndex >= 0 && Directory.Exists(args1[3].Substring(0, separatorIndex)))
{
outputImagePath = args1[3];
}
else
{
Console.WriteLine("\nError: Output Directory doesn't exist.");
Console.WriteLine("\nPress any key to exit the application.");
Console.ReadLine();
return;
}
if (!args1[4].Equals("-width") || !int.TryParse(args1[5], out imageWidth))
{
Console.WriteLine("\nError: Image Width should be integer.");
Console.WriteLine("\nPress any key to exit the application.");
Console.ReadLine();
return;
}
if (!args1[6].Equals("-height") || !int.TryParse(args1[7], out imageHeight))
{
Console.WriteLine("\nError: Image Height should be integer.");
Console.WriteLine("\nPress any key to exit the application.");
Console.ReadLine();
return;
}
}
else
{
if (args1.Length > 8)
{
if (args1.Length < 14)
{
Console.WriteLine("\nError: All three Red, Green and Blue Thresholds should be passed.");
Console.WriteLine("\nPress any key to exit the application.");
Console.ReadLine();
return;
}
else
{
if (args1[0].Equals("--input-image") && File.Exists(args1[1]))
{
inputImagePath = args1[1];
}
else
{
Console.WriteLine("\nError: Input file doesn't exist.");
Console.WriteLine("\nPress any key to exit the application.");
Console.ReadLine();
return;
}
int separatorIndex = args1[3].LastIndexOf(Path.DirectorySeparatorChar);
if (args1[2].Equals("--output-image") && separatorIndex >= 0 && Directory.Exists(args1[3].Substring(0, separatorIndex)))
{
outputImagePath = args1[3];
}
else
{
Console.WriteLine("\nError: Output Directory doesn't exist.");
Console.WriteLine("\nPress any key to exit the application.");
Console.ReadLine();
return;
}
if (!args1[4].Equals("-width") || !int.TryParse(args1[5], out imageWidth))
{
Console.WriteLine("\nError: Image Width should be integer.");
Console.WriteLine("\nPress any key to exit the application.");
Console.ReadLine();
return;
}
if (!args1[6].Equals("-height") || !int.TryParse(args1[7], out imageHeight))
{
Console.WriteLine("\nError: Image Height should be integer.");
Console.WriteLine("\nPress any key to exit the application.");
Console.ReadLine();
return;
}
if (!args1[8].Equals("-red") || !(int.TryParse(args1[9], out redThreshold)) || redThreshold < 0 || redThreshold > 255)
{
Console.WriteLine("\nError: Red Threshold should be in between 0 and 255.");
Console.WriteLine("\nPress any key to exit the application.");
Console.ReadLine();
return;
}
if (!args1[10].Equals("-green") || !(int.TryParse(args1[11], out greenThreshold)) || greenThreshold < 0 || greenThreshold > 255)
{
Console.WriteLine("\nError: Green Threshold should be in between 0 and 255.");
Console.WriteLine("\nPress any key to exit the application.");
Console.ReadLine();
return;
}
if (!args1[12].Equals("-blue") || !(int.TryParse(args1[13], out blueThreshold)) || blueThreshold < 0 || blueThreshold > 255)
{
Console.WriteLine("\nError: Blue Threshold should be in between 0 and 255.");
Console.WriteLine("\nPress any key to exit the application.");
Console.ReadLine();
return;
}
}
}
else
{
redThreshold = -1;
greenThreshold = -1;
blueThreshold = -1;
}
}
Console.WriteLine("\nImage Binarization in progress...");
try
{
ImageBinarizerApplication obj = new ImageBinarizerApplication();
obj.Binarizer(inputImagePath, outputImagePath, imageWidth, imageHeight, redThreshold, greenThreshold, blueThreshold);
}
catch (Exception e)
{
Console.WriteLine($"\nError: {e.Message}");
Console.WriteLine("\nPress any key to exit the application.");
Console.ReadLine();
return;
}
Console.WriteLine("\nImage Binarization completed.");
Console.WriteLine("\nPress any key to exit the application.");
Console.ReadLine();
}
}
}
Hello... Taking the updated code into consideration. Do you know how I can iterate the between 14 and 197 rather than making it a work as one way application. I want to give the user the option of exiting the application or repeating the process. If he indicates that he wants to repeat the process, I would like the code to resume from line 14 (updated code) .Can you please post your current updated code?
Let this be a lesson, that the documentation is there for a reason. Let it also be noted that you should not be re-initialising variables with the new operator because doing so changes the instance in which you were previously using. You also need to understand why this was an issue, which you've completely avoided answering or explaining in your latest replies. You also didn't need to use split() but I understand why Skydiver linked you it, as we discussed this offline. What you should have been doing was setting the parameters of your string[] args like so :
I don't know how you didn't snag any of this from previous examples. However, if you resolved this issue, you should address post 24 next.C#:string[] args = {"", "", "" }; args[0] = "foo"; args[1] = "fi"; args[2] = "fum";
You should take note of the highlighted lines in the spoiler of your code.C#:static void Main(string[] args) { String inputImagePath = ""; String outputImagePath = ""; int imageWidth = 0; int imageHeight = 0; int redThreshold = -1; int greenThreshold = -1; int blueThreshold = -1; Console.WriteLine("Welcome to Binary Image Maker"); Console.WriteLine("\nUse following command for help:"); Console.WriteLine("dotnet ImageBinarizerApp -help"); args = new String[] { Console.ReadLine() }; //Test if necessary input arguments were supplied. if (args.Length < 8) { if (args.Length == 1 && args[0].Equals("-help")) { Console.WriteLine("\nHelp:"); Console.WriteLine("\nPass the arguments as following:"); Console.WriteLine("\nExample with automatic RGB:\ndotnet ImageBinarizerApp --input-image c:\\a.png --output-image c:\\out.txt -width 32 -height 32"); Console.WriteLine("\nExample with explicit RGB:\ndotnet ImageBinarizerApp --input-image c:\\a.png --output-image c:\\out.txt -width 32 -height 32 -red 100 -green 100 -blue 100"); } else { Console.WriteLine("\nError: All necessary arguments are not passed. Please pass the arguments first."); Console.WriteLine("\nPress any key to exit the application."); Console.ReadLine(); return; } args = new String[] { Console.ReadLine() };
I have been to achieve the iteration by introducing the while loop.Hello
Hello... Taking the updated code into consideration. Do you know how I can iterate the between 14 and 197 rather than making it a work as one way application. I want to give the user the option of exiting the application or repeating the process. If he indicates that he wants to repeat the process, I would like the code to resume from line 14 (updated code) .
while (args[0].Equals("yes"))
I am warming up to the complexity of the task needed to make things more simple. I liked the idea of user responding to prompts. I have also thought about many features I would to add but short of the skill at the moment to pull it off. The application was requested to be delivered as a console application. I appreciate you for sticking with me on this. I look forward to provide you updates as I learn more.This is when it is time to answer my meta-question about why are you acting like the user is passing in command line parameters, when in actuality, you have an interactive console. If your goal was to make it easier for the user to interact with your ImageBinarizerApp class while restricted to console UI, in my opinion, it is easier for the user to answer questions/prompts, than have to type in things in your exacting command line parameters format. Recall that there is a difference between a command line interface vs. a console interface.
I think am getting more ideas on how I can improve the interactive nature of the app with switch-case construct.I am warming up to the complexity of the task needed to make things more simple. I liked the idea of user responding to prompts. I have also thought about many features I would to add but short of the skill at the moment to pull it off. The application was requested to be delivered as a console application. I appreciate you for sticking with me on this. I look forward to provide you updates as I learn more.
class Settings
{
public string InputFileName { get; set; }
public string OutputFileName { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public int Red { get; set; } = -1;
public int Green { get; set; } = -1;
public int Blue { get; set; } = -1;
}
static Settings ParseArguments(string [] args)
{
// your code here to parse the arguments array
// recall, there is no need to prompt the user, all you do is look at the args values.
// if there's a problem print out the issue and return null.
}
static string PromptForString(string prompt)
{
// add code to handle empty strings
Console.Write(prompt);
var input = Console.ReadLine();
return input;
}
static string PromptForInteger(string prompt, int min = int.MinValue, int max = int.MaxValue, bool acceptDefault = false, int defaultValue = 0)
{
while (true)
{
Console.Write(prompt);
var input = Console.ReadLine();
if (string.IsNullOrWhiteSpace(input) && acceptDefault)
return defaultValue;
if (int.TryParse(input, out int value) && min <= value && value <= max)
return value;
Console.WriteLine($"Invalid input. Enter an integer that is between {min} and {max} (inclusive).");
}
}
static int PromptForColorThreshold(string color)
{
return PromptForInteger($"What is the {color} color threshold? (0-255, just press Enter for default value of -1)",
min: 0, max: 255, acceptDefault : true, defaultValue : -1);
}
static Settings PromptForSettings()
{
return new Settings()
{
OutputFileName = PromptForString("What is the input bitmap file name? ");
InputFileName = PromptForString("What is the output text file name? ");
Width = PromptForInteger("What is the width of the output image?");
Height = PromptForInteger("What is the height of the output image?");
Red = PromptForColorThreshold("red");
Green = PromptForColorThreshold("green");
Blue = PromptForColorThreshold("blue");
};
}
static int Main(string [] args)
{
string inputFileName;
string outputFileName;
int width;
int height;
int redThreshold = -1;
int greenThreshold = -1;
int blueThreshold = -1;
var settings = args.Length == 0 ? PromptForSettings() : ParseArguments(args);
if (settings == null)
return -1;
var app = new ImageBinarizerApp();
app.Binarize(settings.InputFileName, settings.OutputFileName, settings.Width, settings.Height,
settings.Red, settings.Green, settings.Blue);
}
PromptFor*()
methods. They simply show the user what input is expected, and wait for the user input. The prompt for integer does a bit more error checking to make sure that user enters a value that is within the minimum and maximum range. It also has the allowDefault
flag that lets the user not provide any input value, and the passed in defaultValue
will be returned.