无限循环被卡住,java代码有问题
手头的任务是制作一个输血管理器应用程序,该应用程序可以有效地选择输血的献血者 当应用程序启动时,它应该尝试打开两个文件:“捐助者.txt”和“接收者.txt”。如果其中一个文件丢失,程序应宣布问题并退出。这两个文件的格式应如下:每行应包含一个人的全名和血型,并用分号分隔。程序应该首先读取Centers.txt,将每一行拆分为姓名和血型,并将生成的数组作为新元素存储在Centers arraylist中。它还应该在屏幕上打印列表,并检查每个人的血型是否有效。如果发现无效血型,则不应将条目添加到arraylist中,并应通知用户哪个条目有问题。然后应该以类似的方式读取、处理和存储收件人(在收件人arraylist中),这是任务的第一部分 这是我以前的代码,在更新它以接近任务结束时,我发现代码停止工作,我调试了它,发现它卡在一个无限循环中,不知道如何修复它,或者是否有其他方法重写它以使其工作,可能不使用while循环无限循环被卡住,java代码有问题,java,Java,手头的任务是制作一个输血管理器应用程序,该应用程序可以有效地选择输血的献血者 当应用程序启动时,它应该尝试打开两个文件:“捐助者.txt”和“接收者.txt”。如果其中一个文件丢失,程序应宣布问题并退出。这两个文件的格式应如下:每行应包含一个人的全名和血型,并用分号分隔。程序应该首先读取Centers.txt,将每一行拆分为姓名和血型,并将生成的数组作为新元素存储在Centers arraylist中。它还应该在屏幕上打印列表,并检查每个人的血型是否有效。如果发现无效血型,则不应将条目添加到ar
package java_assigment_4;
import java.io.*;
import java.util.*;
public class java_assigment_4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner file_donors;
Scanner file_recp;
ArrayList list_donors = new ArrayList();
ArrayList list_recp = new ArrayList();
String blood_type = "o- o+ a- a+ b- b+ ab- ab+";
try
{
file_donors = new Scanner(new FileReader("donors.text"));
while (file_donors.hasNextLine())
{
list_donors.add(file_donors.nextLine()); // store donors names & blood type in array list
}//end while
file_donors.close();
}//end try
catch (FileNotFoundException e)
{
System.out.println("Error: " + e.getMessage());
}//end catch
try
{
file_recp = new Scanner(new FileReader("recipients.text"));
while (file_recp.hasNextLine())
{
list_recp.add(file_recp.nextLine()); // store recipents names & blood type in array list
}//end while
file_recp.close();
}//end try
catch (FileNotFoundException e)
{
System.out.println("Error: " + e.getMessage());
}//end catch
System.out.println("donors " + list_donors);
System.out.println("\n");
System.out.println("recipents " + list_recp);
// for (int i=0;i<list_donors.size();i++) {
// list_donors.contains(";"); // need a substring to store type after ;
}
}
}
package java\u assignment\u 4;
导入java.io.*;
导入java.util.*;
公共类java\u assignment\u 4{
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
扫描仪文件;
扫描仪文件记录;
ArrayList_=新建ArrayList();
ArrayList_recp=新的ArrayList();
弦血类型=“o-o+a-a+b-b+ab-ab+”;
尝试
{
file_senders=新扫描仪(新文件阅读器(“senders.text”);
while(文件\u.hasNextLine())
{
列出捐献者。添加(文件_-gorders.nextLine());//在数组列表中存储捐献者姓名和血型
}//结束时
文件_.close();
}//结束尝试
catch(filenotfounde异常)
{
System.out.println(“错误:+e.getMessage());
}//端接
尝试
{
file_recp=新扫描仪(新文件阅读器(“recipients.text”);
while(文件\u recp.hasNextLine())
{
list_recp.add(file_recp.nextLine());//在数组列表中存储收件人姓名和血型
}//结束时
文件_recp.close();
}//结束尝试
catch(filenotfounde异常)
{
System.out.println(“错误:+e.getMessage());
}//端接
System.out.println(“捐赠者”+捐赠者名单);
System.out.println(“\n”);
系统输出打印项次(“收件人”+列表记录);
//对于(int i=0;i,list\u.size()
将始终返回0,
原因列表开始时为空
因此代码从不调用文件\u.nextLine()
而file\u.hasNextLine()
将始终为真
public class java_assigment_4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner file_donors;
Scanner file_recp;
ArrayList<String> list_donors = new ArrayList<String>();
ArrayList<String> list_recp = new ArrayList<String>();
String [] blood_type = {"o-","o+","a-","a+","b-","b+","ab-","ab+"};
try
{
file_donors = new Scanner(new FileReader("donors.txt"));
// file_donors.hasNextLine() always true in your code
while (file_donors.hasNextLine())
{
// list_donors.size() will return 0, cause the list_donors is empty when it begins
// so the code never enter this for and never call file_donors.nextLine()
for (int i=0;i<list_donors.size();i++) {
...
}
}
使用一些代码进行更新以提供帮助
import java.io.*;
import java.util.*;
public class Blood
{
public static void main(String[] args)
{
ArrayList<String> list_donors = new ArrayList<String>();
ArrayList<String> list_recp = new ArrayList<String>();
System.out.println("Starting!");
copyFromFile("/home/mitz/stackoverflow/java/Donors.txt", list_donors);
System.out.println("imported "+ list_donors.size() + " registers");
copyFromFile("/home/mitz/stackoverflow/java/Receptors.txt", list_recp);
System.out.println("imported "+ list_recp.size() + " registers");
System.out.println("Finished!");
}
public static void copyFromFile(String filename, ArrayList<String> listDestiny)
{
Scanner fileScanner;
FileReader fileReader;
try
{
fileReader = new FileReader(filename);
fileScanner = new Scanner(fileReader);
while (fileScanner.hasNextLine())
{
String currentLine = fileScanner.nextLine();
String type = currentLine.substring((currentLine.indexOf(";") + 1));
if(isValidBloodType(type))
{
listDestiny.add(currentLine);
System.out.println("Imported: " + currentLine);
}else{
System.out.println("Invalid blood type!! Alien detected with blood type: " + type);
}
}
fileScanner.close();
}
catch (FileNotFoundException e)
{
System.out.println("Arquivo não encontrado");
}
}
public static Boolean isValidBloodType(String type)
{
String[] blood_type = {"o-", "o+", "a-", "a+", "b-", "b+", "ab-", "ab+"};
return Arrays.asList(blood_type).contains(type);
}
}
import java.io.*;
导入java.util.*;
公家血脉
{
公共静态void main(字符串[]args)
{
ArrayList_=新建ArrayList();
ArrayList_recp=新的ArrayList();
System.out.println(“开始!”);
copyFromFile(“/home/mitz/stackoverflow/java/genders.txt”,列出捐赠者);
System.out.println(“导入的”+列表大小()+“寄存器”);
copyFromFile(“/home/mitz/stackoverflow/java/Receptors.txt”,list_recp);
System.out.println(“导入的”+列表记录大小()+“寄存器”);
System.out.println(“完成!”);
}
公共静态void copyFromFile(字符串文件名,ArrayList listDestiny)
{
扫描仪;
文件阅读器;
尝试
{
fileReader=新的fileReader(文件名);
FileScaner=新扫描仪(fileReader);
while(fileScanner.hasNextLine())
{
字符串currentLine=fileScanner.nextLine();
字符串类型=currentLine.substring((currentLine.indexOf(“;”)+1));
如果(isValidBloodType(类型))
{
listDestiny.add(当前行);
System.out.println(“导入:+currentLine”);
}否则{
System.out.println(“无效血型!!检测到血型为:+的外国人”);
}
}
fileScanner.close();
}
catch(filenotfounde异常)
{
System.out.println(“Arquivo não encontrado”);
}
}
公共静态布尔值isValidBloodType(字符串类型)
{
字符串[]血型={“o-”、“o+”、“a-”、“a+”、“b-”、“b+”、“ab-”、“ab+”};
返回数组.asList(blood_type).contains(type);
}
}
我尝试过你的方法,但在“字符串捐赠者=列表捐赠者。获取(i)”方面,我遇到了困难'bc我已经删除了包含i的for循环。我试图将i设置为0,但没有成功,我还尝试了其他方法,试图让它工作。当我陷入困境时,你能帮助我吗?在我学习Java的第一年,它还希望我将这行字符串转换为current_line=file_providers.hasNextLine()到boolean@ThristianHylle我用一些代码更新了我的答案,我希望它能帮助你
while (file_donors.hasNextLine())
{
string current_line = file_donors.hasNextLine();
import java.io.*;
import java.util.*;
public class Blood
{
public static void main(String[] args)
{
ArrayList<String> list_donors = new ArrayList<String>();
ArrayList<String> list_recp = new ArrayList<String>();
System.out.println("Starting!");
copyFromFile("/home/mitz/stackoverflow/java/Donors.txt", list_donors);
System.out.println("imported "+ list_donors.size() + " registers");
copyFromFile("/home/mitz/stackoverflow/java/Receptors.txt", list_recp);
System.out.println("imported "+ list_recp.size() + " registers");
System.out.println("Finished!");
}
public static void copyFromFile(String filename, ArrayList<String> listDestiny)
{
Scanner fileScanner;
FileReader fileReader;
try
{
fileReader = new FileReader(filename);
fileScanner = new Scanner(fileReader);
while (fileScanner.hasNextLine())
{
String currentLine = fileScanner.nextLine();
String type = currentLine.substring((currentLine.indexOf(";") + 1));
if(isValidBloodType(type))
{
listDestiny.add(currentLine);
System.out.println("Imported: " + currentLine);
}else{
System.out.println("Invalid blood type!! Alien detected with blood type: " + type);
}
}
fileScanner.close();
}
catch (FileNotFoundException e)
{
System.out.println("Arquivo não encontrado");
}
}
public static Boolean isValidBloodType(String type)
{
String[] blood_type = {"o-", "o+", "a-", "a+", "b-", "b+", "ab-", "ab+"};
return Arrays.asList(blood_type).contains(type);
}
}