Click here to Skip to main content
15,889,874 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Find out of Date Servers from the list of the servers.

Take input file as serverList.txt. There are multiple servers in data center. All the software information installed on multiple servers is saved in this file.

For example, software information saved in the file is as follows.

Server1, Database, MySQL, 5.5
Server2, Database, MySQL, 5.1
Server3, OS, Ubuntu, 10.04
Server1, OS, Ubuntu, 12.04
Server2, OS, Ubuntu, 12.04
Server3, Language, Python, 2.6.3

This file indicates that Server1, has version 5.5 of MySQL installed, and Server2 has version 5.1 installed, and Server3 has version 10.04 of Ubuntu installed. For this program that all version numbers are of the form X.Y or X.Y.Z where X, Y, and Z are made up of only digits.

Write a program that reads this file, and prints a list of server names that have at least one software installation that is outdated version.

Thus, in this case, the output of your program should be:

Server2
Server3

What I have tried:

var list = new List<string>();
        //string strFileContent = string.Empty;
        using (StreamReader sr = new StreamReader(@"D:\input.txt"))
        {
            string Line;
            while ((Line = sr.ReadLine()) != null)
            {
                //strFileContent += Line + ",";
                list.Add(Line);
            }
        }


EDIT (CHill60): OP has taken the code further...
Quote:
have tried till here then stuck -
C#
var list = new List<string>();
 //string strFileContent = string.Empty;


 Dictionary<string, string> latestVersion = new Dictionary<string, string>();
 using (StreamReader sr = new StreamReader(@"D:\input.txt"))
 {
     string Line;
     while ((Line = sr.ReadLine()) != null)
     {
         //strFileContent += Line + ",";
         list.Add(Line);
     }
 }
 string str = string.Empty;
 for (int i = 0; i <= list.Count; i++)
 {
     str = list[i].ToString();

     string[] stt = str.Split(',');

     for (int j = 0; j <= stt.Length; j++)
     {


     }
 }
Posted
Updated 22-Mar-18 3:25am
v5
Comments
F-ES Sitecore 22-Mar-18 5:33am    
Once you have the "line" you can use string.Split using a comma to get the relevant values into an array and then process that array. Google "c# string split" for sample code.

Homework and exercises are set by tutors to help you learn. They give you an opportunity to apply and practise the techniques you are being taught.

So, even if you had actually asked a question, we will not do your homework for you. It really would not help you at all.

Hints: The code you have posted reads the file, but you are not doing anything with the list of lines nor are you comparing the contents of each line to anything.

Keep trying, write the rest of the code and come back if you get stuck.

[EDIT - adding more hints as the OP has taken their code a bit further]

As I'm going to suggest that you examine values of things you are going to find this article useful: Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

You have taken the advice of @F-ES_Sitecore and used Split to get the individual components of each line. If you put a breakpoint on the line
C#
for (int j = 0; j <= stt.Length; j++)
and examine your array stt you will notice that for all the lines
stt[0] contains the Server name - Server1, Server2, etc
stt[1] contains the "type" - Database, OS, Language
stt[2] contains the name of the actual thing of type stt[1] - MySQL, Ubuntu, Python
stt[3] contains the version number
So you don't really need that for-loop using j, you can just look directly at the exact location for the information you are going to check against.

You already have a Dictionary defined latestVersion but you need to populate that, probably using the Add method[^]. Just hard-code the values for now until you get something working.

For each line you are going to want to "lookup" stt[2] in your dictionary and compare stt[3] with the value in the dictionary. If they don't match then this server (stt[0]) needs to be added to your output. I suggest using the TryGetValue method[^]

Give that a go and see how you get on
 
Share this answer
 
v2
Comments
Maciej Los 22-Mar-18 6:39am    
5ed!
rummer 22-Mar-18 6:45am    
CHill60 I tried but didn't get what I needed so posted here.
rummer 22-Mar-18 6:50am    
I have tried till here then stuck -

var list = new List<string>();
//string strFileContent = string.Empty;


Dictionary<string, string> latestVersion = new Dictionary<string, string>();
using (StreamReader sr = new StreamReader(@"D:\input.txt"))
{
string Line;
while ((Line = sr.ReadLine()) != null)
{
//strFileContent += Line + ",";
list.Add(Line);
}
}
string str = string.Empty;
for (int i = 0; i <= list.Count; i++)
{
str = list[i].ToString();

string[] stt = str.Split(',');

for (int j = 0; j <= stt.Length; j++)
{


}
}
Note: i completely agree with CHill60[^], because there's no other way to learn programming as to start programming.

Few tips:
1. Reading text file: File.ReadAllLines Method (System.IO)[^]
C#
string[] lines = File.ReadAllLines("FullFileName.txt");

2. Spliting text: String.Split Method (String[], StringSplitOptions) (System)[^]
3. Custom class to store software details: Classes (C# Programming Guide) | Microsoft Docs[^]
Note: a constructor of your class have to accept comma delimited text (one line)
C#
public class SoftwareStorage
{
	private string sMachineName = string.Empty;
	private string sSoftwareType = string.Empty;
	private string sSoftwareName = string.Empty;
	private Version oSoftwateVersion = null;

	public SoftwareStorage(string delimitedText)
	{
		string[] parts = delimitedText.Split(new string[]{", "}, StringSplitOptions.RemoveEmptyEntries);
		sMachineName = parts[0];
		sSoftwareType = parts[1];
		sSoftwareName = parts[2];
		oSoftwateVersion = new Version(parts[3]); //see point 4.
	}
    //other members of class here
}

4. Versioning software: Version Class (System)[^]
This class provides several methods to compare version of software.

Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900