?
Solved

making a new constructor

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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 Month12 days, 16 hours left to enroll

777 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