Thristian Hylle
Thristian Hylle

Reputation: 1

infinite loop stuck and having problem with code in java

the task at hand is to make a Blood Transfusion Manager application that efficiently chooses donors for blood transfusion.

When the application is launched it should try to open two files: “donors.txt” and “recipients.txt”. If one of the files is missing the program should announce the problem and exit. Both files should be formatted in the following way: each row should contain a person’s full name and their blood type separated by semicolon. The program should first read donors.txt, split each line into name and blood type and store the resulting array as a new element in a donors arraylist. It should also print the list on the screen and check that each person’s blood type is valid. If an invalid blood type is found that entry should not be added to the arraylist and the user should be notified which entry had a problem. Recipients should then be read, processed and stored (in recipients arraylist) in a similar manner this is part one of the task

this is my previous code and after updating it more to get closer to the end of my task i found that the code stopped work i debugged it and found that it is stuck in an infinite loop not sure how to fix it to or if there any other way to rewrite it to work maybe not using a while loop

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 ; 
        }
    }
}

this code below is the lastest code and is the one that is not working

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"));

            while (file_donors.hasNextLine())
            {

                  for (int i=0;i<list_donors.size();i++) {

                        String donor=list_donors.get(i);
                        String type =donor.substring((donor.indexOf(";") + 1)); // look for ; and stores info after witch is the blood type into a substring so i can use for checking if vaild 
                        type=type.trim();



                         for (int z=0;z<blood_type.length;z++) {
                             if (type.equals(blood_type [z])) { compares the two lists
                                 list_donors.add(file_donors.nextLine());  // store donors names & blood if vaild type in array list 
                             }
                             else {
                                 System.out.println( "this person with blood;" + type + " is not valid ");
                                    }
                                }
                            }
                        }
                        file_donors.close();

                    } 

                    catch (FileNotFoundException e)
                    {
                        System.out.println("Error: " + e.getMessage());
                    }


        System.out.println("donors " + list_donors);

        try
        {
            file_recp = new Scanner(new FileReader("recipients.txt"));

            while (file_recp.hasNextLine())
            {
                  for (int i=0;i<list_recp.size();i++) {

                        String recp=list_recp.get(i);
                        String type =recp.substring((recp.indexOf(";") + 1));
                        type=type.trim();



                         for (int z=0;z<blood_type.length;z++) {
                             if (type.equals(blood_type [z])) { // compares the two lists 
                                 list_recp.add(file_recp.nextLine());  // store recp names & blood type if vaild in array list 
                             }
                             else {
                                 System.out.println( "this person with blood ;" + type + " is not valid ");
                                    }
                                }
                            } 
                        }

                        file_recp.close();
                     }

                    catch (FileNotFoundException e)
                    {
                        System.out.println("Error: " + e.getMessage());
                    }

       // System.out.println("donors " + list_donors);
       // System.out.println("\n");
       // System.out.println("recipents " + list_recp);


    }

}

Upvotes: 0

Views: 296

Answers (1)

Frederic
Frederic

Reputation: 1028

The list_donors.size() will always return 0, cause the list_donors is empty when it begins

so the code never call file_donors.nextLine()

and file_donors.hasNextLine() will be always true

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++) {
               ...
               }
            }

you can avoid this kind of situation doing something like

  while (file_donors.hasNextLine())
  {
    string current_line = file_donors.hasNextLine();

updating with some code to help

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);
  }
}

Upvotes: 1

Related Questions