Results 1 to 8 of 8
- 02-20-2009, 12:42 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 9
- Rep Power
- 0
Convert Input from JTextField to int?
Hi, I need help on converting the input from a JTextField to an int. The input is supposed to be the length of a race and I need to pass it to another class to use. I am completely lost....here is the code...any help is reall appreciated.
I removed a lot of the unecessary GUI code
The class to pass it toJava Code:class RaceGui extends JFrame { private static final long serialVersionUID = 1L; private JPanel jContentPane = null; private JTextArea lengthTextArea = null; private JButton startButton = null; private JButton clearButton = null; private JButton exitButton = null; private JTextArea resultsTextArea = null; public JTextField lengthTextField; private String input; int racelen; private JTextArea getResultsTextArea() { if (resultsTextArea == null) { resultsTextArea = new JTextArea(); resultsTextArea.setBounds(new Rectangle(16, 61, 571, 332)); } return resultsTextArea; } private JTextField getLengthTextField() { if (lengthTextField == null) { lengthTextField = new JTextField(); lengthTextField.setBounds(new Rectangle(104, 16, 125, 29)); } return lengthTextField; }
Java Code:class Horse extends Thread { JTextField racelen = null; JTextArea resultsTextArea = null; int move; int move1; int total; long totalTime; public Horse(JTextField len, JTextArea results){ super(); racelen = len; resultsTextArea = results; } public void run(){ long time = System.currentTimeMillis();; for (int total = 0; total < racelen; total++){ move = (int)(Math.random() * 100); move = (int)(Math.sqrt(move)); move += total; System.out.println(total + " " + getName()); try { sleep((int)(Math.random() * 100)); }catch (InterruptedException e) {} } totalTime = System.currentTimeMillis() - time; resultsTextArea.setText("Priority\t" + "Name\t" + "Time\n" + getPriority() + "\t" + getName() +"\t"+ totalTime); } }
- 02-20-2009, 12:49 AM #2
To convert String to int:
Hope this helped. Note: Double, Long, Short, etc wrapper classes have similar parsing methods.Java Code:String s = "23"; String s2 = "not a number"; int i = Integer.parseInt(s); // returns an int, 23 try { int i2 = Integer.parseInt(s2); // throws error, s2 isn't a number } catch(Exception e) { System.out.println(e); }
-MK12Tell me if you want a cool Java logo avatar like mine and I'll make you one.
- 02-20-2009, 12:58 AM #3
Member
- Join Date
- Nov 2008
- Posts
- 9
- Rep Power
- 0
I dont get any errors when I do that, but how would I pass that int to the Horse class to use in the for loop?
- 02-20-2009, 01:03 AM #4
1. Which class is running and using the other class? If you are running a class (the main class, or driver class) Which uses the Horse class, but you need to pass values to the horse class, you could pass them as arguments if you are calling methods from Horse, or you could make the fields in the main class and then use them in your Horse class.
-MK12Tell me if you want a cool Java logo avatar like mine and I'll make you one.
- 02-20-2009, 01:07 AM #5
Member
- Join Date
- Nov 2008
- Posts
- 9
- Rep Power
- 0
Its essentially a horse race. The RaceGui is running, but horse class is where the "race" takes place. I need to get the length of the race from the RaceGui class and pass it to the Horse class to put in the for loop.
Java Code:class Horse extends Thread { int length = 0; JTextArea resultsTextArea = null; int move; int move1; int total; long totalTime; public Horse(int len, JTextArea results){ super(); length = len; resultsTextArea = results; } public void run(){ long time = System.currentTimeMillis();; for (int total = 0; total < length; total++){
- 02-20-2009, 01:45 AM #6
I just told you how: arguments or fields.
-MK12Tell me if you want a cool Java logo avatar like mine and I'll make you one.
- 02-20-2009, 02:28 AM #7
Member
- Join Date
- Nov 2008
- Posts
- 9
- Rep Power
- 0
I have a field int length;, but it does not do anything.
- 02-20-2009, 02:29 AM #8
Similar Threads
-
how to access jTextField of one JFrame1 from JFrame2 & Modify JTextField contents
By sumit1mca in forum AWT / SwingReplies: 1Last Post: 01-30-2009, 06:44 PM -
JtextField
By kashifu in forum Advanced JavaReplies: 2Last Post: 06-27-2008, 04:25 PM -
Constraining Input in JTextField
By kataya in forum AWT / SwingReplies: 1Last Post: 06-26-2008, 06:25 PM -
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 -
help with JTextfield
By gary in forum New To JavaReplies: 4Last Post: 07-11-2007, 01:58 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks