ArrayList Help using get and set methods

Hi all! I am extremely new to java and to computer programming in general so I greatly appreciate all our help. Here's my problem, I am trying to write a program that reads in a basic text file with some names and some numbers, and inputs the given information into an array list. Each line of the text file is supposed to serve as a separate object in the array list. The major problem for me is that I am required to use 2 classes to do this. One class must be the main class and the other class is supposed to hold Get and Set methods for each piece of information in each object in the ArrayList, and I am very clueless as to how to use get and set methods. Here is the code that I have so far....i know the Driver class is where the get and set methods are supposed to go...i just dont know what they are supposed to look like or how to do them. Please help me if you can, I am very desperate! Thank you so much in advance.

Here is the main code:

import java.util.ArrayList;
import java.util.Scanner;
import java.util.*;
import java.io.*;
import java.awt.*;

public class C_Part {

/********************************************************************
Creates the C_Part Class
********************************************************************/
public static void main (String[]args)
{
Scanner scan = new Scanner(new File(args[0]));
Scanner scanner = null;
ArrayList drivers = new ArrayList ();

if (args.length != 1)
{
System.out.println ("The name of the file to be read for " +
" the report is expected.");
System.exit(1);
}
try
{
File file = new File (args[0]);
scanner = new Scanner (file);
}
catch(IOException ioe)
{
System.out.println ("The file " + args[0] + " does not exist");
System.exit(1);
}

int count = 0;


while (scanner.hasNext()){
scanner.nextLine();
count++;
}
for (int i=0; i<count; i++){



String Fname = scanner.next();
String Lname = scanner.next();
double fares = scanner.nextDouble();
double gaspaid = scanner.nextDouble();
double distance = scanner.nextDouble();
double gallons = scanner.nextDouble();

Driver d = new Driver(Fname, Lname, fares,
gaspaid, distance, gallons);

drivers.add(d);
}



d.setfirstname(Fname);
d.setlastname(Lname);
}
}


And here is the Driver that is supposed to hold get and sets!

public class Driver
{
public String Fname;
public String Lname;
private double fares;
private double gaspaid;
private double distance;
private double gallons;

public Driver (){
getFname(String firstname);

setFname(String firstname);



}
}

Comments

  • have you tried looking at the Java APIs?

    http://download.oracle.com/javase/6/docs/api/

    In the second box in the lower left of this link locate ArrayList and click on it and it will bring up the Method and Constructor Summaries that ArrayList uses and it tells you how to implement them.

    in the method summary you will find:

    get(int index)
    Returns the element at the specified position in this list.

    and

    set(int index, E element)
    Replaces the element at the specified position in this list with the specified element.

    try working with this for a bit and if you have more questions then I will help you out further. After all, isnt it better to learn by doing then me just telling you how. Ive given you the tools to work through it. :D
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion