Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Entering objects from a file into an array

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Entering objects from a file into an array

    Hello,

    I am a new programmer and am having trouble with some instructions i have to create a program. I am suppose to make a service class called CreateVehicles. In this class I am supposed to declare some instance variables, then create constructors, mutators, accessors, and a helper method. I think my code is fine for the first three, but the compiler complains about the way I try to enter the data from the file into an array using a while loop. These are the instructions I was given.

    Create a class called CreateVehicles. This class acts as a second service class. The task of this class is to provide instance variables , constructors, accessor/mutator methods and a helper method that allow the following tasks to be performed:
    The helper method has the task of reading the text file (Vehicle.txt) that contains information about different vehicles (one line per Vehicle). You need to tokenize each line read by the scanner in order to get the individual pieces of data that the constructor of the Vehicle class needs. Therefore, for each line of data read you create a Vehicle object and then you enter that object into an array of Vehicle type. The method returns the array with the vehicle objects.
    Decide which instance variables you need to declare for this class based on the description of its task.
    Create the text file Vehcicles.txt using Notepad with the following data (similar to the project data):

    An this is what I have for the class.

    import java.io.File;
    import java.io.IOException;
    import java.util.Scanner;
    public class CreateVehicles
    {
    String cartype = " ";
    String cardrive = " ";
    int carprice = 0;
     
    public CreateVehicles()
    {
    cartype = "anytype";
    cardrive = "anydrive";
    carprice = 0;
    }
     
    public CreateVehicles(String ct, String cd, int cp)
    {
    cartype = ct;
    cardrive = cd;
    carprice = cp;
    }
     
    public String getcartype()
    {
    return cartype;
    }
     
    public String getcardrive()
    {
    return cardrive;
    }
     
    public int getcarprice()
    {
    return carprice;
    }
     
    public void setcartype(int cat)
    {
    cartype = cat;
    }
     
    public void setcardrive(int cad)
    {
    cardrive = cad;
    }
     
    public void setcarprice(int cap)
    {
    carprice = cap;
    }
     
    public CreateVehicles [] createArray()
    {
    CreateVehicles [] myvehicles = new CreateVehicles[8];
    try
    {
    File myfile = new File("Vehicle.txt");
    Scanner scan = new Scanner(myfile);
    while(scan.hasNext())
    {
    str=scan.next();
    str.myvehicles[];
    }
    }
    catch(IOException ion)
    {
    System.out.println("The file cannot be read");
    }
    }
    }

    I am certain I messed something up in the while loop. Any input or hints would be greatly appreciated.
    Thanks.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: Entering objects from a file into an array

    If you are getting errors then post the exact error messages. We don't read minds.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Entering objects from a file into an array

    Alright the error the compiler gives is


    CreateVehicles.java:64: not a statement
    str.myvehicles[];
    ^
    1 error

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: Entering objects from a file into an array

    Looks like you need to revise how arrays work.
    Improving the world one idiot at a time!

Similar Threads

  1. all objects in array the same?
    By abrohm in forum What's Wrong With My Code?
    Replies: 6
    Last Post: June 17th, 2011, 11:21 AM
  2. instantiating class objects from an array
    By BadAnti in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 12th, 2011, 03:27 PM
  3. Not entering while loop
    By Kakashi in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 6th, 2011, 01:15 PM
  4. FAQ: find variable in an array of objects
    By dalek in forum Object Oriented Programming
    Replies: 1
    Last Post: April 5th, 2010, 12:40 PM
  5. [SOLVED] Creation of objects of Array in Java
    By sadi_shihab in forum Collections and Generics
    Replies: 4
    Last Post: July 9th, 2009, 01:38 PM