1

# Jolly Jumpers

A sequence is "Jolly Jumper" if the absolute value of the differences between values exists in the provided string.

Salida: true/ false
Prueba:
1432 : true
5142 : false

This is the code

using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace JollyJumpers
{
class MainClass
{
public static void Main(string[] args)
{
try
{
StreamWriter outFile = new StreamWriter("output.out");
int quantity;
int[] numbers;
int[] results;

while (!inFile.EndOfStream)
{
quantity = Convert.ToInt32(matches[0].ToString());
numbers = new int[quantity];
results = new int[quantity - 1];
bool flag = true;

for (int i = 0; i < quantity; i++)
numbers[i] = Convert.ToInt32(matches[i + 1].ToString());

for (int i = 0; i < quantity - 1; i++)
results[i] = Math.Abs(numbers[i] - numbers[i + 1]);

for (int i = 0; i < quantity - 2; i++)
{
if (Math.Abs(results[i] - results[i + 1]) != 1)
{
flag = false;
break;
}
}

if (flag)
outFile.WriteLine("Jolly");
else
outFile.WriteLine("Not Jolly");

numbers = null;
results = null;
}
Console.Write("Finishing the Program");
inFile.Close();
outFile.Close();

}
catch (System.IO.FileNotFoundException)
{
Console.WriteLine("File does not exist");
}
catch
{
Console.WriteLine("Something Unexpected happened");
}

}
}
}

When the code is executed, the following message appears on the console

File does not exist

What is the problem?

asked by Andres Camacho Aguilar 25.01.2016 в 23:37
source

3

First you should indicate a path for the files, at least use

string pathInput = Path.Combine(Application.StartupPath, "input.in");

This at least indicates precisely that you will take the route from the place where you run the application. If you need it, you can change the Application. StartupPath by the fixed path where the files are (you can also define it in the configuration file app.config )

On the other hand, remember that if you run from the VS this compiles the .exe in the bin\Debug folder, so you must make sure that the files you are trying to access are in that folder.

If the files are integrated into the project in the VS, that is if you see them in the Solution Explorer they could be copied next to the .exe in the debug folder just by changing the option of the properties of the file Copy to Output directory with the option Copy always

On the other hand I could mention that you do not need to go through the file that way, if you are going to go through the lines and if the file is not very large, you could load it to memory using

string pathInput = Path.Combine(Application.StartupPath, "input.in");

foreach(string linea in lineas){

MatchCollection matches = Regex.Matches(linea, @"\d+");
//resto codigo
}

The File.ReadAllLines leaves the code more neat and legible to interact with the lines of the file.

0

Obtaining Error FileNotFoundException strictly refers to not finding the specified file. I suggest you define the routes where the files will be found, for example:

string pathInput = @"c:\data\input.in";
string pathOutput = @"c:\data\output.out";

StreamWriter outFile = new StreamWriter(pathOutput);
...
...
...

In case the files do not exist, you can add a validation that allows you to create the files and enter inside them, the data you want, for example:

string pathInput = @"c:\data\input.in";
string pathOutput = @"c:\data\output.out";

if (!File.Exists(pathInput))  //No existe, crea el archivo input.in.
{
File.Create(pathInput);
TextWriter tw = new StreamWriter(path);
tw.WriteLine("Información para el archivo input.in!");
tw.Close();
}
if (!File.Exists(pathOutput))  //No existe, crea el archivo Output.out.
{
File.Create(pathOutput);
TextWriter tw = new StreamWriter(path);
tw.WriteLine("Información para el archivo Output.out!");
tw.Close();
}