Witam, rozpocząłem naukę C# i robię pewien projekcik. Mam taki problem że muszę wczytać plik który załączam (1.txt) Pierwszą linię muszę rozdzielić na dwie zmienne( liczba przedmiotów oraz pojemność) a kolejne linie to wartości które też muszę rozdzielić ( waga i wartość) i począwszy od drugiej linii chciałbym je wrzucić do tablicy aby móc później wykorzystać te dane w dalszej części programu. Niestety mam problem z wpisaniem ich do tablicy. Za wszelkie wskazówki/pomoc bardzo dziękuję!
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
namespace Problemplecakowy
{
class Program
{
static void Main(string[] args)
{
int counter = 0;
string line;
string wybor;
string[] word;
// int[] tablica = new int[10];
string[] dane;
// int i = 0;
System.Console.WriteLine("Wybierz nr dokumentu w skali 1-10");
wybor=System.Console.ReadLine();
// Read the file and display it line by line.
System.IO.StreamReader file = new System.IO.StreamReader(@"C:\Users\Mateusz\Desktop\problem plecakowy\dane_testowe\"+ wybor + ".txt" );
line = file.ReadLine();
System.Console.WriteLine(line);
// to split our string for quantity of elements and volume of backpack
char[] delimiterChars = { ' ', ',', '.', ':', '\t' };
string[] words = line.Split(delimiterChars);
System.Console.WriteLine("{0} words in text:", words.Length);
System.Console.WriteLine(" Liczba przedmiotow: " + words[0] + " Ładowność plecaka: " + words[1]);
int n_przedmiotow = int.Parse(words[0]);
int v_plecaka = int.Parse(words[1]);
string[] t = File.ReadAllLines(@"C:\Users\Mateusz\Desktop\problem plecakowy\dane_testowe\" + wybor + ".txt");
// List<string> t = new List<string>();
for (int i = 1; i <= n_przedmiotow; i++ )
{
// dzieli stringa na czesci
char[] delimiterChar = { ' ', ',', '.', ':', '\t' };
word = t[i].Split(delimiterChars);
System.Console.WriteLine("Przedmiot: " +i + " Waga: " + word[0] + " Wartosc: " + word[1]);
int waga = int.Parse(word[0]);
int wartosc = int.Parse(word[1]);
//TUTAJ GENERALNIE ZACZYNA SIĘ MÓJ PROBLEM. Dane word[0] i word[1] chciałbym wrzucić do innej tablicy gdyż te są nadpisywane
// podczas działania pętli i wyświetlania ich w konsoli
counter++;
}
// System.Console.WriteLine("A teraz niespodzianka" + word[0] + word[1]);
System.Console.WriteLine("Rozbite liczby to: " + n_przedmiotow + " i " + v_plecaka);
file.Close();
System.Console.WriteLine("There were {0} lines.", counter);
// Suspend the screen.
System.Console.ReadLine();
}
}
}