Subject: Re: toString vs. getText Date: Tue, 1 Oct 1996 21:31:53 -0400 (EDT) From: "Deitel & Associates, Inc." To: cappello@cs.ucsb.edu Hi Pete, Thanks for your email. This is Paul Deitel. When the user presses the enter key in a TextField, the second argument of the action method is the String in the TextField. The call o.toString() gives us that String. The call input.getText() also allows you to retrieve the String in the TextField. Using input1.toString() says that you would like to convert the entire TextField object to a String. This is not the same as getting the String in the TextField (actually it gives you a String that contains more information than the String in the TextField -- such as the class name). When you pass this String to the Integer.parseInt() method, it attempts to convert all the characters in the String to a numeric format. Because there are non-digit characters in the String, the parseInt method generates an exception. This exception is not fatal. It simply prevents you from performing the comparison until you pass Strings that contain only numeric digits to the parseInt method. The getText method is used on page 41 for two reasons. First, it is there to show you that you can always use the getText method on a TextField object to retrieve the String in the TextField. Second, in this example, we have two TextFields from which to read data. When the user presses the enter key in a TextField, only the String from that specific TextField is passed to the action method. To get both Strings from the two TextFields, the easiest way is to use getText. Best wishes, Paul and Harvey PS: If you have any more questions on this, please send a copy of your emails to p_deitel@msn.com At 06:00 PM 10/1/96 -0700, you wrote: >Harvey, > >I got this question below from a student in my class. I pondered the >question for some time, and played with the two applets in question. >When one substitutes toString() in the Comparison applet for getText(), >the resulting applet gets an exception. I think that an entirely >satisfactory answer may require an intimate knowledge of Java's >event-driven window system, something that I don't have yet, and >something that we certainly cannot expect students to have when reading >Chapter 1. Hope I'm wrong. > >How would you answer his question? > >-Pete > >Mike Halcrow wrote: > >> The program given on page 29 of the Java textbook reads the following >> command for line 25 of the code: >> --- >> number = Integer.parseInt( o.toString() ); // get number >> --- >> >> On page 41, the code on line 56 reads: >> --- >> num1 = Integer.parseInt( input1.getText() ); >> --- >> >> As far as I could tell, both lines serve the same function (to convert a >> string into an integer), but the book did not make it clear why it used >> one technique for the first program and another for the second program. >> Any help would be appreciated. >> >> Mike Halcrow >> miha4549@resnet.ucsb.edu >> or >> michael.halcrow@resnet.ucsb.edu (same mailbox, easier to remember) > > ---------------------------------------- CURRENTLY AT A STORE NEAR YOU: Java How to Program C & C++ Multimedia Cyber Classroom ---------------------------------------- _______________________________________________________________ Deitel & Associates, Inc. Java(TM), C, C++, Object Technology, Visual Basic(TM), and Windows NT(TM) Education and Consulting deitel@deitel.com http://www.deitel.com Code examples in zip and tar file formats from our texts "C How to Program: Second Edition" and "C++ How to Program" can be found on the downloads page of our web site: http://www.deitel.com Prentice Hall book orders: 1-800-922-0579 _______________________________________________________________