• Post Reply Bookmark Topic Watch Topic
  • New Topic

New Student Having trouble with Applet Assignmet  RSS feed

Mark Harriett
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a student in my first semester of Java and I am having trouble with an Applet assignment. The basis of the assignment is to have the program ask the user to enter 4 integers and have the output on the screen seen in asteriks. Example if I enter the number 7, then 7 asteriks appear in the applet. The problem I am having is i can't get the asteriks to appear on the screen, all I get is a big red X. I tried using an array as a global varibale thinking that it could be accessed by both the Init method and the Paint method. Here is the code :
import javax.swing.JApplet;

public class Stars extends JApplet {
String result[] = new String[5];

public void init()

int x;
int i;
String answer;

for (int y = 0; y <=4; y++) {
answer = JOptionPane.showInputDialog("Please enter a number between 1 and 30");
x = Integer.parseInt(answer);

if (x >= 1 & x <= 30)
{ for (i = 1; i <= x; i++)
result[y] += "*";

} else {
JOptionPane.showMessageDialog(null,"You didn't enter a number between 1 and 30"); }


System.exit (0);
public void paint (Graphics g)

g.drawString("Hi", 50,50);
I originally had " result[x]" in the paint method but it didn't like it so I put the "Hi" in there just to test out the paint method was working but even then I got the X. Can someone tell me what I am doing wrong?
Thanks !!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!