Im creating a project where the area or volume of certain shapes are calculated using dropdown menus in java. I don't have any errors when compiling however I'm getting the message that "Unlikely argument type for equals(): String seems to be unrelated to String[]". As stated, it complies fine, but when running it allows area/volume to be selected, but it does not reach the next option pane to select the shapes.
The message appears on: if (choices.equals("Area")) and : if (choices.equals("Volume"))
`
import javax.swing.JOptionPane;
public class Shapes
{
public static void main(String[] args)
{
//Dropdown menu for area and volume
String[] choices = {"Area", "Volume"};
String question = (String) JOptionPane.showInputDialog(null, "What would you like to calculate?",
"Shapes", JOptionPane.QUESTION_MESSAGE, null,
choices,
choices[0]);
System.out.println(question);
if (choices.equals("Area"))
{
//user chooses area
String[] choices2D = {"triangle", "parallelogram", "rectangle", "trapezoid", "circle"};
String question2D = (String) JOptionPane.showInputDialog(null, "What shape will you choose?",
"Shapes", JOptionPane.QUESTION_MESSAGE, null,
choices2D,
choices2D[0]);
System.out.println(question2D);
}
//user chooses volume
if (choices.equals("Volume"))
{
String[] choices3D = {"cone", "cylinder", "rectanglular prism", "trapezoid prism", "sphere"};
String question3D = (String) JOptionPane.showInputDialog(null, "What figure will you choose?",
"Shapes", JOptionPane.QUESTION_MESSAGE, null,
choices3D,
choices3D[0]);
System.out.println(question3D);
}
}
}
`
I originally had the options linked to a switch but would keep running into errors, after changing it to an if statement it will compile but not run properly.
CodePudding user response:
Your comparisons are choices.equals(…)
, but you meant to compare the selection from the dropbox, and this is stored in question
. So the comparisons should be question.equals(…)
.
Alternatively, try this:
import javax.swing.JOptionPane;
public class Shapes
{
public static void main(String... args)
{
//Dropdown menu for area and volume
String[] choices = {"Area", "Volume"};
String question = (String) JOptionPane.showInputDialog( null,
"What would you like to calculate?",
"Shapes",
JOptionPane.QUESTION_MESSAGE,
null,
choices,
choices[0]) ;
System.out.println(question);
switch( question )
{
case "Area":
{
//user chooses area
String[] choices2D = {"triangle", "parallelogram", "rectangle", "trapezoid", "circle"};
String question2D = (String) JOptionPane.showInputDialog( null,
"What shape will you choose?",
"Shapes",
JOptionPane.QUESTION_MESSAGE,
null,
choices2D,
choices2D[0] );
System.out.println( question2D );
break;
}
case "Volume":
{
//user chooses volume
String[] choices3D = {"cone", "cylinder", "rectanglular prism", "trapezoid prism", "sphere"};
String question3D = (String) JOptionPane.showInputDialog( null,
"What figure will you choose?",
"Shapes",
JOptionPane.QUESTION_MESSAGE,
null,
choices3D,
choices3D[0] );
System.out.println( question3D );
break;
}
}
}
}
You can also use the new switch-case
syntax and optimise further, by using enum
s for the various choices.
CodePudding user response:
You are comparing the whole array with a single string. Use choices[0].equals("Area"). Now this will compare choices array with string at index 0 with Area.