运行以下代码时出现以下错误:运行:
java.lang.ClassCastException: java.lang.String cannot be cast to Panels.AddNewClientSaveAction
整个错误:
run:
java.lang.ClassCastException: java.lang.String cannot be cast to Panels.AddNewClientSaveAction
Before
at Database.FileUpdate.main(128 | Oli | Much
FileUpdate.java:40)
128 | Oli | Much
128 | Francis Kariuki | Mahia
128 | Francis Kariuki | Mahia
LASTSTUDENTEENTRYLINENNAMES
BUILD SUCCESSFUL (total time: 4 seconds)
我正在尝试创建一个功能,在这种情况下,在特定文本“LASTSTUDENTEENTRYLINENNAMES”之前将新用户添加到文本文件中。
这是添加新用户“Eddys Rockery”之前和之后的示例。
前:
123 | Oliver | Muchai
456 | Revilo | Chamu
LASTSTUDENTEENTRYLINENNAMES
后:
123 | Oliver | Muchai
456 | Revilo | Chamu
678 | Eddys | Rockery
LASTSTUDENTEENTRYLINENNAMES
到目前为止的代码。我已经指出了我认为错误是从哪里产生的。
提前感谢大家的任何帮助和建议。
import Panels.AddNewClientSaveAction;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Stack {
public static final String LAST_STUDENT_LINE = "LASTSTUDENTEENTRYLINENNAMES";
public static StringBuilder line;
public static void main(String[] args) {
StringBuilder sb = new StringBuilder(128);
List<AddNewClientSaveAction> objectInputFieldsList = new ArrayList<>(25);
AddNewClientSaveAction values = new AddNewClientSaveAction();
objectInputFieldsList.addAll(values.rayArrayList());
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("/D:/TestFile.DAT/"));
String text = null;
while ((text = br.readLine()) != null) {
if (sb.length() > 0) {
sb.append("\n");
}
sb.append(text);
}
System.out.println("Before");
System.out.println(sb);
// The Error's here: for (AddNewClientSaveAction s : objectInputFieldsList)
for (AddNewClientSaveAction s : objectInputFieldsList) {
int insertIndex = sb.indexOf(LAST_STUDENT_LINE);
line = new StringBuilder(128);
line.append(s.objectGUID).append(" | ").append(s.userGUID).append("\n");
sb.insert(insertIndex, line.toString());
}
System.out.println("\nAfter");
System.out.println(sb);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException exp) {
}
}
try{
java.io.FileWriter fstream = new java.io.FileWriter("/D:/TestFile.DAT/");
BufferedWriter outobj = new BufferedWriter(fstream);
outobj.write(sb.toString() + "\n");
outobj.close();
}catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
}
另一类:
import Panels.AddNewClient;
import java.util.ArrayList;
public class StackAddClientSaveAction {
final public ArrayList objectInputFieldsList = new ArrayList();
public String objectGUID;
public String userGUID;
public String firmGUID;
public String postalCode;
public ArrayList rayArrayList () {
StackAddClientSaveAction addNewClientSaveAction = new StackAddClientSaveAction();
return addNewClientSaveAction.actionPerformed();
}
public ArrayList actionPerformed ()
{
// AddNewClient Class prints out the GUI where postalCode is entered via JTextField
AddNewClient addNewClient = new AddNewClient();
objectGUID = "1452";
userGUID = "90378";
firmGUID = "3663287";
postalCode = addNewClient.postalCodeJTextField.getText();
// Add to list
objectInputFieldsList.add(objectGUID);
objectInputFieldsList.add(userGUID);
objectInputFieldsList.add(firmGUID);
objectInputFieldsList.add(postalCode);
return objectInputFieldsList;
}
}