This is Java programingWrite a Java program that outputs a l
This is Java programing.Write a Java program that outputs a list of students from the given data file(EnglishMathstusent.txt). You have to use input.hasNext() with while loop when your program reads tokens from the file. We are assuming that you don’t know how many lines in the data file. The file contains class code followed by student name. You are required to use HashSet for this program. Your program outputs the student’s name who
1. takes English or Math
2. takes English and Math
3. takes Math but not English
4. takes English but not Math
The date of EnglishMathstusent.txt
Math Kate
Math Watson
Math Sherry
Math Jack
Math Justin
Math Sophia
Math Elly
Math Ocan
Math Ava
Math Tom
Math Lily
English Kate
English Watson
English Sherru
English Jack
English Justin
English ophia
English Elly
English Ocan
English Isabella
English Mac
Solution
solution
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
public class FileTest {
public static void main(String[] args) throws FileNotFoundException {
HashSet set=new HashSet<String>();
File text = new File(\"D:/test1.txt\");
Scanner scanner =new Scanner(text);
//Reading each line of file using Scanner class
int lineNo = 0;
while(scanner.hasNextLine()){
String line = scanner.nextLine();
set.add(line);
System.out.println(\"line \" + lineNo + \" :\" + line);
++lineNo;
}
Iterator iterator=set.iterator();
while(iterator.hasNext())
{
String line=(String) iterator.next();
System.out.println(line);
}
}
}
output
line 0 :Math Kate
line 1 :Math Watson
line 2 :Math Sherry
line 3 :Math Jack
line 4 :Math Justin
line 5 :Math Sophia
line 6 :Math Elly
line 7 :Math Ocan
line 8 :Math Ava
line 9 :Math Tom
line 10 :Math Lily
line 11 :English Kate
line 12 :English Watson
line 13 :English Sherru
line 14 :English Jack
line 15 :English Justin
line 16 :English ophia
line 17 :English Elly
line 18 :English Ocan
line 19 :English Isabella
line 20 :English Mac
line 21 :
Math Kate
Math Tom
English Mac
Math Sherry
English Sherru
English Justin
English Jack
English Watson
Math Jack
English Elly
English ophia
English Kate
Math Elly
Math Justin
Math Ava
English Isabella
Math Sophia
English Ocan
Math Watson
Math Ocan
Math Lily


