Naive Bayes计划预测具有以下参数的人的工作类型:年龄:30岁,资格:MTech,经验:8 ..
WorkType Age Qualication Experience
Consultancy 30 Ph.D. 9
Service 21 MTech. 1
Research 26 MTech. 2
Service 28 BTech. 10
Consultancy 40 MTech. 14
Research 35 Ph.D. 10
Research 27 BTech. 6
Service 32 MTech. 9
Consultancy 45 Btech. 17
Research 36 Ph.D. 7
package try2;
import java.awt.BorderLayout;
import javax.swing.*;
public class bayes
{
JFrame frame;
JTable table;
JPanel panel;
JScrollPane tableContainer;
int i,j;
int countC=0,countR=0,countS=0;
int count=0;
int[] CAge=new int[3];
public bayes()
{
frame = new JFrame("JTable Test Display");
panel = new JPanel();
panel.setLayout(new BorderLayout());
String row[][]={{"consultancy","30","phd","9"},
{"service","21","mtech","1"} ,
{"research","26","mtech","2"},{"service","28","btech","10"},
{"consultancy","40","mtech","14"},{"research","35","phd","10"},
{"research","27","btech","6"},{"service","32","mtech","9"},
{"consultancy","45","btech","17"},{"research","36","phd","7"}};
String column[]={"job","age","qualification","experience"};
table=new JTable(row,column);
tableContainer = new JScrollPane(table);
panel.add(tableContainer, BorderLayout.CENTER);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
//work type count
for(i=0;i<10;i++)
{
if(table.getValueAt(i,0)=="consultancy")
{
countC++;
}
if(table.getValueAt(i,0)=="research")
{
countR++;
}
if(table.getValueAt(i,0)=="service")
{
countS++;
}
}
//consultancy age count
for(i=0;i<10;i++)
{
***if(((table.getValueAt(i, 0))=="consultancy") && ((Integer.parseInt((String)table.getValueAt(i, 1))>=20) || (Integer.parseInt((String)table.getValueAt(i, 1))<=30)) )***
{
count++;
}
上面代码的问题是我无法将年龄列值与数字进行比较。我尝试使用intparse()函数将值转换为int但仍然无效。该行标有** *在上面给出的代码中。请帮助我。它给出错误,它不能将对象类型转换为整数
答案 0 :(得分:1)
问题可能出在以下情况:
((Integer.parseInt((String) table.getValueAt(i, 1)) >= 20) || (Integer.parseInt((String) table.getValueAt(i, 1)) <= 30))
此计算结果始终为true,因此计数不正确。如果您只想计算年龄在20到30岁之间的人,您应该使用AND运算符:
((Integer.parseInt((String) table.getValueAt(i, 1)) >= 20) && (Integer.parseInt((String) table.getValueAt(i, 1)) <= 30))