Help with arrays.

Changing size and removing records from a database.

Page 1 of 1

7 Replies - 992 Views - Last Post: 05 April 2008 - 08:32 PM Rate Topic: -----

#1 mattpd  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 50
  • Joined: 15-March 08

Help with arrays.

Post icon  Posted 04 April 2008 - 06:38 PM

I have this Phone Directory program that I need to edit to do a few extra things. Originally the size of PhoneRecord was 100. I need to set it to 1, and every time it is full it should double in size while retaining the information. This is what I came up with on paper. It makes sense in my head, but when I run the program and enter more than one element into the array it terminates instead of doubling.

Also, I need to add an additional command, "d", to delete a selected entry from the array. I can get as far as prompting the user for the name and creating a string that equals there input, but don't know where to go as far as removing the entry.

import jpb.*;

public class PhoneDirectory {
  public static void main(String[] args) {
	PhoneRecord[] records = new PhoneRecord[1];
	int numRecords = 0;
 
//THIS IS WHERE IS SHOULD DOUBLE WHEN FULL.  
	   
		if (numRecords == records.length) {
		PhoneRecord[] tempArray = new PhoneRecord[records.length*2];
		for (int i=0; i<records.length; i++) {
			tempArray[i]=records[i];
			records=tempArray;
		}
	}

	// Display list of commands
	System.out.println("Phone directory commands:\n" +
					   "  a - Add a new phone number\n" +
					   "  f - Find a phone number\n" +
					   "  q - Quit\n");

	// Read and execute commands
	while (true) {

	  // Prompt user to enter a command
	  SimpleIO.prompt("Enter command (a, f, or q): ");
	  String command = SimpleIO.readLine().trim();

	  // Determine whether command is "a", "f", "q", or
	  // illegal; execute command if legal.
	  if (command.equalsIgnoreCase("a")) {

		// Command is "a". Prompt user for name and number,
		// then create a phone record and store it in the
		// database.
		if (numRecords < records.length) {
		  SimpleIO.prompt("Enter new name: ");
		  String name = SimpleIO.readLine().trim();
		  SimpleIO.prompt("Enter new phone number: ");
		  String number = SimpleIO.readLine().trim();
		  records[numRecords] = 
			new PhoneRecord(name, number);
		  numRecords++;
		} else
		  System.out.println("Database is full");

	  } else if (command.equalsIgnoreCase("f")) {

		// Command is "f". Prompt user for search key.
		// Search the database for records whose names begin
		// with the search key. Print these names and the
		// corresponding phone numbers.
		SimpleIO.prompt("Enter name to look up: ");
		String key = SimpleIO.readLine().trim().toLowerCase();
		for (int i = 0; i < numRecords; i++) {
		  String name = records[i].getName().toLowerCase();
		  if (name.startsWith(key))
			System.out.println(records[i].getName() + " " + 
							   records[i].getNumber());
		}

	  } else if (command.equalsIgnoreCase("q")) {
		// Command is "q". Terminate program.
		return;

	  } else {
		// Command is illegal. Display error message.
		System.out.println("Command was not recognized; " +
						   "please enter only a, f, or q.");
	  }

	  System.out.println();
	}
  }
}

// Represents a record containing a name and a phone number
class PhoneRecord {
  private String name;
  private String number;

  // Constructor
  public PhoneRecord(String personName, String phoneNumber) {
	name = personName;					   
	number = phoneNumber;
  }

  // Returns the name stored in the record
  public String getName() {
	return name;
  }
 
  // Returns the phone number stored in the record
  public String getNumber() {
	return number;
  }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Help with arrays.

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Help with arrays.

Posted 04 April 2008 - 07:31 PM

View Postmattpd, on 4 Apr, 2008 - 06:38 PM, said:

I have this Phone Directory program that I need to edit to do a few extra things. Originally the size of PhoneRecord was 100. I need to set it to 1, and every time it is full it should double in size while retaining the information. This is what I came up with on paper. It makes sense in my head, but when I run the program and enter more than one element into the array it terminates instead of doubling.

Also, I need to add an additional command, "d", to delete a selected entry from the array. I can get as far as prompting the user for the name and creating a string that equals there input, but don't know where to go as far as removing the entry.

import jpb.*;

public class PhoneDirectory {
  public static void main(String[] args) {
	PhoneRecord[] records = new PhoneRecord[1];
	int numRecords = 0;
 
//THIS IS WHERE IS SHOULD DOUBLE WHEN FULL.  
	   
		if (numRecords == records.length) {
		PhoneRecord[] tempArray = new PhoneRecord[records.length*2];
		for (int i=0; i<records.length; i++) {
			tempArray[i]=records[i];
			records=tempArray;
		}
	}

	// Display list of commands
	System.out.println("Phone directory commands:\n" +
					   "  a - Add a new phone number\n" +
					   "  f - Find a phone number\n" +
					   "  q - Quit\n");

	// Read and execute commands
	while (true) {

	  // Prompt user to enter a command
	  SimpleIO.prompt("Enter command (a, f, or q): ");
	  String command = SimpleIO.readLine().trim();

	  // Determine whether command is "a", "f", "q", or
	  // illegal; execute command if legal.
	  if (command.equalsIgnoreCase("a")) {

		// Command is "a". Prompt user for name and number,
		// then create a phone record and store it in the
		// database.
		if (numRecords < records.length) {
		  SimpleIO.prompt("Enter new name: ");
		  String name = SimpleIO.readLine().trim();
		  SimpleIO.prompt("Enter new phone number: ");
		  String number = SimpleIO.readLine().trim();
		  records[numRecords] = 
			new PhoneRecord(name, number);
		  numRecords++;
		} else
		  System.out.println("Database is full");

	  } else if (command.equalsIgnoreCase("f")) {

		// Command is "f". Prompt user for search key.
		// Search the database for records whose names begin
		// with the search key. Print these names and the
		// corresponding phone numbers.
		SimpleIO.prompt("Enter name to look up: ");
		String key = SimpleIO.readLine().trim().toLowerCase();
		for (int i = 0; i < numRecords; i++) {
		  String name = records[i].getName().toLowerCase();
		  if (name.startsWith(key))
			System.out.println(records[i].getName() + " " + 
							   records[i].getNumber());
		}

	  } else if (command.equalsIgnoreCase("q")) {
		// Command is "q". Terminate program.
		return;

	  } else {
		// Command is illegal. Display error message.
		System.out.println("Command was not recognized; " +
						   "please enter only a, f, or q.");
	  }

	  System.out.println();
	}
  }
}

// Represents a record containing a name and a phone number
class PhoneRecord {
  private String name;
  private String number;

