The aim of this task is to create a data file for storing st
The aim of this task is to create a data file for storing student records. Consider a student record consists of following fields namely ID, Last Name, First Name, and Mark. The Mark field contains a number between 0 and 100. Write a Java program with a loop that gets student data (ID, Last Name, First Name, and Mark) from the user and stores those as records in the student.txt file. The user should enter a negative number as ID to signal/indicate the end of the loop.
Solution
import java.io.*;
 import java.util.Scanner;
 public class studentRecords {
    public static void main(String[] args) {
        int id,marks;
        String lastname,firstname;
        File file = new File(\"student.txt\");
        Scanner scan = new Scanner(System.in);
        try {
            file.createNewFile();
            FileWriter writer = new FileWriter(file);
           
            while(true)
            {
                System.out.print(\"Enter the student ID(negative number to quit):\");
                id = scan.nextInt();
                if(id<0)
                {
                    break;
                }
                System.out.print(\"Enter the last name:\");
                lastname = scan.next();
                System.out.print(\"Enter the first name:\");
                firstname = scan.next();
                System.out.print(\"Enter the marks:\");
                marks = scan.nextInt();
                writer.write(id+\" \"+lastname+\" \"+firstname+\" \"+marks+\"\ \");
            }
            writer.flush();
            writer.close();
           
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
       
    }
}

