我正在制定一个计算20名CEO的欠款的计划。我试图使用线性搜索添加搜索功能,但每次都说“找不到xxx”。任何人帮我弄清楚我的代码有什么问题。搜索方法位于代码的末尾。请记住我是一个初学者。 THX
import java.io.*;
import javax.swing.*;
// The "CEO" class.
public class CEO
{
public static void main (String[] args) throws IOException
{
BufferedReader X = new BufferedReader (new FileReader ("G://CEO.txt"));
FileWriter outFile = new FileWriter ("taxinfo.txt");
PrintWriter Y = new PrintWriter (outFile);
String line;
double taxrate;
for (int i = 0 ; i < 20 ; i++)
{
line = X.readLine ();
String[] textarray = line.split (",");
String name = textarray [0];
String company = textarray [1];
String salary = textarray [2];
///////////////////////////////////////////
String namear[] = name.split (",");
String companyar[] = company.split (",");
String salaryar[] = salary.split (",");
String salary2 = String.valueOf (salaryar [0]);
double salary3 = Double.parseDouble (salary2);
//////////////////////////////////////////
if (salary3 <= 10000000)
{
taxrate = 40;
}
else
{
taxrate = 53;
}
calcTax (salary3, taxrate);
Y.println (namear[0]);
Y.println (companyar[0]);
Y.println ((long) salary3);
Y.println (calcTax (salary3, taxrate));
Y.println ("");
System.out.println (namear[0] + ", " + companyar[0] + ", $" + (long)salary3 + ", $" + (calcTax (salary3, taxrate)));
if (i == 19)
{
String findMe;
findMe = JOptionPane.showInputDialog ("Enter name to search");
int loc;
loc = findName (findMe, namear);
if (loc >= 0)
{
JOptionPane.showMessageDialog (null, namear[loc]);
}
else
{
JOptionPane.showMessageDialog (null, findMe + " not found");
}
}
}
Y.close ();
JOptionPane.showMessageDialog (null, "Records stored in file");
//////////////////////////////////////////////////////////////////////////
} // main method
/////////////////////////////////////////////////////////////////////////////////////////
public static double calcTax (double salary, double taxrate)
{
double taxowed = 0;
taxowed = (salary * taxrate) / 100;
return taxowed;
}
/////////////////////////////////////////////////////////////////////////////
public static int findName (String nameToFind, String array[])
{
int place = -1;
for (int i = 0 ; i < array.length ; i++)
{
if (nameToFind.equalsIgnoreCase (array [i]) == true)
{
place = i;
}
}
return place;
}
} // CEO class
答案 0 :(得分:0)
你正试图用你的字符串做一些不可能的事情。
String[] textarray = line.split (",");
...
String name = textarray [0];
...
String namear[] = name.split (",");
&#13;
name
不可能包含任何,
,因为name
来自已被,
拆分的字符串。
我们假设你有一句话John,Smith,Company,Name
,然后按,
- &gt;分开结果将是一个数组[John, Smith, Company, Name]
,现在您正在尝试拆分John
- 这只是无法提供您想要的内容。
答案 1 :(得分:0)
好的,伙计们,我想通了......
import java.io.*;
import javax.swing.*;
// The "CEO" class.
public class CEO
{
public static void main (String[] args) throws IOException
{
BufferedReader X = new BufferedReader (new FileReader ("D://CEO.txt"));
FileWriter outFile = new FileWriter ("taxinfo.txt");
PrintWriter Y = new PrintWriter (outFile);
String line;
double taxrate;
String salary2;
double salary3;
String names[];
String cnames[];
String salary[];
String taxesowedstr[];
names = new String [20];
cnames = new String [20];
salary = new String [20];
taxesowedstr = new String [20];
for (int k = 0 ; k < names.length ; k++)
{
line = X.readLine ();
String[] textarray = line.split (",");
names [k] = textarray [0];
cnames [k] = textarray [1];
salary [k] = textarray [2];
salary2 = String.valueOf (salary [k]);
salary3 = Double.parseDouble (salary2);
if (salary3 <= 10000000)
{
taxrate = 40;
}
else
{
taxrate = 53;
}
Y.println (names [k]);
Y.println (cnames [k]);
Y.println ((long) salary3);
Y.println (calcTax (salary3, taxrate));
Y.println ("");
System.out.println (names [k] + ", " + cnames [k] + ", $" + (long) salary3 + ", $" + (calcTax (salary3, taxrate)));
double taxes = calcTax (salary3, taxrate);
taxesowedstr [k] = String.valueOf (taxes);
if (k == 19)
{
JOptionPane.showMessageDialog (null, "Records stored in file");
String findMe;
findMe = JOptionPane.showInputDialog ("Enter name to search");
int loc;
loc = findName (findMe, names);
if (loc >= 0)
{
JOptionPane.showMessageDialog (null, names [loc] + ", " + cnames [loc] + ", $" + salary [loc] + ", $" + taxesowedstr [loc]);
}
else
{
JOptionPane.showMessageDialog (null, findMe + " not found");
}
}
}
Y.close ();
} // main method
/////////////////////////////////////////////////////////////////////////////////////////
public static double calcTax (double salary, double taxrate)
{
double taxowed = 0;
taxowed = (salary * taxrate) / 100;
return taxowed;
}
/////////////////////////////////////////////////////////////////////////////
public static int findName (String nameToFind, String array[])
{
int place = -1;
for (int i = 0 ; i < array.length ; i++)
{
if (nameToFind.equalsIgnoreCase (array [i]) == true)
{
place = i;
}
}
return place;
}
} // CEO class