  // Constructor
  public PhoneRecord(String personName, String phoneNumber) {
	name = personName;					   
	number = phoneNumber;
  }

  // Returns the name stored in the record
  public String getName() {
	return name;
  }
 
  // Returns the phone number stored in the record
  public String getNumber() {
	return number;
  }
}



Your array re-assignment shoud be done outside the loop

// assuming recod.lenght > 0 if not whatever times 0 = 0
if(array.lenght() is not enought) {
   PhoneRecord[] tempArray = new PhoneRecord[record.length * 2]);
   for(int i = 0; i < records.length; i++) {
	   tempArray[i] = records[i];
  }
  // outside the loop
  records = temArray;
}


Was This Post Helpful? 0
  • +
  • -

#3 mattpd  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 50
  • Joined: 15-March 08

Re: Help with arrays.

Posted 04 April 2008 - 09:22 PM

What's this error about?

.java:51: cannot resolve symbol
symbol : variable tempArray
location: class PhoneDirectory
records=tempArray;
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 6979
  • View blog
  • Posts: 14,602
  • Joined: 16-October 07

Re: Help with arrays.

Posted 05 April 2008 - 02:12 AM

View Postmattpd, on 5 Apr, 2008 - 12:22 AM, said:

What's this error about?

.java:51: cannot resolve symbol
symbol : variable tempArray
location: class PhoneDirectory
records=tempArray;


It's probably about scope. You may have declared tempArray inside a block. If you're using the prior example, make sure you're outside the loop, but inside the if.

Now, if you're in a position where your array is likely to have to grow it's probably time to consider using a collection class. Your code need change very little, but adding elements is automatic and far more efficient.

e.g.
//PhoneRecord[] records = new PhoneRecord[1];
List<PhoneRecord> records = LinkedList<PhoneRecord>();
..
// records[numRecords] = new PhoneRecord(name, number);
records.add(new PhoneRecord(name, number));



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#5 mattpd  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 50
  • Joined: 15-March 08

Re: Help with arrays.

Posted 05 April 2008 - 11:20 AM

Ok i got rid of the error, but after the first entry the data base becomes full. So the size of records never goes above 1. What gives? And how should I go about deleting an entry. The way I did it was the way my book said but it still doesn't work.

This post has been edited by mattpd: 05 April 2008 - 11:37 AM

Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Help with arrays.

Posted 05 April 2008 - 11:53 AM

View Postmattpd, on 5 Apr, 2008 - 11:20 AM, said:

Ok i got rid of the error, but after the first entry the data base becomes full. So the size of records never goes above 1. What gives? And how should I go about deleting an entry. The way I did it was the way my book said but it still doesn't work.

Post you code
Was This Post Helpful? 0
  • +
  • -

#7 mattpd  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 50
  • Joined: 15-March 08

Re: Help with arrays.

Posted 05 April 2008 - 12:17 PM

I actually got that part working. It was a silly mistake. But the code for deleting an entry is what I need help with now.

SimpleIO.prompt ("Enter name to delete: ");
		   String key = SimpleIO.readLine().trim().toLowerCase();
		 for (int i = 0; i < numRecords; i++) {
		 String name = records[i].getName().toLowerCase();
		  if (name.startsWith(key))
			  records[i] = records[--numRecords]; 
		}
	  }



I went by the book but it does nothing. I also remember something about setting a value to null in order to delete it but the book says nothing about it.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Help with arrays.

Posted 05 April 2008 - 08:32 PM

Or something like:

	SimpleIO.prompt ("Enter name to delete: ");
	String key = SimpleIO.readLine().trim().toLowerCase();
	int k = 0;
	boolean found = false;
	for (int i = 0; i < numRecords; i++) {
		String name = records[i].getName().toLowerCase();
		// if key does not match copy
		if (!name.startsWith(key)) {
		  record[k++] = record[i];
		}
		else {				 // flag we found it
		   found = true;
		}
	 }
	 // if we found it
	 if(found) {
		numRecord--;					 // decrement recordCount
		record[numRecord] = null;  // set to null the last one
	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1