How to use ArrayList while adding something to another Class's constructor ?

Mert Serin

I'm try to create one simple reservation system, we'll read a file, then we'll add Train, Bus, etc., then we'll writer everything to output.

import java.io.*;
import java.util.*;
public class Company
{

private static ArrayList<Bus> bus = new ArrayList<Bus>();
static int buscount = 0, traincount = 0;
public static void main (String[] args) throws IOException
{

    FileParser();
}
public Company()
{

}

public static void FileParser()
{

     try { 
          File file = new File(); //i fill this later
          File file2 = new File(); // i fill this later
          FileInputStream fis = new FileInputStream(file); 
          FileOutputStream fos = new FileOutputStream(file2); 
          BufferedReader br = new BufferedReader(new InputStreamReader(fis)); 
          BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos)); 
          String line; 
          while ((line = br.readLine()) != null) 
        { 
            String[] splitted = line.split(",");
            if(splitted[0].equals("ADDBUS"))
            {
                bus.add(buscount) = Bus(splitted[0],splitted[1],splitted[2],splitted[3],splitted[4],splitted[5]);
            }

        }
     }
        catch (FileNotFoundException fnfe) { 

        } 
        catch (IOException ioe) { 

        } 
     }
}

I try to read the file line by line. For example one of the line is "ADDBUS,78KL311,10,140,54" I split the line for "," then i try to add every pieces of array to Bus' class' constructor but i couldn't figured it out.

My Bus Class is like ` public class Bus extends Vehicle{

private String command;
private String busName;
private String busPlate;
private String busAge;
private String busSpeed;
private String busSeat;

public Bus(String command, String busname, String busplate, String busage, String busspeed, String busseat)
{
    this.command = command;
    this.busName = busname;
    this.busPlate = busplate;
    this.busAge = busage;
    this.busSpeed = busspeed;
    this.busSeat = busseat;
}

public String getBusName() {
    return busName;
}

public void setBusName(String busName) {
    this.busName = busName;
}

public String getBusPlate() {
    return busPlate;
}

public void setBusPlate(String busPlate) {
    this.busPlate = busPlate;
}

public String getBusAge() {
    return busAge;
}

public void setBusAge(String busAge) {
    this.busAge = busAge;
}

public String getBusSpeed() {
    return busSpeed;
}

public void setBusSpeed(String busSpeed) {
    this.busSpeed = busSpeed;
}

public String getBusSeat() {
    return busSeat;
}

public void setBusSeat(String busSeat) {
    this.busSeat = busSeat;
}

public String getCommand() {
    return command;
}

public void setCommand(String command) {
    this.command = command;
}
   } 

can someone show me a way to solve this problem?

Thank you,

Jean Logeart

You are missing the keyword new to create a new instance of the class:

bus.add(new Bus(...));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to call on something from another class in Java and use it's Array?

From Dev

Adding a constructor's properties to an ArrayList?

From Dev

Adding data into arrayList of another class

From Dev

How to use an ArrayList from one class in another?

From Dev

How to use base constructor data to another constructor in the same class?

From Dev

How to use base constructor data to another constructor in the same class?

From Dev

java: Use Constructor of another class

From Dev

Adding objects to ArrayList from another class

From Dev

Adding objects to an ArrayList from another class

From Dev

Adding objects to ArrayList from another class

From Dev

Adding objects to an ArrayList from another class

From Dev

How to use ArrayList to call a method from another class in Java?

From Dev

How to use Class<? extends Something>?

From Dev

How to use Class<? extends Something>?

From Dev

Something is wrong in a constructor of a class

From Dev

how to take something from another class in python?

From Dev

How can I use constructor of one class as a parameter to an object in another class in Java?

From Dev

How to use an ArrayList<String> as a parameter in a constructor in java?

From Dev

Java - adding value from TextField to object constructor of another class

From Dev

Add something to arraylist while checking it

From Dev

How to add an object from an ArrayList to another ArrayList in another class

From Dev

How to use a class in another class

From Dev

Adding one arraylist to another

From Dev

How to use templated class in constructor for a different class

From Dev

Adding new objects to an ArrayList in constructor

From Dev

Adding new objects to an ArrayList in constructor

From Dev

Adding to an ArrayList using a perameterized constructor

From Dev

How to save a class instance in an ArrayList that is in the class? It has to be done in the constructor (Java)

From Dev

Error while passing an object by reference to another class' constructor

Related Related

  1. 1

    How to call on something from another class in Java and use it's Array?

  2. 2

    Adding a constructor's properties to an ArrayList?

  3. 3

    Adding data into arrayList of another class

  4. 4

    How to use an ArrayList from one class in another?

  5. 5

    How to use base constructor data to another constructor in the same class?

  6. 6

    How to use base constructor data to another constructor in the same class?

  7. 7

    java: Use Constructor of another class

  8. 8

    Adding objects to ArrayList from another class

  9. 9

    Adding objects to an ArrayList from another class

  10. 10

    Adding objects to ArrayList from another class

  11. 11

    Adding objects to an ArrayList from another class

  12. 12

    How to use ArrayList to call a method from another class in Java?

  13. 13

    How to use Class<? extends Something>?

  14. 14

    How to use Class<? extends Something>?

  15. 15

    Something is wrong in a constructor of a class

  16. 16

    how to take something from another class in python?

  17. 17

    How can I use constructor of one class as a parameter to an object in another class in Java?

  18. 18

    How to use an ArrayList<String> as a parameter in a constructor in java?

  19. 19

    Java - adding value from TextField to object constructor of another class

  20. 20

    Add something to arraylist while checking it

  21. 21

    How to add an object from an ArrayList to another ArrayList in another class

  22. 22

    How to use a class in another class

  23. 23

    Adding one arraylist to another

  24. 24

    How to use templated class in constructor for a different class

  25. 25

    Adding new objects to an ArrayList in constructor

  26. 26

    Adding new objects to an ArrayList in constructor

  27. 27

    Adding to an ArrayList using a perameterized constructor

  28. 28

    How to save a class instance in an ArrayList that is in the class? It has to be done in the constructor (Java)

  29. 29

    Error while passing an object by reference to another class' constructor

HotTag

Archive