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 <Driver> drivers = new ArrayList <Driver> ();

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);



}
}

Getters and setters are quite simple.

A getter is a method that takes no arguments and returns the value of a particular field. (If you haven't heard the term "field" before, it's the same as "instance variable".) Its return type is the same as that field's type.

A setter is a void method that takes one argument and sets a particular field to the value of that argument. The argument's type is the same as the field's type.

Getters and setters are necessary because another class can't access a private field directly. Making fields private is good practice because it allows you to control what goes in them. For instance, you can stop someone from trying to use a negative number for distance.

Looking at your code right now, I see some problems. First, your constructor takes no arguments, while your call to it in the first class provides six. (Also, you can't pass anything to a properly written getter, and you generally should do something with the return value of a getter instead of just leaving it there.) I'm assuming that you intend that someone creating a new Driver has to pass the six values to it, and the new Driver's fields are set to those arguments. You don't need to use getters and setters for that part; you can just set the values directly in the constructor.

Second, once you leave a loop, any variables declared inside it are gone and cannot be used later. Driver d is declared inside a for loop, so your use of it at the bottom of your first class is illegal. I strongly suspect that that was never what you really wanted to do. Look back at your assignment or specification and figure out what you're supposed to do with the Driver objects once you have them all in a list.

Finally, it is a very good idea to indent your code. It makes it a lot more readable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.