Solved

making a new constructor

Posted on 2001-06-04
6
146 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
Comment Utility
Change your constructor to look like this:

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

Cheers,
Kylar
0
 
LVL 4

Expert Comment

by:kylar
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:rnieuwl
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks to all
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

763 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

6 Experts available now in Live!

Get 1:1 Help Now