My code
[
public class Student{
private String lastName;
private String firstName;
private String middleName;
private int sid;
private double units;
private String Programs;
public Student(){
this( "", "", "", 0, 0.0, "");
}
public Student (String lastName, String firstName, String middleName, int sid, double units, String Programs )
{
//setLastName(lastName);
setFirstName(firstName);
setMiddleName(middleName);
setsid(sid);
setunits(units);
setPrograms(Programs);
}
public void setLastName(String lastName){
lastName = lastName;
}
public String getLastName(){
return lastName;
}
public void setFirstName(String firstName){
firstName = firstName;
}
public String getFirstName(){
return firstName;
}
public void setMiddleName(String middleName){
middleName = middleName;
}
public String getMiddleName(){
return middleName;
}
public void setsid(int sid){
sid = sid;
}
public int getsid(){
return sid;
}
public void setunits(double units){
units = units;
}
public double getunits()
{
return units;
}
public void setPrograms(String Programs){
Programs = Programs;
}
public String getPrograms(){
return Programs;
}
}
]
Another class
[
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class Roaster {
Student st[] = new Student[100];
/* public static void copyLines() throws IOException {
BufferedReader inputStream = null;
PrintWriter outputStream = null;
try {
inputStream =
new BufferedReader(new FileReader("roaster.txt"));
for(i=0;i<no.oflines;i++)
{
addstudent(fir,
}
outputStream =
new PrintWriter(new FileWriter("characteroutput.txt"));
String l;
while ((l = inputStream.readLine()) != null) {
outputStream.println(l);
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}*/
public void addStudentfromFile() throws IOException
{
Scanner inputStream = null;
try {
inputStream =
new Scanner(new File("roaster.txt"));
int noofstu = 1;
while (inputStream.hasNext()) {
String temp = inputStream.next();
String[] name = null;
name = temp.split(",");
/* System.out.println(name[0]);
System.out.println(name[1]);
System.out.println(inputStream.next());
System.out.println(inputStream.nextInt());
System.out.println(inputStream.nextDouble());
System.out.println(inputStream.next());*/
st[noofstu-1] = new Student(name[0], name[1], inputStream.next(),inputStream.nextInt() , inputStream.nextDouble(),inputStream.next());
noofstu++;
}
} finally {
if (inputStream != null) {
inputStream.close();
}
}
}
public int getStudentno(){
//getStudent = new Student;
return st.length;
}
public static void main(String[] args) {
}
}
]
Another class for testing
[
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
public class RoasterTest {
public static void main (String[] args) throws Exception{
File RoasterTest = new File( "roaster.txt" );
if ( RoasterTest.exists() )
{
// Create the buffered reader for reading the file
BufferedReader inFile = new BufferedReader( new FileReader( RoasterTest ) );
// Get the first line of the file
String line = inFile.readLine();
// If line is not end of file continue
while ( line != null )
{
// Create a StringTokenizer with a colon sign as a delimiter
java.util.StringTokenizer st =
new java.util.StringTokenizer( line, ":" );
// Display the content of the first token
System.out.print( " Name: " + st.nextToken() );
// Display the total number of tokens remaining this string
int numScores = st.countTokens();
// Initialize the sum to zero
int sum = 0;
// Get each score, add it to the sum and print it
for ( int i=1; i <= numScores; i++ )
{
int score = Integer.parseInt( st.nextToken() );
sum += score;
}
// Display the average score for this student
System.out.println( " average = " + sum/numScores );
// Read next line of the file
line = inFile.readLine();
} // end while not at end of file
// Close the BufferedReader
inFile.close();
// try {
// RoasterTest.clone();
// } catch (IOException ioe) {
} // end if the grade file doesn't exist
}
}
]
My test class does not read student class or roaster class .. I know I am missing something but I am loss
8 2363
My code
[
public class Student{
private String lastName;
private String firstName;
private String middleName;
private int sid;
private double units;
private String Programs;
public Student(){
this( "", "", "", 0, 0.0, "");
}
public Student (String lastName, String firstName, String middleName, int sid, double units, String Programs )
{
//setLastName(lastName);
setFirstName(firstName);
setMiddleName(middleName);
setsid(sid);
setunits(units);
setPrograms(Programs);
}
public void setLastName(String lastName){
lastName = lastName;
}
public String getLastName(){
return lastName;
}
public void setFirstName(String firstName){
firstName = firstName;
}
public String getFirstName(){
return firstName;
}
public void setMiddleName(String middleName){
middleName = middleName;
}
public String getMiddleName(){
return middleName;
}
public void setsid(int sid){
sid = sid;
}
public int getsid(){
return sid;
}
public void setunits(double units){
units = units;
}
public double getunits()
{
return units;
}
public void setPrograms(String Programs){
Programs = Programs;
}
public String getPrograms(){
return Programs;
}
}
]
Another class
[
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class Roaster {
Student st[] = new Student[100];
/* public static void copyLines() throws IOException {
BufferedReader inputStream = null;
PrintWriter outputStream = null;
try {
inputStream =
new BufferedReader(new FileReader("roaster.txt"));
for(i=0;i<no.oflines;i++)
{
addstudent(fir,
}
outputStream =
new PrintWriter(new FileWriter("characteroutput.txt"));
String l;
while ((l = inputStream.readLine()) != null) {
outputStream.println(l);
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}*/
public void addStudentfromFile() throws IOException
{
Scanner inputStream = null;
try {
inputStream =
new Scanner(new File("roaster.txt"));
int noofstu = 1;
while (inputStream.hasNext()) {
String temp = inputStream.next();
String[] name = null;
name = temp.split(",");
/* System.out.println(name[0]);
System.out.println(name[1]);
System.out.println(inputStream.next());
System.out.println(inputStream.nextInt());
System.out.println(inputStream.nextDouble());
System.out.println(inputStream.next());*/
st[noofstu-1] = new Student(name[0], name[1], inputStream.next(),inputStream.nextInt() , inputStream.nextDouble(),inputStream.next());
noofstu++;
}
} finally {
if (inputStream != null) {
inputStream.close();
}
}
}
public int getStudentno(){
//getStudent = new Student;
return st.length;
}
public static void main(String[] args) {
}
}
]
Another class for testing
[
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
public class RoasterTest {
public static void main (String[] args) throws Exception{
File RoasterTest = new File( "roaster.txt" );
if ( RoasterTest.exists() )
{
// Create the buffered reader for reading the file
BufferedReader inFile = new BufferedReader( new FileReader( RoasterTest ) );
// Get the first line of the file
String line = inFile.readLine();
// If line is not end of file continue
while ( line != null )
{
// Create a StringTokenizer with a colon sign as a delimiter
java.util.StringTokenizer st =
new java.util.StringTokenizer( line, ":" );
// Display the content of the first token
System.out.print( " Name: " + st.nextToken() );
// Display the total number of tokens remaining this string
int numScores = st.countTokens();
// Initialize the sum to zero
int sum = 0;
// Get each score, add it to the sum and print it
for ( int i=1; i <= numScores; i++ )
{
int score = Integer.parseInt( st.nextToken() );
sum += score;
}
// Display the average score for this student
System.out.println( " average = " + sum/numScores );
// Read next line of the file
line = inFile.readLine();
} // end while not at end of file
// Close the BufferedReader
inFile.close();
// try {
// RoasterTest.clone();
// } catch (IOException ioe) {
} // end if the grade file doesn't exist
}
}
]
My test class does not read student class or roaster class .. I know I am missing something but I am loss
Use Code Tags.
Please post the file content.
Debasis Jana
My code
[
public class Student{
private String lastName;
private String firstName;
private String middleName;
private int sid;
private double units;
private String Programs;
public Student(){
this( "", "", "", 0, 0.0, "");
}
public Student (String lastName, String firstName, String middleName, int sid, double units, String Programs )
{
//setLastName(lastName);
setFirstName(firstName);
setMiddleName(middleName);
setsid(sid);
setunits(units);
setPrograms(Programs);
}
public void setLastName(String lastName){
lastName = lastName;
}
public String getLastName(){
return lastName;
}
public void setFirstName(String firstName){
firstName = firstName;
}
public String getFirstName(){
return firstName;
}
public void setMiddleName(String middleName){
middleName = middleName;
}
public String getMiddleName(){
return middleName;
}
public void setsid(int sid){
sid = sid;
}
public int getsid(){
return sid;
}
public void setunits(double units){
units = units;
}
public double getunits()
{
return units;
}
public void setPrograms(String Programs){
Programs = Programs;
}
public String getPrograms(){
return Programs;
}
}
]
Another class
[
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class Roaster {
Student st[] = new Student[100];
/* public static void copyLines() throws IOException {
BufferedReader inputStream = null;
PrintWriter outputStream = null;
try {
inputStream =
new BufferedReader(new FileReader("roaster.txt"));
for(i=0;i<no.oflines;i++)
{
addstudent(fir,
}
outputStream =
new PrintWriter(new FileWriter("characteroutput.txt"));
String l;
while ((l = inputStream.readLine()) != null) {
outputStream.println(l);
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}*/
public void addStudentfromFile() throws IOException
{
Scanner inputStream = null;
try {
inputStream =
new Scanner(new File("roaster.txt"));
int noofstu = 1;
while (inputStream.hasNext()) {
String temp = inputStream.next();
String[] name = null;
name = temp.split(",");
/* System.out.println(name[0]);
System.out.println(name[1]);
System.out.println(inputStream.next());
System.out.println(inputStream.nextInt());
System.out.println(inputStream.nextDouble());
System.out.println(inputStream.next());*/
st[noofstu-1] = new Student(name[0], name[1], inputStream.next(),inputStream.nextInt() , inputStream.nextDouble(),inputStream.next());
noofstu++;
}
} finally {
if (inputStream != null) {
inputStream.close();
}
}
}
public int getStudentno(){
//getStudent = new Student;
return st.length;
}
public static void main(String[] args) {
}
}
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
public class RoasterTest {
public static void main (String[] args) throws Exception{
File RoasterTest = new File( "roaster.txt" );
if ( RoasterTest.exists() )
{
// Create the buffered reader for reading the file
BufferedReader inFile = new BufferedReader( new FileReader( RoasterTest ) );
// Get the first line of the file
String line = inFile.readLine();
// If line is not end of file continue
while ( line != null )
{
// Create a StringTokenizer with a colon sign as a delimiter
java.util.StringTokenizer st =
new java.util.StringTokenizer( line, ":" );
// Display the content of the first token
System.out.print( " Name: " + st.nextToken() );
// Display the total number of tokens remaining this string
int numScores = st.countTokens();
// Initialize the sum to zero
int sum = 0;
// Get each score, add it to the sum and print it
for ( int i=1; i <= numScores; i++ )
{
int score = Integer.parseInt( st.nextToken() );
sum += score;
}
// Display the average score for this student
System.out.println( " average = " + sum/numScores );
// Read next line of the file
line = inFile.readLine();
} // end while not at end of file
// Close the BufferedReader
inFile.close();
// try {
// RoasterTest.clone();
// } catch (IOException ioe) {
} // end if the grade file doesn't exist
}
}
]
This is my code
Look, have look at "ObjectInputStream","ObjectOutputStream" and "Serialization" in java.
Have a look through Google.
Any further problem, please post here.
Debasis Jana
I still don't get it please help me
I still don't get it please help me
Look,
"ObjectInputStream" for object reading from a stream,
and "ObjectOutputStream" for object writing for a strean.
Now the Object should implement "Serializable" interface.
Have a look at this sample code ............... -
ObjectOutputStream o = new OutjectOutputStream(new FileOutputStream("test.dat"));
-
Student s = new Student(/*Some data goes here*/);
-
o.writeObject(s);
-
.
-
.
-
ObjectInputStream i = new ObjectInputStream(new FileInputStream("test.dat"));
-
Student _s = (Student)i.readObject();
-
//now access the _s object.
-
Your Student class should be like this ....... -
class Student implements Serializable
-
{
-
//your code goes here
-
}
-
Debasis Jana
The
Code[
ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("roaster.txt"));
Student s = new Student("String lastName, String firstName, String middleName, int sid, double units, String Programs", null, null, 0, 0, null);
o.writeObject(s);
ObjectInputStream i = new ObjectInputStream(new FileInputStream("roaster.txt"));
Student _s = (Student)i.readObject();
// now access the _s object.
]
Goes in Roaster class
You got your code worked?
Actually I forgot to close the streams.
See the code carefully. -
ObjectOutputStream o = new OutjectOutputStream(new FileOutputStream("test.dat"));
-
Student s = new Student(/*Some data goes here*/);
-
o.writeObject(s);
-
o.close();
-
.
-
.
-
ObjectInputStream i = new ObjectInputStream(new FileInputStream("test.dat"));
-
Student _s = (Student)i.readObject();
-
i.close();
-
//now access the _s object.
-
Your Student class should be like this ....... -
class Student implements Serializable
-
{
-
//your code goes here
-
}
-
Debasis Jana
I am still having problems I don't where to put the object stream
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Yang Li Ke |
last post by:
Hi guys!
I have some datas that I must check
everytime a visitor comes to my site
What is better to do:
1- Read data from a file
or
2- Read...
|
by: ZafT |
last post by:
Thanks in advance for any tips that might get me going in the right
direction.
I am working on a simple exercise for school that is supposed to...
|
by: cnu |
last post by:
My program generates a log file for every event that happens in the program.
So, I open the file and keep it open till the end. This is how I open...
|
by: ESPN Lover |
last post by:
Below is two snippets of code from MSDN showing how to read a file. Is one
way preferred over the other and why? Thanks.
using System;
using...
|
by: a |
last post by:
I have a struct to write to a file
struct _structA{
long x;
int y;
float z;
}
struct _structA A;
//file open
write(fd,A,sizeof(_structA));...
|
by: lovecreatesbea... |
last post by:
The condition at line 31 is added to check if the program finished to
read the whole file. Is it needed and correct? Thank you.
#include...
|
by: lovecarole |
last post by:
hi, i am the student who should write a program about reading wav file and do the DFT.
actually i don't know how to read data of the wav song and...
|
by: Thomas Kowalski |
last post by:
Hi,
currently I am reading a huge (about 10-100 MB) text-file line by line
using
fstreams and getline. I wonder whether there is a faster way to...
|
by: flebber |
last post by:
I was working at creating a simple program that would read the content
of a playlist file( in this case *.k3b") and write it out . the
compressed...
|
by: Kevin Ar18 |
last post by:
I posted this on the forum, but nobody seems to know the solution: http://python-forum.org/py/viewtopic.php?t=5230
I have a zip file that is...
|
by: tammygombez |
last post by:
Hey fellow JavaFX developers,
I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
|
by: tammygombez |
last post by:
Hey everyone!
I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
|
by: concettolabs |
last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
|
by: teenabhardwaj |
last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
|
by: CD Tom |
last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
| |