I am having troubles with my "for" loop in my program. The main error I am getting is "type mismatch: cannot convert from int to Boolean." All I want to do is to have the corresponding "time" element printed out. I know that conditions need to be of type bool for "classNames[0].length", but I can't figure out how to do that. Thanks!
import javax.swing.JOptionPane;
import java.util.Scanner;
public class arraytest {
public static void main(String[] args)
{
int i = 0;
String classInput;
String[][] classNames = {
{"CIS 280", "ACC 212", "HIS 300"},
{"Tue 7:30", "Thu 2:30", "Fri 1:00" }
};
classInput = JOptionPane.showInputDialog("Please input a class name: ");
for (i = 0; classNames[0].length; ++i) // problem line
if(classNames[0][i].equals(classInput))
{
System.out.println("class time:" + classNames[1][i]);
}
}
}
You probably wanted to compare i
to classNames[0].length
to provide the necessary boolean
argument. Providing just classNames[0].length
is just an int
.
for (i = 0; i < classNames[0].length; ++i)
我想你的意思是:
for (i = 0; i < classNames[0].length; i++)
import javax.swing.JOptionPane;
import java.util.Scanner;
public class arraytest {
public static void main(String[] args)
{
int i = 0;
String classInput;
String[][] classNames = {
{"CIS 280", "ACC 212", "HIS 300"},
{"Tue 7:30", "Thu 2:30", "Fri 1:00" }
};
classInput = JOptionPane.showInputDialog("Please input a class name: ");
for (i = 0; i<classNames[0].length; ++i) // problem line
if(classNames[0][i].equals(classInput))
{
System.out.println("class time:" + classNames[1][i]);
}
}
}
this will solve your error you just forgot the i<
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.