0

need to combine two text files of girl names and boy names into one text file. the new file has to have the boy names and girl names separated into two lists of each gender, and we dont know how many names each file will have. the program runs but gets stuck in an infinite loop

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.Scanner;

public class NameTester 
{
    public static void main(String args[]) throws FileNotFoundException
    {
        Scanner userIn = new Scanner(System.in);
        System.out.println("Enter file name for boys name file: ");
        String boyFile = userIn.next();
        System.out.println("Enter file name for girls name file: ");
        String girlFile = userIn.next();
        userIn.close();

        Scanner boyIn = new Scanner(new FileReader(boyFile));
        Scanner girlIn = new Scanner(new FileReader(girlFile));

        PrintWriter out = new PrintWriter("names.txt");
        out.print("Boys Names:                  Girls Names: ");
        int count = 1;
        while(boyIn.hasNextLine() || girlIn.hasNextLine());
        {
            String b = boyIn.next();
            String g = girlIn.next();
            out.print(count + " " + b + "                   " + count + " " + g);
            count++;
        }

        boyIn.close();
        girlIn.close();
        out.close();
   }
}
EJoshuaS - Reinstate Monica
  • 10,460
  • 46
  • 38
  • 64
jacobc55
  • 11
  • 1

1 Answers1

2

This line is an empty while loop that will run forever:

while(boyIn.hasNextLine() || girlIn.hasNextLine());

Get rid of the semicolon at the end:

while(boyIn.hasNextLine() || girlIn.hasNextLine()) // <- NO SEMICOLON
{
    ....
}

I haven't checked for other logic errors in your program, but this should get rid of the infinite loop.

Ted Hopp
  • 222,293
  • 47
  • 371
  • 489