tags:

views:

88

answers:

3

Hi All,

pl correct the prg.

Q. Write a java prg to accept RollNo, Name of a Student and store it in Student.txt.Accept n records and store it.

Following are the drawbacks of o/p produced 1. When we write rno i.e. int {roll no of stud} to the file "Student.txt" it does not write the int value

i/p

C:\j2sdk1.4.1_01\bin>java StudentInfo
Enter how many students
3
Student 1
Enter Roll no.:
1
Enter Name :
Geeta
Student 2
Enter Roll no.:
2
Enter Name :
Pinky
Student 3
Enter Roll no.:
3
Enter Name :
Shaily

o/p written to file "student.txt" is GeetaPinkyShaily

2) When we print the values in Student.txt then student roll no is displayed correctly loop used is

while((b=fin.read())!=-1)
{
 System.out.print(b);
}

But as char c=(char) b; is not done it does not print the name

C:\j2sdk1.4.1_01\bin>java StudentInfo
Enter how many students
3
Student 1
Enter Roll no.:
1
Enter Name :
Geeta
Geeta
Student 2
Enter Roll no.:
2
Enter Name :
Pinky
Pinky
Student 3
Enter Roll no.:
3
Enter Name :
Shaily
Shaily
Contents of "Student.txt" are :
1711011011169728010511010712138310497105108121
C:\j2sdk1.4.1_01\bin>


                              or

vice versa

while((b=fin.read())!=-1)
{
 char ch=(char) b;
 System.out.print(ch);
}

prints the the names correctly but not roll no

Following is the code. Kindly make the necessary changes......................

import java.io.*;
class StudentInfo
{
 int rno;
 String name;
 StudentInfo()
 {
  rno=0;
  name="";
 }
 void getDetails() throws IOException
 {
  BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  System.out.println("Enter Roll no.: ");
  rno=Integer.parseInt(br.readLine());
  System.out.println("Enter Name : ");
  name=br.readLine();
 }
 public static void main(String[] args) throws IOException
 {
  FileOutputStream fout=null;
  FileInputStream fin;
  int n;
  char space=' ';
  try
  {
   //open Student.txt
   try
   {
    fout=new FileOutputStream("Student.txt");
   }
   catch(FileNotFoundException e)
   {
    System.out.println("Error opening the file Student.txt");
    System.exit(0);
   }
   BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
   System.out.println("Enter how many students");
   n=Integer.parseInt(br.readLine());
   StudentInfo s[]=new StudentInfo[n];
   for(int i=0;i<n;i++)
   {
    s[i]=new StudentInfo();
    System.out.println("Student "+(i+1));
    s[i].getDetails();
    int rollno=s[i].rno;
    fout.write(rollno);
    char c[]=s[i].name.toCharArray();
    for(int z=0;z<c.length;z++)
    fout.write(c[z]);
   }
   fout.close();
   System.out.println("Contents of \"Student.txt\" are : ");
   fin=new FileInputStream("Student.txt");
   int b;
   while((b=fin.read())!=-1)
   {
    System.out.print(b);
   }
   fin.close();
  }
  catch(Exception e)
  {
   System.out.println(e.getMessage());
  }
 }
}

I guess if we can distinguish between the roll no and name we can solve this. In the o/p file I want o/p as rno name rno name rno name i.e. 1 Geeta 2 Pinky 3 Shaily

Then, how to insert it ?

if I declare as

char ch=' ';
fout.write(ch);

it writes 32

And even if

char ch=' ';
fout.write((int)ch);

it wites 32 to Student.txt

+1  A: 

You could use DataOutputStream here. FileOutputStream can write everything as bytes only.Please refer this Sun API Reference

In this you could use writeInt for RollNumber and writeChars for Name. It will be also better if you write each record in a separate line inside the file just for better understanding :)

Aviator
+2  A: 

Don't use an OutputStream directly to write text data. Use a Writer of some form, so you can write strings out instead.

The absolute simplest approach would be to open a PrintWriter, but that masks errors. The next level up is to use a FileWriter, but that will always use the system default encoding. It may be good enough for your purposes.

Converting numbers into text is normally a culture-dependent process, but again I'll ignore that for the moment for the sake of simplicity. So, using FileWriter wrapped in a BufferedWriter (to make it easier to write a new line):

BufferedWriter writer = new BufferedWriter(new FileWriter("Student.txt"));
try
{
    for(int i=0;i<n;i++)
    {
        s[i]=new StudentInfo();
        System.out.println("Student "+(i+1));
        s[i].getDetails();
        writer.write(s[i].rno + ": " + s[i].name);
        writer.newline();
    }
}
finally
{
    writer.close(); // Close output even if there's an error
}

There are various other aspects of the code which aren't ideal, but this should at least help with the file format...

Jon Skeet
+1  A: 

If you ask the community to correct your programs you will only get downvotes. Instead highlight your specific question. In this case it is why does

char ch=' ';
fout.write(ch);

Outputs 32. Think about it..why does System.out.print the right thing but not fout.write? What is the difference between fout and System.out, if any? What is difference between write and print, if any? Look up the documentation.

Best of luck with your homework, at least you didn't pretend that it is not.

Hemal Pandya