First, I want to thank you for helping if you did, if not, thanks for checking it anyway. Second, I have been working on this for awhile, and I am slowly making progress. I am to make an address book, and for the most part I am getting it done with out a hitch. I have 4 other classes besides these, but they are doing just fine. My problem is in my worker outFile.print(); statements. When I try and uncomment them, it tells me it can't find the symbol. When I insert PrintWriter outFile = new PrintWriter(outfile);, I still get the error. Thanks again for the assistance; I really appreciate it.

//DRIVER
import java.io.*;
import java.util.*;
class Project3
{
	static Scanner console = new Scanner(System.in);
	public static void main(String[] args)
	{
		//Data Members
		int answer;
		answer = 0;

		//Call constructor and set those variables as null
		Address addressObject = new Address();
		addressObject.street = "null";
		addressObject.city = "null";
		addressObject.state = "null";
		addressObject.zip = "null";

		//Menu print
		System.out.println("1. Add a person to the address book.");
		System.out.println("2. See if a person is in the address book.");
		System.out.println("3. Print information of a specific person.");
		System.out.println("4. Print names of persons having a birthday in a particular month.");
		System.out.println("5. Print names of persons between two last names.");
		System.out.println("6. Print names of persons having a particular status.");
		System.out.println("7. Print the address book.");
		System.out.println("8. Save the address book to disk.");
		System.out.println("9. Load the address book from disk.");
		System.out.println("10. Terminate the program.");

		//Get data member to navigate menu
		answer = console.nextInt();

		//Switch to navigate menu
		switch (answer)
		{

			case 1:	addressObject.getbook();
					addressObject.getaddress();
			break;
			case 2:	//make method to search then print yes/no
			break;
			case 3: //make method to search then print that person
			break;
			case 4:	System.out.println("No birthday was entered.");
			break;
			case 5: //make method to print ppl between 2 last names
			break;
			case 6:	//make method to particular status
			break;
			case 7: addressObject.getbook(); //make a method to print
			break;
			case 8: addressObject.getaddress(); //getaddress is unfinished
			break;
			case 9: addressObject.getbook();
			break;
			case 10: System.exit(0);
			break;
			default: System.out.println("Enter a number between 1 and 10.");

		}

   	}
}

then my worker

import java.io.*;
import java.util.*;
public class Address
{
	//Declare variables
	String street;
	String city;
	String state;
	String zip;
	String phone;
	static Scanner console = new Scanner(System.in);

	//Constructor
		public Address(String st, String ct, String sta, String zp)
		{
			String street = st;
			String city = ct;
			String state = sta;
			String zip = zp;
		}
		Address()
		{
		}

	//Locate address book file
    public void getbook()
    {
		//Declare variables
		String infile;
		String outfile;

		//Get file locations
		System.out.println("Enter location of the address book");
		System.out.println("An example of this is C:hello.txt");
		infile = console.next();
		System.out.println("Enter location of the output file.");
		outfile = console.next();
	}
	//Get address information
	public void getaddress()
	{

		//Get address
		System.out.println("Enter contact's Streetname as one word");
		System.out.println("Example: 1111AmericanAvenue");
		street = console.next();
		//outFile.print(street);
		System.out.println("Enter contact's city");
		city = console.next();
		//outFile.print(city);
		System.out.println("Enter contact's state");
		state = console.next();
		//outFile.print(state);
		System.out.println("Enter contact's zip");
		zip = console.next();
		//outFile.print(zip);

	}



}

Recommended Answers

All 5 Replies

I have no idea what I'm looking for as I do not see any use of outFile.print(); or PrintWriter outFile = new PrintWriter(outfile); However what I see here is intentionally another problem

public class Address
{
	//Declare variables
	String street;
	String city;
	String state;
	String zip;
	String phone;
	static Scanner console = new Scanner(System.in);

	//Constructor
		public Address(String st, String ct, String sta, String zp)
		{
			String street = st;
			String city = ct;
			String state = sta;
			String zip = zp;
		}
		Address()
		{
		}

Even though you declare global variables street, city, state, zip these are never initialized because you create another identical set inside your constructor. This is called shadowing and it happens when you redeclare a variable that’s already been declared somewhere else. The effect of Shadowing is to hide the previously declared variable in such a way that it may look as though you’re using the hidden variable, but you’re actually using the shadowing variable.
So the above should be corrected either by removing type declarations inside constructor or use of setter methods

public class Address
{
	//Declare variables
	String street;
	String city;
	String state;
	String zip;
	String phone;
	static Scanner console = new Scanner(System.in);

