Results 1 to 19 of 19
  1. #1
    BHCluster is offline Member
    Join Date
    Mar 2008
    Posts
    32
    Rep Power
    0

    Default Basic keyboard input and edit

    Hi everybody I have another problem basically what I am trying to do is read in the name of a person and then edit that account what I have is the following:

    Java Code:
    ArrayList<Employee> emps = new ArrayList<Employee>();
    
    // add employees to array list
    emps.add(new Employee("Addams", "Gomez"));
    emps.add(new Employee("Taylor", "Andy"));
    emps.add(new Employee("Kirk", "James"));
    
    // print array list
    System.out.println(emps);
    
    // change one of the employee's names
    Employee e = emps.get(1);
    e.changeName("Petrie", "Robert");
    
    // print the array list again
    System.out.println(emps);
    So instead of
    Java Code:
    // change one of the employee's names
    Employee e = emps.get(1);
    e.changeName("Petrie", "Robert");
    I would like to read in the name for editing and then input from a keyboard new name. If anybody knows how to do this please help. Thank you for taking interest.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,563
    Rep Power
    25

    Default

    input from a keyboard
    There are several ways to do this.
    One is byte by byte using the System.in class
    another is to use GUI to present the user with an input area such as a Textarea to type into.
    There are probably code examples for both that you could fine by using Search.

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    This ought to get you started in the right direction.
    Java Code:
    import java.util.*;
    
    public class KeyEntry {
        public static void main(String[] args) {
            ArrayList<Employee3> emps = new ArrayList<Employee3>();
            // add employees to array list
            emps.add(new Employee3("Addams", "Gomez"));
            emps.add(new Employee3("Taylor", "Andy"));
            emps.add(new Employee3("Kirk", "James"));
    
            // print array list
            print(emps);
    
            Scanner scanner = new Scanner(System.in);
    
            boolean processMore = true;
            while(processMore) {
                System.out.println("---- Menu ----");
                System.out.println("Add Employee : \"a\"");
                System.out.println("Edit Employee : \"e\"");
                System.out.println("Remove Employee : \"r\"");
                System.out.println("Quit - \"q\"");
                String choice = scanner.nextLine();
    
                if(choice.equals("e")) {
                    System.out.println("enter surName of Employee");
                    String lastName = scanner.nextLine();
                    Employee3 target = null;
                    for(Employee3 e : emps) {
                        if(e.lastName.equals(lastName)) {
                            target = e;
                            break;
                        }
                    }
                    if(target != null) {
                        // Now we have the Employee3, let's edit it:
                        System.out.println("you want to edit " + target);
                        System.out.println("---- Menu ----");
                        System.out.println("lastName : \"l\"");
                        System.out.println("firstName : \"f\"");
                        String edit = scanner.nextLine();
                        if(edit.equals("l")) {
                            System.out.println("enter lastName");
                            String s = scanner.nextLine();
                            target.lastName = s;
                        } else if(edit.equals("f")) {
                            System.out.println("enter firstName");
                            String s = scanner.nextLine();
                            target.firstName = s;
                        } else {
                            // couldn't do it, try again...
                            continue;
                        }
                        System.out.println("Edited: " + target);
                    } else {
                        System.out.println("couldn't find entry for: " +
                                            lastName);
                    }
                }
                if(choice.equals("q")) {
                    processMore = false;
                    continue;
                }
            }
            scanner.close();
        }
    
        private static void print(List<Employee3> list) {
            for(Employee3 employee : list) {
                System.out.println(employee);
            }
        }
    }
    
    class Employee3 {
        String lastName;
        String firstName;
    
        public Employee3(String lastName, String firstName) {
            this.lastName = lastName;
            this.firstName = firstName;
        }
    
        public String toString() {
            return "Employee3[lastName:" + lastName +
                          ", firstName:" + firstName + "]";
        }
    }

  4. #4
    BHCluster is offline Member
    Join Date
    Mar 2008
    Posts
    32
    Rep Power
    0

    Default

    I get these errors when I try to compile it with the rest of the code

    Java Code:
    AddressBookTest.java:64: address has private access in AddressBook
    					   if(e.address.equals(address)) {
    					       ^
    AddressBookTest.java:79: address has private access in AddressBook
    					        target.address = s;
    					              ^
    AddressBookTest.java:83: name has private access in AddressBook
    					        target.name = s;
    					              ^
    3 errors
    
    Tool completed with exit code 1

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,563
    Rep Power
    25

    Default

    Looks like address and name are private variables in another class(AddressBook).
    You need to change something in AddressBook to allow access to those values. Either add get() methods or make the variables public.

  6. #6
    BHCluster is offline Member
    Join Date
    Mar 2008
    Posts
    32
    Rep Power
    0

    Default

    In a address class I have following:

    Java Code:
    public void setName(String name1) {
    		name = name1;
    	}
    
    	public void setAddress(String address1) {
    		address = address1;
    Java Code:
    public String getName() {
    		return name;
    	}
    
    public String getAddress() {
    		return address;
    	}

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,563
    Rep Power
    25

    Default

    How does that relate to your errors?

  8. #8
    BHCluster is offline Member
    Join Date
    Mar 2008
    Posts
    32
    Rep Power
    0

    Default

    Unfortunately I get the same errors "cannot find symbol
    symbol : variable address1"

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,563
    Rep Power
    25

    Default

    You'll have to post the error message text if you want help.
    Don't edit it or leave anything out.

  10. #10
    BHCluster is offline Member
    Join Date
    Mar 2008
    Posts
    32
    Rep Power
    0

    Default

    C:\Documents and Settings\User\Desktop\Address Book 2\AddressBookTest.java:64: cannot find symbol
    symbol : variable address1
    location: class AddressBook
    if(e.address1.equals(address1)) {
    ^
    C:\Documents and Settings\User\Desktop\Address Book 2\AddressBookTest.java:79: cannot find symbol
    symbol : variable address1
    location: class AddressBook
    target.address1 = s;
    ^
    C:\Documents and Settings\User\Desktop\Address Book 2\AddressBookTest.java:83: cannot find symbol
    symbol : variable name1
    location: class AddressBook
    target.name1 = s;
    ^
    3 errors

    Tool completed with exit code 1

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,563
    Rep Power
    25

    Default

    You'll have to learn to interpret the compiler's error messages.
    They give the path to the source program followed by:## where ## is the line number in the source file where the error occured.
    The above error messages say the variables address1 and name1, usage found at lines 64, 79 and 83 are NOT defined in the scope where they are used or that they are NOT fields in the object reference by target.
    So look at the code and see if you spelled the variable names correctly or where they are defined.

  12. #12
    BHCluster is offline Member
    Join Date
    Mar 2008
    Posts
    32
    Rep Power
    0

    Default

    People thanks for all of the help so far, but I am so stuck I don't know what to do. This is what I have so far please help.

    Java Code:
    
    public class AddressBook {
    	 String name;
    	 String address;
    	 String telNo;
    	 String email;
    
    	public AddressBook() {
    		name = "";
    		address = "";
    		telNo = "";
    		email = "";
    	}
    
    	public AddressBook(String name1, String address1, String telNo1, String email1) {
    		name = name1;
    		address = address1;
    		telNo = telNo1;
    		email = email1;
    	}
    
    	//setters
    	public void setName(String name1) {
    		name = name1;
    	}
    
    	public void setAddress(String address1) {
    		address = address1;
    	}
    
    	public void setTelNo(String telNo1) {
    		telNo = telNo1;
    	}
    
    	public void setEmail(String email1) {
    		email = email1;
    	}
    
    	//getters
    	public String getName() {
    		return name;
    	}
    
    	public String getAddress() {
    		return address;
    	}
    
    	public String getTelNo() {
    		return telNo;
    	}
    
    	public String getEmail() {
    		return email;
    	}
    
    	// method to convert the object to a string
    	public String toString(){
    	String str="";
    	str+="\n"+name;
    	str+="\n"+address;
    	str+="\n"+telNo;
    	str+="\n"+email;
    	return str;
    	}
    }
    Java Code:
    import java.util.Scanner; // program uses class Scanner
    import java.util.ArrayList;
    import java.util.ListIterator;
    import java.util.*;
    
    public class AddressBookTest{
    
    
    	public AddressBookTest(){
    
    	    // create Scanner to obtain input from command window
    		Scanner input = new Scanner( System.in );
    		Scanner keyboard = new Scanner(System.in);
    		Scanner scanner = new Scanner(System.in);
    
            ArrayList<AddressBook> book = new ArrayList<AddressBook>();
    
    		boolean done = false;
            while(!done){
    
            System.out.println();
            System.out.println("Options");
            System.out.println();
            System.out.println("A. Add new entry:");
            System.out.println("B. Display all");
            System.out.println("C. Search Address Book");
            System.out.println("D. Delete entry");
            System.out.println("X. Exit the programme");
            System.out.println();
            System.out.print("Select an option from the menu (A, B or X): ");
            char option = keyboard.next().charAt(0);
    
    
            if (option == 'A' || option == 'a') {
                AddressBook entry = new AddressBook();
    	        System.out.println("Enter Name");
    		    entry.setName(input.next()); // read first name from user
    			System.out.println("Enter Address");
    			entry.setAddress(input.next()); // read in address
    			System.out.println("Enter Telephone No");
    			entry.setTelNo(input.next()); // read in telephone number
    			System.out.println("Enter E-Mail Address");
    			entry.setEmail(input.next()); // read in telephone number
                book.add(entry);
            }
    
            else if (option == 'B' || option == 'b') {
                System.out.println();
                System.out.println("Result: ");
                System.out.println(book);
                ListIterator list = book.listIterator();
                while(list.hasNext()) {
                    System.out.println(list.next());
                    System.out.println();
                }
            }
    
            else if (option == 'C' || option == 'c') {
    		    System.out.println();
    
    
    
    
            }
    
            else if (option == 'D' || option == 'd') {
    			System.out.println();
    
    
    
    
            }
    
            else if (option == 'X' || option == 'x') {
                System.out.println("Closing address book...");
                System.out.println();
                done = true;
                System.exit(0);
            }
    
            else {
                System.out.println("Error! You did not select a valid option from the menu");
            }
           }
          }
    
    
    
    
    
    
    	public static void main (String [] args){
    		new AddressBookTest();
    	}
    }
    I just need to know how to search for entry, Delete entry. If anybody know how to do this please help me.

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,563
    Rep Power
    25

    Default

    To delete an entry, you need to decide how to chose the entry to be deleted. There is the name, address, telno and email. Do all four have to match or only one?
    In a loop look at each element in your list for a match based on the above fields. Look at the API doc for the list to see how to remove an entry.

  14. #14
    BHCluster is offline Member
    Join Date
    Mar 2008
    Posts
    32
    Rep Power
    0

    Default

    Thanks for answering so quickly. I tried the API but nothing I don't seem to understand it, and it needs to match only one field. If you can help please do cos I am going insane with this.

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,563
    Rep Power
    25

    Default

    match only one field
    You could use the equals() method to compare a field.

    Not sure what API you are looking at?
    What would you think a method called remove would do?

  16. #16
    BHCluster is offline Member
    Join Date
    Mar 2008
    Posts
    32
    Rep Power
    0

    Default

    This is what I have
    AddressBook entry1 = new AddressBook();
    System.out.println();
    System.out.println("Enter Name to deleate");
    String s = scanner.nextLine();
    if (entry1.name.equals(s))
    entry1.name.remove(s);

    // print the array list again
    System.out.println(book);

    System.out.println("Deleated");
    But I get this error
    C:\Documents and Settings\User\Desktop\Address Book 2\AddressBookTest.java:72: cannot find symbol
    symbol : method remove(java.lang.String)
    location: class java.lang.String
    entry1.name.remove(s);
    ^
    1 error

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,563
    Rep Power
    25

    Default

    entry1.name.remove(s);
    entry1 is an AddressBook object (your class)
    name is a String field in that class
    does String have a method called remove?
    What class has a method called remove?
    Use the API doc to find all the classes with remove methods - use the Index link at the top of the API doc page and look at the R entries.

  18. #18
    BHCluster is offline Member
    Join Date
    Mar 2008
    Posts
    32
    Rep Power
    0

    Default

    no it doesnt how would you do this? Show me please.

  19. #19
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,563
    Rep Power
    25

    Default

    Where is the object you want to remove? If it is in a container of some kind, read the API doc for that class for relavant methods.
    Use the API doc to find all the classes with remove methods - use the Index link at the top of the API doc page and look at the R entries.
    Have you done that? What classes have remove? There are a lot but you are using one of them as a container for your classes.

Similar Threads

  1. How To Edit/Add JSP Pages in NetBeans IDE
    By JavaForums in forum NetBeans
    Replies: 2
    Last Post: 02-17-2009, 11:14 AM
  2. Polled keyboard input through swing
    By Prometheus in forum Advanced Java
    Replies: 2
    Last Post: 02-04-2008, 04:05 PM
  3. Help with keyboard events?
    By Bibendum in forum New To Java
    Replies: 2
    Last Post: 11-02-2007, 02:51 AM
  4. how to edit lines.
    By jason27131 in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 04:41 AM
  5. how to take input and verify input in Java programs
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-21-2007, 08:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •