luna621
asked on
Java Applet - Reading input from user
Hello,
This applet will take a person's name & ID number, and stores it in a sorted list. When the user clicks the add button, it will read what the user inputted in the name & ID textfields, and store the person in a listed sorted by the ID number. The three buttons: Remove First, Remove Last, Clear List, will be disabled if there are no items in the list. If any error occurs, an error message will be displayed and the ok button (initially disabled) will be enabled.
Question: How do I read input from a user?
Thank you!
-luna621 =^^=
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- --
Here's the applet: http://www2.hawaii.edu/~flam/a04.html
Sorted List codes:
1. http://www2.hawaii.edu/~flam/DLN.java
2. http://www2.hawaii.edu/~flam/ISortedList.java
3. http://www2.hawaii.edu/~flam/Person.java
4. http://www2.hawaii.edu/~flam/SortedList.java
5. http://www2.hawaii.edu/~flam/SortedListException.java
This applet will take a person's name & ID number, and stores it in a sorted list. When the user clicks the add button, it will read what the user inputted in the name & ID textfields, and store the person in a listed sorted by the ID number. The three buttons: Remove First, Remove Last, Clear List, will be disabled if there are no items in the list. If any error occurs, an error message will be displayed and the ok button (initially disabled) will be enabled.
Question: How do I read input from a user?
Thank you!
-luna621 =^^=
--------------------------
Here's the applet: http://www2.hawaii.edu/~flam/a04.html
Sorted List codes:
1. http://www2.hawaii.edu/~flam/DLN.java
2. http://www2.hawaii.edu/~flam/ISortedList.java
3. http://www2.hawaii.edu/~flam/Person.java
4. http://www2.hawaii.edu/~flam/SortedList.java
5. http://www2.hawaii.edu/~flam/SortedListException.java
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Where do I create a new SortedList? In the init() or in another method?
y
in the init()
ASKER
ok, let me try that =^^=
ASKER
add(java.awt.PopupMenu) in java.awt.Component cannot be applied to (int)
list.add(iUserInput);
^
1 error
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- --
What does this mean? Where did I go wrong @_@??
http://www2.hawaii.edu/~flam/a04.html
list.add(iUserInput);
^
1 error
--------------------------
What does this mean? Where did I go wrong @_@??
http://www2.hawaii.edu/~flam/a04.html
ASKER
The code is provided on that page via link =^^=
ASKER
Okay, be back in a few hours... >_<''
> What does this mean? Where did I go wrong @_@??
Its basically saying that you can not add an interger iUserInput to a component
which is list.
Hope that helps . . .
Javatm
Its basically saying that you can not add an interger iUserInput to a component
which is list.
Hope that helps . . .
Javatm
that error is telling u the add() method in Component class is not execting an int parameter.
try {
int iUserInput = Integer.parseInt(userInput );
if(iUserInput<1 || iUserInput>2000) {
error.setText("Please enter an integer between 1 and 2000.");
}
if(iUserInput>0 && iUserInput<2001){
// Here was your problem . . .
list.add(iUserInput);
}
}
catch(NumberFormatExceptio n notInt) {
error.setText("Sorry, the ID is not a valid integer.");
}
int iUserInput = Integer.parseInt(userInput
if(iUserInput<1 || iUserInput>2000) {
error.setText("Please enter an integer between 1 and 2000.");
}
if(iUserInput>0 && iUserInput<2001){
// Here was your problem . . .
list.add(iUserInput);
}
}
catch(NumberFormatExceptio
error.setText("Sorry, the ID is not a valid integer.");
}
> list.add(iUserInput);
looks like it should be:
list.append(iUserInput+"\n ");
looks like it should be:
list.append(iUserInput+"\n
Thats correct :)
ASKER
Oops! You guys are fast. Okay, the list.append() just displays the ID number in the textarea in the order that you add. How do I make it so when you add, it will add the person to the list in a sorted order.
Example:
-------------------------- ---------- ---------- ---------- --
Name: ____________ ID: _____________ |Add|
__________________________ __________ __
| |
| |
| |
| |
| |
| |
| |
-------------------------- ---------- ---------- --------
|Remove First| |Remove Last| |Clear List|
Error msg: _______________________ |OK|
Ok, that's the applet. This is what I want to do
-------------------------- ---------- ---------- ----------
Step 1: Add 1st person.
-------------------------- ---------- ---------- ----------
Name: John Doe ID: 1234 |Add|
__________________________ __________ __
|1234, John Doe |
| |
| |
| |
| |
| |
| |
-------------------------- ---------- ---------- --------
|Remove First| |Remove Last| |Clear List|
Error msg: _______________________ |OK|
-------------------------- ---------- ---------- ----------
Step 2: Add 2nd person and sort by ID number.
-------------------------- ---------- ---------- ----------
Name: Mary Belle ID: 0555 |Add|
__________________________ __________ __
|0555, Mary Belle |
|1234, John Doe |
| |
| |
| |
| |
| |
-------------------------- ---------- ---------- --------
|Remove First| |Remove Last| |Clear List|
Error msg: _______________________ |OK|
-------------------------- ---------- ---------- ----------
Step 3: Here's an error example.
-------------------------- ---------- ---------- ----------
Name: Casey Year ID: 1234 |Add|
__________________________ __________ __
|0555, Mary Belle |
|1234, John Doe |
| |
| |
| |
| |
| |
-------------------------- ---------- ---------- --------
|Remove First| |Remove Last| |Clear List|
Error msg: Duplicate ID. Casey Year not added. |OK|
Something like that is what I had in mind.
-------------------------- ---------- ---------- ---------- ----------
Codes:
-------------------------- ---------- ---------- ---------- ----------
1. http://www2.hawaii.edu/~flam/DLN.java
2. http://www2.hawaii.edu/~flam/ISortedList.java
3. http://www2.hawaii.edu/~flam/Person.java
4. http://www2.hawaii.edu/~flam/SortedList.java
5. http://www2.hawaii.edu/~flam/SortedListException.java
Example:
--------------------------
Name: ____________ ID: _____________ |Add|
__________________________
| |
| |
| |
| |
| |
| |
| |
--------------------------
|Remove First| |Remove Last| |Clear List|
Error msg: _______________________ |OK|
Ok, that's the applet. This is what I want to do
--------------------------
Step 1: Add 1st person.
--------------------------
Name: John Doe ID: 1234 |Add|
__________________________
|1234, John Doe |
| |
| |
| |
| |
| |
| |
--------------------------
|Remove First| |Remove Last| |Clear List|
Error msg: _______________________ |OK|
--------------------------
Step 2: Add 2nd person and sort by ID number.
--------------------------
Name: Mary Belle ID: 0555 |Add|
__________________________
|0555, Mary Belle |
|1234, John Doe |
| |
| |
| |
| |
| |
--------------------------
|Remove First| |Remove Last| |Clear List|
Error msg: _______________________ |OK|
--------------------------
Step 3: Here's an error example.
--------------------------
Name: Casey Year ID: 1234 |Add|
__________________________
|0555, Mary Belle |
|1234, John Doe |
| |
| |
| |
| |
| |
--------------------------
|Remove First| |Remove Last| |Clear List|
Error msg: Duplicate ID. Casey Year not added. |OK|
Something like that is what I had in mind.
--------------------------
Codes:
--------------------------
1. http://www2.hawaii.edu/~flam/DLN.java
2. http://www2.hawaii.edu/~flam/ISortedList.java
3. http://www2.hawaii.edu/~flam/Person.java
4. http://www2.hawaii.edu/~flam/SortedList.java
5. http://www2.hawaii.edu/~flam/SortedListException.java
You might be better off using an java.awt.List instead of a TextArea.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
What if each time I add a person, I re-print the list? Will that work?
> What if each time I add a person, I re-print the list? Will that work?
Yes, it will depending on how you do it :)
Hope that helps . . .
Javatm
Yes, it will depending on how you do it :)
Hope that helps . . .
Javatm
ASKER
public class Ears implements ActionListener {
public void actionPerformed(ActionEven t e) {
String whichButton = e.getActionCommand();
if(whichButton.equals("Add ")) {
String idInput = id.getText();
String userInput = idInput + ", " + name.getText();
try {
int iIDInput = Integer.parseInt(idInput);
if(iIDInput<1 || iIDInput>2000) {
error.setText("Please enter an integer between 1 and 2000.");
} // end if
if(iIDInput>0 && iIDInput<2001){
list.add(userInput); //<----------------------- ---------- --- error from this part here
} // end if
} // end try
catch(NumberFormatExceptio n notInt) {
error.setText("Sorry, the ID is not a valid integer.");
} // end catch
} // end if
if(whichButton.equals("Rem ove First")) {
if(list.isEmpty()==true) {
error.setText("Sorry, there are no people in the list.");
} // end if
if(list.isEmpty()==false) {
remove(0);
} // end if
} // end if
} // end actionPerformed
} // end Ears
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
unreported exception SortedListException; must be caught or declared to be thrown
list.add(userInput);
^
1 error
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
But, when I try to throw the Exception I get these errors:
'{' expected
public class Ears implements ActionListener throws Exception {
^
'}' expected
} // end OrderedList
^
2 errors
public void actionPerformed(ActionEven
String whichButton = e.getActionCommand();
if(whichButton.equals("Add
String idInput = id.getText();
String userInput = idInput + ", " + name.getText();
try {
int iIDInput = Integer.parseInt(idInput);
if(iIDInput<1 || iIDInput>2000) {
error.setText("Please enter an integer between 1 and 2000.");
} // end if
if(iIDInput>0 && iIDInput<2001){
list.add(userInput); //<-----------------------
} // end if
} // end try
catch(NumberFormatExceptio
error.setText("Sorry, the ID is not a valid integer.");
} // end catch
} // end if
if(whichButton.equals("Rem
if(list.isEmpty()==true) {
error.setText("Sorry, there are no people in the list.");
} // end if
if(list.isEmpty()==false) {
remove(0);
} // end if
} // end if
} // end actionPerformed
} // end Ears
--------------------------
unreported exception SortedListException; must be caught or declared to be thrown
list.add(userInput);
^
1 error
--------------------------
But, when I try to throw the Exception I get these errors:
'{' expected
public class Ears implements ActionListener throws Exception {
^
'}' expected
} // end OrderedList
^
2 errors
ASKER
Also, is something wrong with my add method. If I want to read the userInput from the applet, and add that String to the list, will my add method work?
public void add(Comparable item) throws SortedListException {
DLN node = new DLN(item);
DLN nodeTmp = new DLN(get(count/2));
if (nodeTmp==null) { // list is empty
count = 1;
head = tail = node;
return;
} // end if
int firstCmpResult = nodeTmp.compareTo(node);
while(true) {
int cmpResult = nodeTmp.compareTo(node);
if (cmpResult < 0 && firstCmpResult < 0) {
if (nodeTmp.getNext()==null) {
// we've reached the tail of the list
// add the new element there
tail = node;
nodeTmp.setNext(node);
node.setPrev(nodeTmp);
count++;
return;
} // end if
nodeTmp = nodeTmp.getNext();
} // end if
else if (cmpResult > 0 && firstCmpResult > 0) {
if (nodeTmp.getPrev()==null) {
// reached the head of the list
// add the new element
head = node;
node.setNext(nodeTmp);
nodeTmp.setPrev(node);
count++;
break;
} // end if
nodeTmp = nodeTmp.getPrev();
} // end if
else if (cmpResult < 0 && firstCmpResult > 0) {
// After all >0 found the first <0
// So, add the new element here
node.setNext(nodeTmp.getNe xt());
node.setPrev(nodeTmp);
nodeTmp.getNext().setPrev( node);
nodeTmp.setNext(node);
count++;
} // end if
else if (cmpResult > 0 && firstCmpResult < 0) {
// After all <0 found the first >0
// So, add the new element here
node.setNext(nodeTmp);
node.setPrev(nodeTmp.getPr ev());
nodeTmp.getPrev().setNext( node);
nodeTmp.setPrev(node);
count++;
} // end else
else if (cmpResult==0) {
System.out.println("Sorry! A Person with this ID already exists!");
} // end if
} // end while
} // end add()
public void add(Comparable item) throws SortedListException {
DLN node = new DLN(item);
DLN nodeTmp = new DLN(get(count/2));
if (nodeTmp==null) { // list is empty
count = 1;
head = tail = node;
return;
} // end if
int firstCmpResult = nodeTmp.compareTo(node);
while(true) {
int cmpResult = nodeTmp.compareTo(node);
if (cmpResult < 0 && firstCmpResult < 0) {
if (nodeTmp.getNext()==null) {
// we've reached the tail of the list
// add the new element there
tail = node;
nodeTmp.setNext(node);
node.setPrev(nodeTmp);
count++;
return;
} // end if
nodeTmp = nodeTmp.getNext();
} // end if
else if (cmpResult > 0 && firstCmpResult > 0) {
if (nodeTmp.getPrev()==null) {
// reached the head of the list
// add the new element
head = node;
node.setNext(nodeTmp);
nodeTmp.setPrev(node);
count++;
break;
} // end if
nodeTmp = nodeTmp.getPrev();
} // end if
else if (cmpResult < 0 && firstCmpResult > 0) {
// After all >0 found the first <0
// So, add the new element here
node.setNext(nodeTmp.getNe
node.setPrev(nodeTmp);
nodeTmp.getNext().setPrev(
nodeTmp.setNext(node);
count++;
} // end if
else if (cmpResult > 0 && firstCmpResult < 0) {
// After all <0 found the first >0
// So, add the new element here
node.setNext(nodeTmp);
node.setPrev(nodeTmp.getPr
nodeTmp.getPrev().setNext(
nodeTmp.setPrev(node);
count++;
} // end else
else if (cmpResult==0) {
System.out.println("Sorry!
} // end if
} // end while
} // end add()
ASKER
Okay, I'm off to bed. Be back tomorrow =^^=
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks zzynx! I used method (2) - try/catch. Anyways, I was trying the applet out and I realized I never wrote my print list method!! So, I wrote it:
public void printList() {
if(count <= 1) {
System.out.println("Oro?! List is empty.\n");
return;
} // end if
DLN cursor = head;
while(cursor != null) {
System.out.println(cursor. toString() );
cursor = cursor.getNext();
} // end while
} // end printList
-------------------------- ---------- ---------- ---------- -------
Then in my Ears method, I added this statement:
public class Ears implements ActionListener {
public void actionPerformed(ActionEven t e) {
String whichButton = e.getActionCommand();
if(whichButton.equals("Add ")) {
idInput = id.getText();
userInput = idInput + ", " + name.getText();
try {
int iIDInput = Integer.parseInt(idInput);
if(iIDInput<1 || iIDInput>2000) {
error.setText("Please enter an integer between 1 and 2000.");
} // end if
if(iIDInput>0 && iIDInput<2001){
try {
list.add(userInput);
listArea.setText(list.prin tList()); //<-------------- this gives me errors!!
}
catch (SortedListException ex) {
error.setText("Sorry, this person could not be added.");
}
} // end if
} // end try
catch(NumberFormatExceptio n notInt) {
error.setText("Sorry, the ID is not a valid integer.");
} // end catch
} // end if
if(whichButton.equals("Rem ove First")) {
if(list.isEmpty()==true) {
error.setText("Sorry, there are no people in the list.");
} // end if
if(list.isEmpty()==false) {
remove(0);
} // end if
} // end if
} // end actionPerformed
} // end Ears
-------------------------- ---------- ---------- ------
This line: listArea.setText(list.prin tList()); gives this error: 'void' type not allowed here listArea.setText(list.prin tList());
^
1 error
-------------------------- ---------- ---------- ------
If I can't do that, how would I print the list to the TextArea of the applet?
Thanks!
-luna621 =^^=
public void printList() {
if(count <= 1) {
System.out.println("Oro?! List is empty.\n");
return;
} // end if
DLN cursor = head;
while(cursor != null) {
System.out.println(cursor.
cursor = cursor.getNext();
} // end while
} // end printList
--------------------------
Then in my Ears method, I added this statement:
public class Ears implements ActionListener {
public void actionPerformed(ActionEven
String whichButton = e.getActionCommand();
if(whichButton.equals("Add
idInput = id.getText();
userInput = idInput + ", " + name.getText();
try {
int iIDInput = Integer.parseInt(idInput);
if(iIDInput<1 || iIDInput>2000) {
error.setText("Please enter an integer between 1 and 2000.");
} // end if
if(iIDInput>0 && iIDInput<2001){
try {
list.add(userInput);
listArea.setText(list.prin
}
catch (SortedListException ex) {
error.setText("Sorry, this person could not be added.");
}
} // end if
} // end try
catch(NumberFormatExceptio
error.setText("Sorry, the ID is not a valid integer.");
} // end catch
} // end if
if(whichButton.equals("Rem
if(list.isEmpty()==true) {
error.setText("Sorry, there are no people in the list.");
} // end if
if(list.isEmpty()==false) {
remove(0);
} // end if
} // end if
} // end actionPerformed
} // end Ears
--------------------------
This line: listArea.setText(list.prin
^
1 error
--------------------------
If I can't do that, how would I print the list to the TextArea of the applet?
Thanks!
-luna621 =^^=
ASKER
Okay, I just tested my add method and I get NullPointerException. What happened?
http://www2.hawaii.edu/~flam/SortedList.java
http://www2.hawaii.edu/~flam/SortedList.java
ASKER
Alrighty, re-wrote the add method. But, now I get errors regarding my compareTo method:
Here's a sample run:
-------------------------- ---------- ---------- ---------- --------
Enter your String -or- press <ENTER> to quit: fsjsakfs
Enter your String -or- press <ENTER> to quit: fsfsdfs
Exception in thread "main" java.lang.NullPointerExcep tion
at DLN.compareTo(DLN.java:57)
at SortedList.add(SortedList. java:44)
at MyList.main(MyList.java:46 )
Press any key to continue . . .
Here's a sample run:
--------------------------
Enter your String -or- press <ENTER> to quit: fsjsakfs
Enter your String -or- press <ENTER> to quit: fsfsdfs
Exception in thread "main" java.lang.NullPointerExcep
at DLN.compareTo(DLN.java:57)
at SortedList.add(SortedList.
at MyList.main(MyList.java:46
Press any key to continue . . .
ASKER
Sorry, here are the codes:
1. http://www2.hawaii.edu/~flam/DLN.java
2. http://www2.hawaii.edu/~flam/ISortedList.java
3. http://www2.hawaii.edu/~flam/Person.java
4. http://www2.hawaii.edu/~flam/SortedList.java
5. http://www2.hawaii.edu/~flam/SortedListException.java
5. http://www2.hawaii.edu/~flam/MyList.java
1. http://www2.hawaii.edu/~flam/DLN.java
2. http://www2.hawaii.edu/~flam/ISortedList.java
3. http://www2.hawaii.edu/~flam/Person.java
4. http://www2.hawaii.edu/~flam/SortedList.java
5. http://www2.hawaii.edu/~flam/SortedListException.java
5. http://www2.hawaii.edu/~flam/MyList.java
ASKER
Is something wrong with the compareTo() in DLN.java?
public int compareTo(Object obj) {
DLN node = (DLN)obj;
return getItem().compareTo(node.g etItem());
} // end compareTo()
public int compareTo(Object obj) {
DLN node = (DLN)obj;
return getItem().compareTo(node.g
} // end compareTo()
ASKER
Okay, I guess I should ask another question since my question about reading from the user input was already answered =^^=
ASKER
Thank you!!
ASKER
String userInput = id.getText();
userInput = userInput + ", " + name.getText();