Results 1 to 19 of 19
Thread: Basic keyboard input and edit
- 07-28-2008, 11:57 PM #1
Member
- Join Date
- Mar 2008
- Posts
- 32
- Rep Power
- 0
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:
So instead ofJava 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);
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.Java Code:// change one of the employee's names Employee e = emps.get(1); e.changeName("Petrie", "Robert");
- 07-29-2008, 01:22 AM #2
There are several ways to do this.input from a keyboard
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.
- 07-29-2008, 07:32 AM #3
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 + "]"; } }
- 07-31-2008, 12:06 AM #4
Member
- Join Date
- Mar 2008
- Posts
- 32
- Rep Power
- 0
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
- 07-31-2008, 02:05 AM #5
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.
- 07-31-2008, 11:14 PM #6
Member
- Join Date
- Mar 2008
- Posts
- 32
- Rep Power
- 0
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; }
- 07-31-2008, 11:44 PM #7
How does that relate to your errors?
- 07-31-2008, 11:48 PM #8
Member
- Join Date
- Mar 2008
- Posts
- 32
- Rep Power
- 0
Unfortunately I get the same errors "cannot find symbol
symbol : variable address1"
- 08-01-2008, 02:57 AM #9
You'll have to post the error message text if you want help.
Don't edit it or leave anything out.
- 08-01-2008, 09:46 PM #10
Member
- Join Date
- Mar 2008
- Posts
- 32
- Rep Power
- 0
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
- 08-01-2008, 11:07 PM #11
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.
- 08-07-2008, 11:45 PM #12
Member
- Join Date
- Mar 2008
- Posts
- 32
- Rep Power
- 0
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; } }I just need to know how to search for entry, Delete entry. If anybody know how to do this please help me.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(); } }
- 08-08-2008, 12:18 AM #13
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.
- 08-08-2008, 01:56 AM #14
Member
- Join Date
- Mar 2008
- Posts
- 32
- Rep Power
- 0
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.
- 08-08-2008, 02:49 AM #15
You could use the equals() method to compare a field.match only one field
Not sure what API you are looking at?
What would you think a method called remove would do?
- 08-08-2008, 10:59 PM #16
Member
- Join Date
- Mar 2008
- Posts
- 32
- Rep Power
- 0
This is what I have
But I get this errorAddressBook 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");
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
- 08-08-2008, 11:35 PM #17
entry1 is an AddressBook object (your class)entry1.name.remove(s);
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.
- 08-08-2008, 11:37 PM #18
Member
- Join Date
- Mar 2008
- Posts
- 32
- Rep Power
- 0
no it doesnt how would you do this? Show me please.
- 08-08-2008, 11:52 PM #19
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.
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.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.
Similar Threads
-
How To Edit/Add JSP Pages in NetBeans IDE
By JavaForums in forum NetBeansReplies: 2Last Post: 02-17-2009, 11:14 AM -
Polled keyboard input through swing
By Prometheus in forum Advanced JavaReplies: 2Last Post: 02-04-2008, 04:05 PM -
Help with keyboard events?
By Bibendum in forum New To JavaReplies: 2Last Post: 11-02-2007, 02:51 AM -
how to edit lines.
By jason27131 in forum New To JavaReplies: 1Last Post: 08-01-2007, 04:41 AM -
how to take input and verify input in Java programs
By bilal_ali_java in forum Advanced JavaReplies: 0Last Post: 07-21-2007, 08:46 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks