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?
There are a few errors, one is that I need the variable "inputs" to be recognized outside of the for loop, it is because it was created inside the for loop, I was wondering if there was a way to go around that or a different approach I could try. Also, there is a minor issue with the char variables at the end. They do not like to be in if statements and be treated like Strings. How do you use char's in if statements like the way I need to?
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?"));
//lets you chose how big your array is or your "class(like in school)"
String[] students = new String[numStudents];
//creates array that the students in the class will be in
for(int i = 0; i < numStudents; i++){
students[i] = JOptionPane.showInputDialog
("Now enter the names of your students. "
+ "Student #" + (i + 1) + ":");
}
//lets you input the names of the students in the class
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, "
+ "E-Excused, nothing-on time", JOptionPane.PLAIN_MESSAGE);
//creates a pop up dialog box with as many places to type in
//as you have students, and has the name of each student next
//to the place to type. This means that you should be able to
//write an A T or E next to each students name.
int howManyAbsent = 0;
int howManyTardy = 0;
int howManyExcused = 0;
for(int i = 0; i < numStudents; i++){
if(nameBox[i].getText().equalsIgnoreCase('A')){
howManyAbsent = howManyAbsent + 1;
}
else if(nameBox[i].getText().equalsIgnoreCase('T')){
howManyTardy = howManyTardy + 1;
}
else if(nameBox[i].getText().equalsIgnoreCase('E')){
howManyExcused = howManyExcused + 1;
}
}
//goes through the list of A's, T's, E's, and Blanks,
//and for each one, adds 1 to a corresponding variable
JOptionPane.showMessageDialog
(null, "# of Absent Students: " + howManyAbsent
+ "# of Tardy Students: " + howManyTardy
+ "# of Excused Students: " + howManyExcused);
//prints out a page with the number of each student who
//was not on time.
}
}
1 Answer
- green meklarLv 78 years agoFavorite Answer
You are free to declare inputs before starting the for loop. However, if you want to change it to something new each time the for loop iterates, you won't be able to declare it as final. I'm not sure why you're declaring it as final, so unless you have a good reason, you can just remove that qualifier.
Alternatively, you could declare another variable of the same type before the loop, and then inside the loop, set that variable equal to inputs (after inputs has been declared and initialized, of course). Since Java object variables (including array variables) are pointers, this should give you the same power that you need.