Results 1 to 4 of 4

Thread: Little Help

  1. #1
    crab_com is offline Member
    Join Date
    Jul 2007
    Posts
    2
    Rep Power
    0

    Default Little Help

    Hey all thought i would sign up here and see if anyone would be able to help me.

    I'm doing some simple java and wondering if anyone can find the problem in the script.

    It says that the line num1 = Integer.parseInt(value1.getText()); //Convert text value1 and value2 is incorrect but in fact from the example given in my booklet everything i have is correct...

    heres the script:

    import java.applet.Applet;
    import java.awt.*;
    import java.awt.event.*; //Handels events initiated by the user


    //This class extends the previous class ComponentDemo to actually provide a means of adding
    //up the numbers. It does this y including an Action Event method.
    //Additions to ComponentDemo are in bold type.

    //Simple class to demonstrate the use of Graphics Components.
    //It provides the user interface add to two numbers. WITHOUT actually doing the addition.
    //The extended class to perform the addition is EventDemo.

    public class EventDemo extends Applet implements ActionListener {

    Label myPrompt1, myPrompt2; //declare prompts (Lables)
    TextField value1, value2; //declare user input (text)
    Button addButton; //declare a button to start addition
    int num1, num2, answer; //integers displayed in text fields and their sum

    //The init method sets up the GUI components that have been declared globally.
    //To do this we must:
    // - "instantiate" the components using the "new" command. This provides space in memory for the components.
    // - "add" the components to the applet window, in the left to right, up to down order in
    // which they are added in init.

    public void init() {
    myPrompt1 = new Label("This class will add two numbers.");
    myPrompt2 = new Label("Type in the numbers in the spaces.");
    value1 = new TextField(10); //width of TextField
    value2 = new TextField(10);
    addButton = new Button("Press to add"); //text in Button
    add (myPrompt1);
    add (myPrompt2);
    add (value1);
    add (value2);
    add (addButton);
    addButton.addActionListener(this); //Tell the class which event to listen to:
    }

    //The method "actionPeformed (ActionEvent e) is used to "listen" to events and respond

    public void actionPerformed(ActionEvent e) {
    //The user must type the integers into the text fields. When the button is pressed,
    // - the text field strings are converted to inteers (two different methods are
    // provided - they do the same job.
    // - the integers are added together
    // - the sum is displayed in the status bar.

    num1 = Integer.parseInt(value1.getText()); //Convert text value1 and value2
    num2 = Integer.valueOf(value2.getText()).intValue();
    answer = num1 + num2; //Find the sum
    showStatus(Integer.toString(answer)); //Display the sum
    repaint();
    }
    }

  2. #2
    valery is offline Member
    Join Date
    Jul 2007
    Posts
    32
    Rep Power
    0

    Default

    you didn't set any value to value1

    try that
    Java Code:
    value1 = new JTextField(10);
    value1.setText("167021"); 
    int num1 = Integer.parseInt(value1.getText());

  3. #3
    crab_com is offline Member
    Join Date
    Jul 2007
    Posts
    2
    Rep Power
    0

    Default

    think my program screwed its self i created a new project and it fixed and compiled very strange...

    anyways thankyou for your help....

    ohh one more thing how do i go about generating random numbers from a certain range for example 0 - 100

    cheers, crab

  4. #4
    leonard is offline Member
    Join Date
    Jul 2007
    Posts
    43
    Rep Power
    0

    Default

    you can use the Random class to generate random integers between 0 and N.

    Java Code:
    import java.util.Random;
    import java.util.List;
    import java.util.ArrayList;
    
    public class Example{
    
      public static void main (String args[]) {
        Example example= new Example();
        //the following creates NON-RANDOM lists of integers, since
        //Random objects are created in rapid succession with the same seed
        //value taken from the system time.
        for (int idx=0; idx<10; idx++) {
          System.out.println("In range 0..9 (inclusive):" + example.pickNumber(10));
        }
        for (int idx=0; idx<10; idx++) {
          System.out.println("In range 1..10(inclusive):" + example.pickNumberInRange(1,10));
        }
        //this list is indeed random
        List numbers = example.pickNumbers(10, 5);
        System.out.println("Random list built from ONE Random object:" + numbers);
      }
    /**
    * Get a single random integer in the range 0 (inclusive) to N (exclusive).
    *
    * Important: if this method is called multiple times in rapid
    * succession, it will return the SAME result.
    *
    * @param aUpperLimit must be greater than 0.
    * @exception IllegalArgumentException if param does not comply.
    */
    Java Code:
      public int pickNumber( int aUpperLimit ){
        if (aUpperLimit <=0) {
          throw new IllegalArgumentException("UpperLimit must be positive: " + aUpperLimit);
        }
        //if the no-argument constructor is used, the seed is taken as
        //the current time; thus, Random objects created in rapid succession
        //in this way will have the same seed, and will generate the same sequence
        //of random numbers.
        Random generator = new Random();
        return generator.nextInt(aUpperLimit);
      }

Posting Permissions

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