Solved

making a new constructor

Posted on 2001-06-04
6
147 Views
Last Modified: 2010-03-31
I want for a class a new constructor that reads in data.

This is my code

public class TrainClass {
  String  Name;
  int     DecNum;
  int     TDecType;
  int     SpdStp;
  int     MaxSpd;
  int     MinSpd;
  int     HalfSpd;
  int     Acc;
  int     Decc;
  String  PictureBig;
  public  TrainClass() {
  }

  void ReadFromFile(FileInputStream is) throws IOException {
    DataInputStream in = new DataInputStream(is);
    Name = in.readUTF();
    DecNum = in.readInt();
    TDecType = in.readInt();
    SpdStp = in.readInt();
    MaxSpd = in.readInt();
    MinSpd = in.readInt();
    HalfSpd = in.readInt();
    Acc = in.readInt();
    Decc = in.readInt();
    PictureBig = in.readUTF();
    in.close();
  }

  void WriteToFile(FileOutputStream os) throws IOException {
    DataOutputStream out = new DataOutputStream(os);
    out.writeUTF(Name);
    out.writeInt(DecNum);
    out.writeInt(TDecType);
    out.writeInt(SpdStp);
    out.writeInt(MaxSpd);
    out.writeInt(MinSpd);
    out.writeInt(HalfSpd);
    out.writeInt(Acc);
    out.writeInt(Decc);
    out.writeUTF(PictureBig);
    out.flush();
    out.close();
  }
}

The ReadFromFile needs to be a constructor, but what ever I try, it keeps giving errors.

I want to call
TrainClass train = new ReadFromFile(a fileinputstream);

Thanks for any help

Robert Nieuwland
0
Comment
Question by:rnieuwl
6 Comments
 
LVL 4

Expert Comment

by:kylar
ID: 6153544
Change your constructor to look like this:

public TrainClass(FileInputStream fis){
    ReadFromFile(fis);
}

Cheers,
Kylar
0
 
LVL 4

Expert Comment

by:kylar
ID: 6153546
Whoops, I forgot to add that when you instantiate your class, do this:

TrainClass myTrain = new TrainClass(myFileInputStream);

:)
Kylar
0
 
LVL 2

Expert Comment

by:Tom7
ID: 6153553
Hi

in Java, constructors must always have the same name as the class itself. In your example, you should write a second constructor which takes a FileInputStream as an argument.


public class TrainClass {
  ...
  ...
  public TrainClass(FileInputStream is) {
    ReadFromFile(is);
  }
  ...
  ...
}
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:rnieuwl
ID: 6155414
This looks rather confusing to me.
I have changed the code so it looks

public class TrainClass {
  String  Name;
  int     DecNum;
  int     TDecType;
  int     SpdStp;
  int     MaxSpd;
  int     MinSpd;
  int     HalfSpd;
  int     Acc;
  int     Decc;
  String  PictureBig;
  public  TrainClass() {}
  public  TrainClass(FileInputStream fis) throws IOException{
    ReadFromFile(fis);
}

  void ReadFromFile(FileInputStream fis) throws IOException{
    try {
         DataInputStream in = new DataInputStream(fis);
         Name = in.readUTF();
         DecNum = in.readInt();
         TDecType = in.readInt();
         SpdStp = in.readInt();
         MaxSpd = in.readInt();
         MinSpd = in.readInt();
         HalfSpd = in.readInt();
         Acc = in.readInt();
         Decc = in.readInt();
         PictureBig = in.readUTF();
         in.close();
        }
    catch (IOException e_io) {};
  }
}

Do I need to put constructor or void in front of the second ReadFromFile?
What if I changed the class TrainClass to

public class DecoderClass {
  int DecNum;
  int DecType;
  public DecoderClass(){}
  public DecoderClass(FileInputStream fis) {
    ReadFromFile(fis);
}

public class TrainClass extends DecoderClass {
  String  Name;
  int     SpdStp;
  int     MaxSpd;
  int     MinSpd;
  int     HalfSpd;
  int     Acc;
  int     Decc;
  String  PictureBig;
  public  TrainClass() {}
  public  TrainClass(FileInputStream fis) throws IOException{
    ReadFromFile(fis); }

Thus this means that when I do
  TrainClass myTrain = new TrainClass(fis);
I automaticly read the variables from DecoderClass?

Thanks Rob Nieuwland

0
 
LVL 2

Accepted Solution

by:
mariuso earned 100 total points
ID: 6155625
No, you don't need void there (only public).
In constr TrainClass you don't need any Read.... . You have to put only super(fis);

0
 

Author Comment

by:rnieuwl
ID: 6156567
Thanks to all
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now