	//Constructor
		public Address(String st, String ct, String sta, String zp)
		{
			street = st;
			city = ct;
			state = sta;
			zip = zp;
		}

		Address()
		{
		}
public class Address
{
	//Declare variables
	String street;
	String city;
	String state;
	String zip;
	String phone;
	static Scanner console = new Scanner(System.in);

	//Constructor
		public Address(String st, String ct, String sta, String zp)
		{
			setStreet(st);
			setCity(ct);
			setState(sta);
			setZip(zp);
		}
		Address()
		{
		}

		private void setStreet(String st){ street = st;}
		private void setCity(String ct){ city = ct;}
		private void setState(String sta){ state = st;}
		private void setZip(String zp){ zip = zp;}
commented: good response +4

I would like to thank you for pointing the error in my code.
The problem started at my getaddress() method. However, when I throw my FileNotFoundException it says it needs a { after my public class Address. I commented the outFile.print() out because it was giving me problems, so I figured I would work on other parts and get back to that. Sorry for the confusion. Also, I have strings called outfile and infile that are used to get the user to locate where they have their input file and where they want to output it. These can be found in my getbook() method, but they work fine. My new and updated code is here:

//=-WORKER-=
import java.io.*;
import java.util.*;
public class Address
	throws FileNotFoundException
{
	//Declare variables
	String street;
	String city;
	String state;
	String zip;
	String phone;

	static Scanner console = new Scanner(System.in);


	Scanner inFile = new Scanner(new FileReader(infile));
	PrintWriter outFile = new PrintWriter(outfile);

	//Constructor
		public Address(String st, String ct, String sta, String zp)
		{
			String street = st;
			String city = ct;
			String state = sta;
			String zip = zp;
		}
		Address()
		{
		}

		private void setStreet(String st){ street = st;}
		private void setCity(String ct){ city = ct;}
		private void setState(String sta){ state = sta;}
		private void setZip(String zp){ zip = zp;}

	//Locate address book file
    public void getbook()
    {
		//Declare variables
		String infile;
		String outfile;

		//Get file locations
		System.out.println("Enter location of the address book");
		System.out.println("An example of this is C:hello.txt");
		infile = console.next();
		System.out.println("Enter location of the output file.");
		outfile = console.next();
	}
	//Get address information
	public void getaddress()
	{

		//Get address
		System.out.println("Enter contact's Streetname as one word");
		System.out.println("Example: 1111AmericanAvenue");
		street = console.next();
		//outFile.print(street);
		System.out.println("Enter contact's city");
		city = console.next();
		//outFile.print(city);
		System.out.println("Enter contact's state");
		state = console.next();
		//outFile.print(state);
		System.out.println("Enter contact's zip");
		zip = console.next();
		//outFile.print(zip);

	}
	/*Print to file
	public void printaddress()
	{
		outFile.print(street);
		outFile.print(city);
		outFile.print(state);
		outFile.print(zip);
	}
*/



}

You cannot throw exception on class. You can make class throw exception with use of try catch block (often followed by finally)

Scanner inFile;
    try{
        inFile = new Scanner(new FileReader(infile));
    }
    catch(FileNotFoundException e){
        e.printStackTrace();
    }

or directly from method

public Scanner getBook() throws FileNotFoundException{}

but then you will need to catch possible exception on the receiving which is supposed to receive Scanner object back from getBook() method.

So remove exception from class header and handle it by one of the above ways.

PS: Also I'm not sure what you trying to do on line 17&18. Are these just some de-relics from past or are they have so other purpose?

Hello, Did you manage finishing your address book? Or do u still need help? I would like to see what you've done so far whether ur done with it or not because i did an address book as a project for my programming course last semester. So can you please post your code till now? Thanks & Good luck! :)

Hi can you please tell me the code you wrote for the address book till now? Thanks! :)

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.