gpt4 book ai didi

c# - "Input string was not in a correct format"错误

转载 作者:太空宇宙 更新时间:2023-11-03 19:09:33 25 4
gpt4 key购买 nike

下面的脚本展示了

"index was outside the bounds of the array"

在线

for (int i = 0; i <u_f.GetLength(1); i++)

当我把它改成

for (int i = 0; i <=u_f.GetLength(1); i++)

显示

"Input string was not in a correct format"

int uid = Convert.ToInt32(txtbx_id.Text); 行错误

        float userscore,itemscore,result;
string lineitem, lineuser;
//float[][] a = new float[89395][100];
//float[][] b = new float[1143600][100];
float[][] a = Enumerable.Range(0, 89395).Select(i => new float[100]).ToArray();
float[][] b = Enumerable.Range(0, 1143600).Select(j => new float[100]).ToArray();
//float[,] c = new float[89395, 100];
StreamReader fileitem = new StreamReader("c:\\1.txt");
StreamReader fileuser = new StreamReader("c:\\2.txt");
public Form1()
{
InitializeComponent();
for (int x = 0; x <= 8939500; x++)
{
lineuser = fileuser.ReadLine();
if (!string.IsNullOrEmpty(lineuser))
{
string[] values = lineuser.Split(' ');
int userid, factoriduser;
foreach (string value in values)
{
userid = Convert.ToInt32(values[0]);
factoriduser = Convert.ToInt32(values[1]);
userscore = Convert.ToSingle(values[2]);
a[userid][factoriduser] = userscore;
}
}
}

for (int y = 0; y <= 114360000; y++)
{
lineitem = fileitem.ReadLine();
if (!string.IsNullOrEmpty(lineitem))
{
string[] valuesi = lineitem.Split(' ');
int itemid, factoriditem;
foreach (string value in valuesi)
{
itemid = Convert.ToInt32(valuesi[0]);
factoriditem = Convert.ToInt32(valuesi[1]);
itemscore = Convert.ToSingle(valuesi[2]);
b[itemid][factoriditem] = itemscore;
}
}

}

}
public float dotproduct(int userid,int itemid)
{

//get the score of 100 from user and item to dotproduct
float[] u_f = a[userid];
float[] i_f = b[itemid];

for (int i = 0; i <u_f.GetLength(1); i++) //<----error
{
result += u_f[userid] * i_f[itemid];
}
return result;

}

private void btn_recomm_Click(object sender, EventArgs e)
{
if(txtbx_id.Text==null)
{
MessageBox.Show("please insert user id");
}
if (txtbx_id.Text != null && txtbx_itemid==null)
{
int sc = Convert.ToInt32(txtbx_id.Text);
if (sc>=0 &&sc<=89395)
{
for (int z=0;z<=1143600;z++)
{
dotproduct(sc,z);
}
Hashtable hashtable = new Hashtable();
//put the result in hashtable
foreach (DictionaryEntry entry in hashtable)
{
System.Console.WriteLine("{0}, {1}", entry.Key, entry.Value);
}
}
}
if (txtbx_id!=null && txtbx_itemid!=null)
{
int uid = Convert.ToInt32(txtbx_id.Text); //<----error
int iid = Convert.ToInt32(txtbx_itemid.Text);
{
if (uid>=0 && uid<=89395 && iid>=0 && iid<=1143600)
{
dotproduct(uid,iid);
MessageBox.Show("The Score of user id "+uid+" is "+result);
}
}
}

最佳答案

"index was outside the bounds of the array"

问题 1:您正在从无效维度访问 GetLength(1) 元素。你只有一维数组,但访问第二维长度时,数组索引总是从零开始。

解决方案 1:您应该使用 GetLength(0)Length,因为您的数组是一维数组。

Array.GetLength(0) 返回数组第一维中的元素数。

来自 MSDN:

Gets a 32-bit integer that represents the number of elements in the specified dimension of the Array.

替换这个:

for (int i = 0; i <u_f.GetLength(1); i++)  

有了这个:

for (int i = 0; i <u_f.GetLength(0); i++)  

或与

  for (int i = 0; i <u_f.Length; i++)  

问题 2:您正在执行 null 直接检查 TextBox 控件而不是 TextBox 文本/值。所以这里发生的是,即使用户没有在您的文本框中输入任何值,if 条件也不会失败,因为 TextBox 不是 null。所以它进入 if block ,但是当它尝试转换该空字符串时/使用以下语句将值转换为整数:

int uid = Convert.ToInt32(txtbx_id.Text);     //<----error

如您所说,它会抛出异常。

解决方案 2:您需要执行 null 检查 TextBox 值,例如 TextBox1.Text!=null

建议1:您可以使用String.IsNullOrWhiteSpace()方法来验证值是Null、Empty还是WhiteSpace。

建议2:如果想在将用户输入解析为整数时避免运行时异常,需要使用int.TryParse()方法。

替换这个:

if (txtbx_id!=null && txtbx_itemid!=null)
{
int uid = Convert.ToInt32(txtbx_id.Text); //<----error
int iid = Convert.ToInt32(txtbx_itemid.Text);
{
if (uid>=0 && uid<=89395 && iid>=0 && iid<=1143600)
{
dotproduct(uid,iid);
MessageBox.Show("The Score of user id "+uid+" is "+result);
}
}
}

有了这个:

if (!String.IsNullOrWhiteSpace(txtbx_id.Text) &&      
!String.IsNullOrWhiteSpace(txtbx_itemid.Text))
{
int uid,iid;
if(int.TryParse(txtbx_id.Text,out uid) && int.TryParse(txtbx_itemid.Text,out iid))
{
if (uid>=0 && uid<=89395 && iid>=0 && iid<=1143600)
{
dotproduct(uid,iid);
MessageBox.Show("The Score of user id "+uid+" is "+result);
}
}
}

关于c# - "Input string was not in a correct format"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21937254/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com