Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
anyone familiar with this field of java?
The problem is that the print statement JOptionPane does not like to recognize that the word input exists, because it was initiated inside of the for loop. Think you can help? it is not finished yet, in case you are wondering. But this is everything that I have so far.
package javaapplication63;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class JavaApplication63 {
public static void main(String[] args) {
int numStudents = Integer.parseInt(JOptionPane.showInputDialog
(null, "Create your class, How many people are in your class?"));
String[] students = new String[numStudents];
for(int i = 0; i < numStudents; i++){
students[i] = JOptionPane.showInputDialog
("Now enter the names of your students. "
+ "Student #" + (i + 1) + ":");
}
JTextField[] nameBox = new JTextField[numStudents];
for(int i = 0; i < numStudents; i++){
nameBox[i] = new JTextField();
final JComponent[] inputs = new JComponent[] {
new JLabel(students[i]),
nameBox[i],
};
}
JOptionPane.showMessageDialog(null, inputs, "A-Absent, T-Tardy, "
// problem = inputs
+ "E-Excused, nothing-on time", JOptionPane.PLAIN_MESSAGE);
}
}
So how can I make it so that it can still be in the loop but be recognized by stuff outside of the loop? is there a different approach that I could try?
this is another idea i had but it doesn't seem to work either:
final JComponent[] inputs;
for(int i = 0; i < numStudents; i++){
nameBox[i] = new JTextField();
inputs = new JComponent[]{
new JLabel(students[i]),
nameBox[i],
};
}
this definately does not work:
JTextField[] nameBox = new JTextField[numStudents];
for(int i = 0; i < numStudents; i++){
nameBox[i] = new JTextField();
}
final JComponent[] inputs = new JComponent[] {
for(int i = 0; i < numStudents; i++){
new JLabel(students[i]),
nameBox[i],
}
};
2 Answers
- KaydellLv 78 years agoFavorite Answer
You'll have to write a specification of what it is you're trying to do.
You're creating some components, JTextField and JLabel objects are components, so you must want to put them into a window, a JFrame. Try extending JFrame and adding your components to the frame.
- godfatherofsoulLv 78 years ago
Your code won't work since inputs is declared inside your for loop. It can't be seen outside that for loop. YOur code also doesn't seem to make logical sense since you're looping over students, creating text fields, and an array of inputs for each iteration.