0

My assignment is to input 20 numbers via a text field then out the mean, the median and the total using a while loop. I should be able to figure out the while loop myself, but I can't get the text field to input numbers into an array. Please help, here is my code so far:

 import java.applet.Applet;
import java.awt.Graphics;
import java.awt.*;
import java.awt.event.*;

public class whileloopq extends Applet implements ActionListener
{
    Label label;
    TextField input;
    int[] numArray = new int[20];
    int num;

    public void init ()
    {
        Label label = new Label("Enter numbers");
        TextField input = new TextField(5);
        add(label);
        add(input);
        input.addActionListener(this);
    }

    public void actionPerformed (ActionEvent ev)
    {
        int num = Integer.parseInt(input.getText());
        int index = 0;
        numArray[index] = num;
        index++;
        input.setText("");

    }

    public void paint (Graphics graf)
    {
        graf.drawString("Array" + numArray, 25, 85);
    }
}

Any help would be much appreciated.

3 Answers 3

1

(Answer written under the assumption that this is a homework assignment.)

You know how to parse an integer from a string, as you show with your usage of Integer.parseInt, but you are calling it to parse the entire 20 characters as one integer. You need to get each character individually to be parsed.

I recommend using a for loop, and String#substring to substring the input text into several strings of length one.

Alternatively, you can split the input text around an empty string and then iterate through the resulting array (note that the first string in the array will be empty), but the other approach is more likely the one expected from someone new to Java, so you'll have to use your judgement here.

Sign up to request clarification or add additional context in comments.

Comments

0

In actionPerformed() you are trying to read from class filed input.setText("");

but in init() you didn't initialized that field but created and added to applet local variable

TextField input = new TextField(5);

so class field is steal null. Change it to

input = new TextField(5);

Comments

0
import java.awt.*;
public class frame4array extends Frame
{
Checkbox c1[];
TextField t1[];
int i;
frame4array(String p)
{
super(p);
c1=new Checkbox[2];
t1=new TextField[2];
for(i=0;i<2;i++)
{
t1[0]=new TextField();
t1[0].setBounds(200, 50, 150, 30);
t1[1]=new TextField();
t1[1].setBounds(200, 80, 150, 30);
c1[0]=new Checkbox("Singing");
c1[0].setBackground(Color.red);
c1[0].setBounds(430,200,120,40);
c1[1]=new Checkbox("Cricket",true);
}
for(i=0;i<2;i++)
{
add(t1[i]);
add(c1[i]);
}
setFont(new Font("Arial",Font.ITALIC,40));
}
public static void main(String s[])
{
frame4array f1=new frame4array("hello");
f1.setSize(600,500);
f1.setVisible(true);
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.