Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

making a new constructor

Posted on 2001-06-04
6
Medium Priority
?
157 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
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!

 

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 400 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month20 days, 20 hours left to enroll

810 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