텍스트 파일에서 두 개의 숫자를 읽고 싶습니다 : 2,5 (1 행은 2, 2 행은 5 내 텍스트 파일). 몇 시간 동안이 문제를 해결하려고하지만 코드를 실행할 때마다이 줄에서 오류가 발생합니다.
citaj[o] = int.Parse(h);
이것은 버튼에 대한 전체 코드입니다. 또한 한 줄씩 richtextbox에 넣는 동안 오류가 발생했습니다.
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
int p = 4;
int i = 0;
int b = 0;
int c = 0;
int x = 0;
TextBox[] text = new TextBox[50];
string[] linije = new string[50];
string[] brojac = new string[10];
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
text[1] = textBox1;
text[2] = textBox2;
text[3] = textBox3;
brojac[0] = p.ToString();
brojac[1] = c.ToString();
System.IO.File.WriteAllLines(@"text\brojac.txt", brojac);
if (b == 0)
{
for (i = c; i <= p; i++)
{
if ((i == c) && (x == 0))
{
linije[i] = "---------------";
x = 1;
}
else if (i == p)
{
linije[i] = "------------------";
b = 1;
}
else
{
switch (x)
{
case 3: linije[i] ="Sifra:" + " " + text[3].Text;
x = 0;
break;
case 2: linije[i] ="Korisnicko ime:" + " " + text[2].Text;
x = 3;
break;
case 1: linije[i] ="Naziv:" + " " + text[1].Text;
x = 2;
break;
}
}
}
}
if(b == 1)
{
c = c + 5;
p = p + 5;
b = 0;
System.IO.File.WriteAllLines(@"text\Kontener.txt", linije);
}
}
private void button2_Click(object sender, EventArgs e)
{
int o = 0;
string h;
int[] citaj = new int[2];
System.IO.StreamReader sr = new System.IO.StreamReader(@"text\brojac.txt");
while ((h = sr.ReadLine()) != null)
{
string[] parts = h.Split(',');
citaj[0] = int.Parse(parts[0]);
citaj[1] = int.Parse(parts[1]);
}
richTextBox1.Lines = new string[] { citaj[0].ToString(), citaj[1].ToString() };
}
}
}
배열이 초기화되지 않았습니다.
배열 대신 목록을 사용해보십시오.
private void button2_Click(object sender, EventArgs e)
{
List<int> citaj = new List<int>();
string h;
using(System.IO.StreamReader sr = new System.IO.StreamReader(@"text\brojac.txt"))
{
while ((h = sr.ReadLine()) != null)
{
int number = 0;
if (int.TryParse(h, out number))
citaj.Add(number);
}
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다