Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Define a java program to write the student class into a file. Register number, course1, mark1, course2 and mark2 are members of the class. Get ‘n’ objects from user and write those into a file. Later, read those objects from the file and find the average marks of each student. Also, count the number of students take the subject "Java Program" as their course1 and display the count.


What I have tried:

import java.io.File;
import java.io.FileOutputStream;
import java.util.Scanner;
import java.io.*;
import java.util.Arrays;
import java.io.DataOutputStream;
public class Student
{
    public static void main(String args[])
    {
        try
        {
            File f=new File("Record.dat");
            FileOutputStream f1=new FileOutputstream(f);
            Scanner sc=new Scanner(System.in);
            DataOutputStream data=new DataOutputstream(f1);
            System.out.println("Enter the number of students:");
            int n=sc.nextInt();
            int reg_no[]=new int[n];
            String c1[]=new String[n];
            int m1[]=new int[n];
            String c2[]=new String[n];
            int m2[]=new int[n];
            int total[]=new int[n];
            int grandtotal=0;
           
            for(int i=0;i<n;i++)
            {
                System.out.println("Enter student "+(i+1)+"details:");
                reg_no[i]=sc.nextInt();
                sc.nextLine();
                
                System.out.println("course1: ");
                c1[i]=sc.nextLine();
                
                System.out.println("course1 mark");
                m1=sc.nextInt();
                
                System.out.println("course 2:");
                c2=sc.nextLine();
                
                System.out.println("course2 mark");
                m2=sc.nextInt();
                
                total[i]=sc.nextInt();
                grandtotal+=total[i];
                
                data.writeInt(reg_no);
                data.writeUTF(c1);
                data.writeInt(m1);
                data.writeUTF(c2);
                data.writeInt(m2);
            
               }
               double avg=grandtotal/(int)n;
               System.out.println("Average mark is: "+avg);
               data.close();
            }
            catch(Exception e)
            {
                System.out.println(e.getMessage());
                e.printStackTrace();
                
            }
    }
}




Getting Errors:
Student.java:14: error: cannot find symbol
            FileOutputStream f1=new FileOutputstream(f);
                                    ^
  symbol:   class FileOutputstream
  location: class Student
Student.java:16: error: cannot find symbol
            DataOutputStream data=new DataOutputstream(f1);
                                      ^
  symbol:   class DataOutputstream
  location: class Student
Student.java:37: error: incompatible types: int cannot be converted to int[]
                m1=sc.nextInt();
                             ^
Student.java:40: error: incompatible types: String cannot be converted to String[]
                c2=sc.nextLine();
                              ^
Student.java:43: error: incompatible types: int cannot be converted to int[]
                m2=sc.nextInt();
                             ^
Student.java:48: error: incompatible types: int[] cannot be converted to int
                data.writeInt(reg_no);
                              ^
Student.java:49: error: incompatible types: String[] cannot be converted to String
                data.writeUTF(c1);
                              ^
Student.java:50: error: incompatible types: int[] cannot be converted to int
                data.writeInt(m1);
                              ^
Student.java:51: error: incompatible types: String[] cannot be converted to String
                data.writeUTF(c2);
                              ^
Student.java:52: error: incompatible types: int[] cannot be converted to int
                data.writeInt(m2);
                              ^

Error: Could not find or load main class Student
Caused by: java.lang.ClassNotFoundException: Student
Posted
Updated 10-Nov-21 20:43pm
v3
Comments
Richard MacCutchan 11-Nov-21 4:13am    
You are mis-spelling the names of classes that you are trying to use: FileOutputstream instead of FileOutputStream and DataOutputstream instead of DataOutputStream. The remaining errors are obvious, you cannot convert a single element to an array, and vice versa.

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Comments
xcommunicado 11-Nov-21 1:57am    
Understable. Could you atleast tell how to approach this question. It would be helpful if you could
OriginalGriff 11-Nov-21 2:23am    
Follow the link at the bottom of my message, and read what it says!
xcommunicado 11-Nov-21 2:32am    
I read it although I ain't a beginner in coding. Thanks anyways!
The approach to this question could be:
  1. Write the student class as requested. The requirements are pretty clear and you may find tons of related examples on the web.
  2. Allow the user to insert objects of the student class. Again you may find many many examples of this very topic on the web.
  3. File I/O: here you (if allowed) may take advantage of Java serialization, see, for instance: Java - Serialization[^].
  4. Counting the students that took the "Java Program" subject is just a matter of iterate (see, for instance: Loops in Java | Java For Loop - Javatpoint[^] ) and compare strings (Java String equals() Method[^]).



[update]
It looks you didn't take the proper OOP path.
Your class should be something similar to
Java
public class Student
{
  int number;
  String course[2];
  int mark[2];
  //...

and then in the main function you need to instantiate objects of the Student class, e.g.
Java
public static void main(String args[])
{
  Student s = new Student();
  // here set the relevant properties
  //..

By the way, the errors you are getting, occur because you are trying to assign to whole arrays, (you should instead assign to array items), for example
Quote:
m1=sc.nextInt();
should actually be
Java
m1[i] = sc.nextInt();
[/update]
 
Share this answer
 
v3
Comments
xcommunicado 11-Nov-21 2:34am    
Thanks!
CPallini 11-Nov-21 3:01am    
You are welcome.
xcommunicado 11-Nov-21 2:44am    
I have updated my code there. If you could help, please do tell
CPallini 11-Nov-21 3:01am    
I have updated my solution as well.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900