Question

In: Computer Science

Create C# code that can search a text file and output the data at the line...

Create C# code that can search a text file and output the data at the line number inputted and amount of entries needed.

Example of call in command window:

Search16s filename.txt 273   10

Where 273 is the line number to start the output from, and 10 is the number of sequences that the program should output.

The number of sequences entered on call should always be a odd number or give an error in console.

The output should also display in the console.

Solutions

Expert Solution

using System;

using System.IO;

class MainClass

{

public static void Main(String[]args)

{

Console.Write("\nEnter the name of the file: ");

string fileName = Console.ReadLine();

if(!File.Exists(fileName))

{

Console.WriteLine("Could not locate file: " + fileName);

}

else

{

string[] allLines = File.ReadAllLines(fileName);

int counter = allLines.Length;

Console.WriteLine("\nFile read complete: Total " + counter + " lines read");

Console.Write("Enter the line number: ");

int lineNum = Int32.Parse(Console.ReadLine());

while(lineNum < 0 || lineNum > counter)

{

Console.WriteLine("Input for line number exceeded the file content!");

Console.Write("\nEnter the line number: ");

lineNum = Int32.Parse(Console.ReadLine());

}

string line = allLines[lineNum - 1];

Console.Write("Enter a sequence number for the line (must be odd): ");

int sequenceNum = Int32.Parse(Console.ReadLine());

while(sequenceNum < 0 || sequenceNum > line.Length)

{

Console.WriteLine("Input for sequence number exceeded the line length");

Console.Write("\nEnter a sequence number for the line (must be odd): ");

sequenceNum = Int32.Parse(Console.ReadLine());

}

while(sequenceNum % 2 == 0)

{

Console.WriteLine("Input for sequence number must be odd");

Console.Write("\nEnter a sequence number for the line (must be odd): ");

sequenceNum = Int32.Parse(Console.ReadLine());

}

string result = line.Substring(0, sequenceNum);

Console.WriteLine("\nResult: " + result);

}

}

}

***************************************************************** SCREENSHOT **********************************************************

INPUT FILE:


Related Solutions

Using C++, write a code that this program always stores text file output into a text...
Using C++, write a code that this program always stores text file output into a text file named "clean.txt". -The program should read one character at a time from "someNumbers.txt", and do the following. -If it is a letter, print that letter to the screen, AND also store it in the text file. All letters should be converted to lowercase beforehand. -If it is a number, print that number to screen, but do NOT store it in the text file....
Write a C program that can search for a string within a text file, replace it...
Write a C program that can search for a string within a text file, replace it with another string and put results in a new file.
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
Create a JAVA code program: Huffman code will be converted to its text equivalent Output an...
Create a JAVA code program: Huffman code will be converted to its text equivalent Output an error message if the input cannot be converted I can give an thumbs up! :)
please submit the C code( no third party library). the C code will create output to...
please submit the C code( no third party library). the C code will create output to a file, and iterate in a loop 60 times and each iteration is 1 second, and if any key from your keyboard is pressed will write a 1 in the file, for every second no key is pressed, will write a 0 into the output file.
In C Programming Language Write a program to output to a text log file a new...
In C Programming Language Write a program to output to a text log file a new line starting with day time date followed by the message "SUCCESSFUL". Please screenshot the results.
Keep getting error where the code cannot read the text file and create an arraylist of...
Keep getting error where the code cannot read the text file and create an arraylist of objects from it. HouseListTester: import java.util.*; //Hard codes the criteria public class HouseListTester { static HouseList availableHouses; public static void main(String []args) { availableHouses = new HouseList("C:\\Users\\jvs34\\Downloads\\houses.txt"); Criteria c1 = new Criteria(1000, 500000, 100, 5000, 0, 10); Criteria c2 = new Criteria(1000, 100000, 500, 1200, 0, 3); Criteria c3 = new Criteria(100000, 200000, 1000, 2000, 2, 3); Criteria c4 = new Criteria(200000, 300000, 1500,...
Create a C program that solves the word search puzzle contained in the file 'WordSearchPuzzle.txt'. The...
Create a C program that solves the word search puzzle contained in the file 'WordSearchPuzzle.txt'. The words that need to be found are in the file 'WordList.txt'. There are 100 words to be found. You must find the location of the first and last letter of each word as well as the cardinal direction that describes the word's orientation. The location of first and last letters are to be described in terms of row and column, with indexing starting at...
Consider a text file that you will create named “employees.txt”. The file contains data organized according...
Consider a text file that you will create named “employees.txt”. The file contains data organized according to the following format:John Smith 10 15Sarah Johnson 40 12Mary Taylor 27 13Jim Stewart 25 8For instance, “John” is the first name, “Smith” is the last name, “10” is the number of hours per week, and “15” is the hourly rate.Write a program that computes the weekly salary of each employee. The program prints the first name, last name, and weekly salary of each...
HW_6a - Read a text file Create a new C++ project and name it as:   Numbers...
HW_6a - Read a text file Create a new C++ project and name it as:   Numbers Create a text file and     save it as:   data.txt Create and save the file      in a C++ project      in the Resource folder. Enter the following numbers:        3                                                              4                                                              5       Note:   After you enter the 5, don’t press the enter key. Save and close the file. Add another file and name it:   Source.cpp Write one statement that declares a file...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT