如果已经应用了所述方法,我将如何最好地实现阻止一种方法的系统?
我正在处理的工作的一部分需要我提示用户选择是否希望通过字母内的姓氏组织.txt文件,反之亦然。我已经删除了所有代码,因此我的麻烦在编辑或更改.txt文件方面并不是我想防止用户选择姓氏(如果已经应用姓氏,反之亦然)。基本上,就像灯开关一样,如果它打开(有组织的名字),则唯一的选项是关闭(按姓氏组织)。
我不确定如何最好地描述这一点,但是如果您运行代码firstorlast.equals.equals(“ last”),然后运行firstorlast.equals(“ first”),它将无能为力,但是您可以继续运行“最后”。我相信我的错误涉及在检查其当前值是什么时如何分配布尔值。
public static boolean isFirst = true;
public static void sortPatientsByName(String firstOrLast, String fileName) throws IOException {
if (firstOrLast.equals("last")) { // CODE FOR LAST NAME, FIRST NAME
if (isFirst == true) { // BOOLEAN TO SEE IF OTHER HAS BEEN CALLED
FileReader fileReader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String[] words = null;
ArrayList<String> sortName = new ArrayList<>();
String line = "";
while ((line = bufferedReader.readLine()) != null) {
words = line.split(" ");
String swapFirst = "";
String swapLast = "";
String holdBirth = "";
String holdStatus = "";
swapFirst = words[1];
swapLast = words[0];
holdBirth = words[2];
holdStatus = words[3];
line = swapFirst + " " + swapLast + " " + holdBirth + " " + holdStatus;
sortName.add(line);
}
bufferedReader.close();
Collections.sort(sortName);
FileWriter writer = new FileWriter(fileName);
for (int i = 0; i < sortName.size(); i++) {
writer.write(sortName.get(i));
writer.write("\r\n");
}
writer.close();
//isFirst = false; // NOT SURE IF ASSIGNING BOOLEAN FALSE SHOULD BE CALLED WITHIN
}
isFirst = false;
}
if (firstOrLast.equals("first")) { // CODE FOR FIRST NAME, LAST NAME
if (isFirst == false) { // BOOLEAN TO SEE IF OTHER HAS BEEN CALLED
FileReader fileReader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileReader);
ArrayList<String> sortName = new ArrayList<>();
String line = "";
while ((line = bufferedReader.readLine()) != null) {
sortName.add(line);
}
bufferedReader.close();
Collections.sort(sortName);
FileWriter writer = new FileWriter(fileName);
for (int i = 0; i < sortName.size(); i++) {
writer.write(sortName.get(i));
writer.write("\r\n");
}
writer.close();
//isFirst = true;
}
isFirst = true;
}
}
One part of what I'm working on requires me to prompt the user to choose if they want a .txt file to be organized via the last name alphabetically or vice versa. I've gotten all the code down so my trouble isn't in editing or changing my .txt file but I want to prevent the user from selecting last name if last name has already been applied or vice versa. Basically, like a light switch, where if it's on (organized first name) then the only option is off (organize by last name).
I'm not sure how to best describe this, but if you run the code firstOrLast.equals("last") then run firstOrLast.equals("first") it will do nothing, but you can continue to run "last". I believe my error involves how I assign the boolean when checking what its current value is.
public static boolean isFirst = true;
public static void sortPatientsByName(String firstOrLast, String fileName) throws IOException {
if (firstOrLast.equals("last")) { // CODE FOR LAST NAME, FIRST NAME
if (isFirst == true) { // BOOLEAN TO SEE IF OTHER HAS BEEN CALLED
FileReader fileReader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String[] words = null;
ArrayList<String> sortName = new ArrayList<>();
String line = "";
while ((line = bufferedReader.readLine()) != null) {
words = line.split(" ");
String swapFirst = "";
String swapLast = "";
String holdBirth = "";
String holdStatus = "";
swapFirst = words[1];
swapLast = words[0];
holdBirth = words[2];
holdStatus = words[3];
line = swapFirst + " " + swapLast + " " + holdBirth + " " + holdStatus;
sortName.add(line);
}
bufferedReader.close();
Collections.sort(sortName);
FileWriter writer = new FileWriter(fileName);
for (int i = 0; i < sortName.size(); i++) {
writer.write(sortName.get(i));
writer.write("\r\n");
}
writer.close();
//isFirst = false; // NOT SURE IF ASSIGNING BOOLEAN FALSE SHOULD BE CALLED WITHIN
}
isFirst = false;
}
if (firstOrLast.equals("first")) { // CODE FOR FIRST NAME, LAST NAME
if (isFirst == false) { // BOOLEAN TO SEE IF OTHER HAS BEEN CALLED
FileReader fileReader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileReader);
ArrayList<String> sortName = new ArrayList<>();
String line = "";
while ((line = bufferedReader.readLine()) != null) {
sortName.add(line);
}
bufferedReader.close();
Collections.sort(sortName);
FileWriter writer = new FileWriter(fileName);
for (int i = 0; i < sortName.size(); i++) {
writer.write(sortName.get(i));
writer.write("\r\n");
}
writer.close();
//isFirst = true;
}
isFirst = true;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的问题是,当您启动代码时,唯一可以运行的是“第一部分”。
您可以尝试将布尔类型更改为布尔值,并将其设置为null,例如:
static boolean isfirst = null
null然后在代码中您检查布尔值是否为null(首次运行)运行的结尾将其设置为相应的值:
这样,当您运行它时,它首次通过空并且只能传递到第一个通过
一种清洁方法,就是为了创建一个枚举不同的可能性
The issue here is that when you start your code the only thing that can run is the "first" part.
You can try to change the boolean type to Boolean and set it to null like :
static Boolean isFirst = null
Then in your code you check if boolean is null (for the first time is run) and at then end of the run set it to the corresponding value:
That way when you'll run it it pass the first time due to null and after it can only pass to the first one it pass through
One cleaner way would be to create a enum for the different possibility