Solved

making a new constructor

Posted on 2001-06-04
6
150 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 74
How to overcome error: The method of type x must override y or implement a supertype method 4 30
Java array 21 139
Java pass by reference 3 69
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.
Suggested Courses

740 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