Results 1 to 8 of 8
  1. #1
    jls7168 is offline Member
    Join Date
    Nov 2008
    Posts
    9
    Rep Power
    0

    Question 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
    Java 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;
    	}
    The class to pass it to
    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);
    					
    									
    		}	
    }

  2. #2
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    To convert String to int:
    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); }
    Hope this helped. Note: Double, Long, Short, etc wrapper classes have similar parsing methods.
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  3. #3
    jls7168 is offline Member
    Join Date
    Nov 2008
    Posts
    9
    Rep Power
    0

    Default

    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?

  4. #4
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    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.
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  5. #5
    jls7168 is offline Member
    Join Date
    Nov 2008
    Posts
    9
    Rep Power
    0

    Default

    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++){

  6. #6
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    I just told you how: arguments or fields.
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  7. #7
    jls7168 is offline Member
    Join Date
    Nov 2008
    Posts
    9
    Rep Power
    0

    Default

    I have a field int length;, but it does not do anything.

  8. #8
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    Then initialize it and then the other class can use it.
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

Similar Threads

  1. Replies: 1
    Last Post: 01-30-2009, 06:44 PM
  2. JtextField
    By kashifu in forum Advanced Java
    Replies: 2
    Last Post: 06-27-2008, 04:25 PM
  3. Constraining Input in JTextField
    By kataya in forum AWT / Swing
    Replies: 1
    Last Post: 06-26-2008, 06:25 PM
  4. how to take input and verify input in Java programs
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-21-2007, 08:46 AM
  5. help with JTextfield
    By gary in forum New To Java
    Replies: 4
    Last Post: 07-11-2007, 01:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •