New Posts  All Forums:Forum Nav:

Some more basic java help? - Page 2

post #11 of 14
Quote:
Originally Posted by kidshenlong View Post
Thanks guys I'm still not a 100% on this but I get what it basically does. I'm given a String. I convert into something I can apply maths to, an integer. And then the rest is magic. I think my lecturer might've wanted me to use a different method but again the notes aren't too clear. But hey the result in the end is the same. So thanks again guys. Helpful as ever
Sure, yeah.

In complete layman's terms:

Every method you use in Java has to be stored somewhere. Methods are the "actions" that all of your objects are doing. It's really the entire point of OOP .

When you import sets of methods (packages) like the "import javax.swing.*;" you have at the top of your code, you allow your objects to utilize those new actions, provided they are formatted correctly and can compile.

Within the javax.swing package is the JTextField class. That class contains the methods that you're trying to use (there is no getValue()). As an added bonus, common classes like that often have a lot of other methods that are "inherited" from similar functioning classes. In this case, you are able to use the getText() method that was inherited from the javax.swing.text.JTextComponent class. You get this one by default because:
"import javax.swing.*;" means that you're importing every single class beyond the .swing. designation.

Here are a few other methods that you inherited from this class alone:

Code:
addCaretListener, addInputMethodListener, addKeymap, copy, cut, fireCaretUpdate, getCaret, getCaretColor, getCaretListeners, getCaretPosition, getDisabledTextColor, getDocument, getDragEnabled, getFocusAccelerator, getHighlighter, getInputMethodRequests, getKeymap, getKeymap, getMargin, getNavigationFilter, getPreferredScrollableViewportSize, getScrollableBlockIncrement, getScrollableTracksViewportHeight, getScrollableTracksViewportWidth, getScrollableUnitIncrement, getSelectedText, getSelectedTextColor, getSelectionColor, getSelectionEnd, getSelectionStart, getText, getText, getToolTipText, getUI, isEditable, loadKeymap, modelToView, moveCaretPosition, paste, processInputMethodEvent, read, removeCaretListener, removeKeymap, removeNotify, replaceSelection, select, selectAll, setCaret, setCaretColor, setCaretPosition, setComponentOrientation, setDisabledTextColor, setDragEnabled, setEditable, setFocusAccelerator, setHighlighter, setKeymap, setMargin, setNavigationFilter, setSelectedTextColor, setSelectionColor, setSelectionEnd, setSelectionStart, setText, setUI, updateUI, viewToModel, write
From there it's easy cheesy. You understand that you can't add(+) two Strings together, otherwise your 2 + 2 comes out as "22." So you need to transform the String into an Integer. The parse() method that we used here can be found in the Integer class, the exact same way we pulled methods from above.

http://download.oracle.com/javase/1.5.0/docs/api/

This is the last link you should ever need when you're stumped on some of the built-in API .
Edited by Plex - 3/1/11 at 2:46pm
Intellect v2
(9 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7-6700K Processor ASUS ROG MAXIMUS VIII HERO LGA1151 DDR4 M.2 SAT... EVGA GTX 1080 SC ACX 3.0 Crucial Ballistix Sport 32GB DDR4 2400 MT/s (PC... 
CoolingKeyboardPowerCase
Noctua NH-D15 Das Keyboard 4 Professional (Brown) Corsair AX860 Fractal Design Define R5 
Mouse
MIONIX NAOS 7000 
  hide details  
Reply
Intellect v2
(9 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7-6700K Processor ASUS ROG MAXIMUS VIII HERO LGA1151 DDR4 M.2 SAT... EVGA GTX 1080 SC ACX 3.0 Crucial Ballistix Sport 32GB DDR4 2400 MT/s (PC... 
CoolingKeyboardPowerCase
Noctua NH-D15 Das Keyboard 4 Professional (Brown) Corsair AX860 Fractal Design Define R5 
Mouse
MIONIX NAOS 7000 
  hide details  
Reply
post #12 of 14
Right. the JTextField holds a value in it, to obtain access to that value you call the getText() method.

The getText method returns a string representing the value inside of the JTextField instance you created, since you want to add the two values in the text fields, and you know they are both expected to be integers, you want to call the Integer class static parseInt(String s) method.

To call that you simply put Integer.parseInt, you do not have to New up an instance of the integer class because static methods can be used without an active instance of the class.

Since you know that the Integer.parseInt method takes a string to convert, you can call the method simply passing in your JTextField instances getText method that returns the string as shown.

int firstInt = Integer.parseInt(someJTextField.getText());

is the same as

int firstInt = Get me the value in my someJTextField, and convert it to an integer.


Also if your teacher asks you what happens when you try to parse a string that cant be converted to an integer such as a character "Abcdef.... etc" your program will throw an exception and crash. Unless of course you have exception handling in place which im sure you will get to soon.
Edited by mykah89 - 3/1/11 at 3:22pm
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
I5 760 GA-H55N GTX 470 PNY Optima 8GB 
Hard DriveMonitorPowerCase
Spinpoint F4 LG227WTG sg07 PSU Sugo sg07 
Mouse
CM Sentinel 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
I5 760 GA-H55N GTX 470 PNY Optima 8GB 
Hard DriveMonitorPowerCase
Spinpoint F4 LG227WTG sg07 PSU Sugo sg07 
Mouse
CM Sentinel 
  hide details  
Reply
post #13 of 14
Exactly, mykah89. More correctly, the JTextField has a method called getText() which returns the String stored, in a (private) field/variable, within it.
One thing to be wary of:
if you call Integer.parseInt(String s) on a String s that is not a "parsable integer", i.e. if it contains anything besides [1,2,3,4,5,6,7,8,9,0], a NumberFormat exception will be thrown. Note, you can test for this exception, but that is likely out of the scope of your needs at this stage.
For sale
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7-920 Asus P6T Deluxe Asus GTX460 TOP 768mb G Skill ECO 1600 CAS7 1.35V 
Hard DriveOptical DriveOSMonitor
2x Vertex 60 GB raid[0] Asus DVDRW W7,Ubuntu 2 xAsus VH236H 
KeyboardPowerCaseMouse Pad
Razer Ultra X3 1000W HAF 932 My Desk 
  hide details  
Reply
For sale
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7-920 Asus P6T Deluxe Asus GTX460 TOP 768mb G Skill ECO 1600 CAS7 1.35V 
Hard DriveOptical DriveOSMonitor
2x Vertex 60 GB raid[0] Asus DVDRW W7,Ubuntu 2 xAsus VH236H 
KeyboardPowerCaseMouse Pad
Razer Ultra X3 1000W HAF 932 My Desk 
  hide details  
Reply
post #14 of 14
Thread Starter 
Oh wow I just noticed this guys. Thanks. I really mean it. You guys are just the best. Thanks again for all the help. Rep+
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming