2 Replies - 2615 Views - Last Post: 27 March 2008 - 05:12 AM Rate Topic: -----

#1 Dio1080  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 25-March 08

How do i add, delete to a list

Posted 25 March 2008 - 05:36 PM

How a problem, i don't know how to add a phone number to a list and delete to a list and able to check for the phone number in the list. Can so someone explain to me how to do it, seem like the only thing I right was the binary search, here is my code:

import java.util.Scanner;
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
	Scanner keyboard = new Scanner(System.in); 
int choice = 1;
String [] nums = new String [110];
System.out.print("Please enter the amount of phone numbers you want to enter in the start: \n");
String Friends = keyboard.next();
int friends = Integer.parseInt(Friends);
System.out.println();
while(choice != 0){
System.out.println("Please choose an option: "
+"\n1. Enter in a phone ."
+"\n2. Edit the phone number."
+"\n3. Add a phone number to the list."
+"\n4. Sort for your number."
+"\n5. Print the information to the console."
+"\n6. Check for the phone number"
+"\n0. To Exit the Program.");
 System.out.print("\nPlease make your choice by number: ");
/* Reads in Choice for menu */
String Choice = keyboard.next();
choice = Integer.parseInt(Choice);
		
switch(choice){
case 1://Enter in People
						
						
for(int x = 0; x < friends; x++){
System.out.println("Information for friend " + (x + 1) + ": \n");
System.out.print("Enter in the friend's First Name: ");
nums[x] = keyboard.next();
							
System.out.println();
						}
				
						break;
case 2://Edit Entries
System.out.println("What would you like to Edit?" +"\n1. Frist Name.");
System.out.print("Please enter in your choice: ");
String Edit = keyboard.next();
int edit = Integer.parseInt(Edit);
						
System.out.println();
System.out.print("Please enter the phone number you wish to edit: ");
String Last = keyboard.next();
						
for(int m = 0; m < friends; m++){
if(nums[m].compareToIgnoreCase( Last ) == 0){
System.out.print("Enter in the new information for " + Last + ": ");
nums[m] = keyboard.next();
								break;
							}
						}
		
						break;
				case 3://Add entries
		
		
						break;
				case 4://Sort
			// Reads in Phone Number to Search for 
			System.out.print("\nPlease type the phone number you are looking for: ");
			String Phone = keyboard.next();
			float phone = Float.parseFloat(Phone);
				for(int x = 0; x < friends; x++){
					if(Float.parseFloat(nums[x]) == phone){
							System.out.println("\n" + nums[x]);
							System.out.println("Number of iterations: " + (x + 1));
					}
				}
			
	

		
						break;
				case 5://Print
						System.out.println("Phone Numners");
						for(int n = 0; n < friends; n++){
							for(int o = 0; o < 7; o++){
								System.out.print(nums[n] + "\t");
							}
							System.out.println();
						}
		
						break;
				case 6://Send to File
		
		
						break;
													}	
		
	}

			}
		

	//Method that does the binary search
	public static int BinarySearch(String [][] nums){
		Scanner keyboard = new Scanner(System.in); 
		System.out.print("Enter in the Seven Digit Phone Number: ");
		String Num = keyboard.next();
		float num = Float.parseFloat(Num);
		int count = 0;
		int low = 0;
		int high = 49;
		//formula for binary search
		while(high >= low){
			int mid = (low + high) / 2;
			if(num < Float.parseFloat(nums[mid][2]))
				high = mid - 1;
			else if(num == Float.parseFloat(nums[mid][2])){
				System.out.println("\n" + nums[mid]);
				return count;
			}
			else
			low = mid + 1;
		count++;
		}
		//return to the main
		return count;	
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: How do i add, delete to a list

#2 fahlyn  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 03-November 07

Re: How do i add, delete to a list

Posted 26 March 2008 - 04:28 AM

you may want to consider using a HashMap object if you need to search for an existing phone number. Then you can use methods such as these....



HashMap phoneNumber = new HashMap();

phoneNumber.put("1234567891", new Boolean(true));
String number = (String)phoneNumber.get("1234567891");

//to see if a number already exists in your HashMap you can do this

if (phoneNumber.containsKey("1234567891")) {
	//do something here
}





If that's not what you're looking for maybe try a java.util.ArrayList.
Was This Post Helpful? 0
  • +
  • -

#3 spullen  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 10
  • View blog
  • Posts: 356
  • Joined: 22-March 07

Re: How do i add, delete to a list

Posted 27 March 2008 - 05:12 AM

Everything you need to know about lists are in the API (like the function to add or delete from a list) http://java.sun.com/...LinkedList.html .
Basically the function to add something to the list is add, and the function to delete something is remove (and there a couple of options that the api gives you for adding and deleting).
Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1