I'm trying to read about using while loops for pretest conditions for a small program that will compile responses and output data on them, but I'm having an issue where no matter what I enter in the input box it tells me it's invalid. I'm not sure what's wrong. Here's the relevant code.
import javax.swing.JOptionPane;
public class SurveySummarization
{
public static void main(String[] args)
{
int agree = 0;
int disagree = 0;
int neutral = 0;
int totalVotes = 0;
int input;
String inputString;
inputString = JOptionPane.showInputDialog("Response: \n" +
"(1=agree, 2=disagree, 3=no opinion, -1=exit)");
input = Integer.parseInt(inputString);
while (input != -1)
{
if (input == 1)
{
agree += 1;
totalVotes += 1;
}
if (input == 2)
{
disagree += 1;
totalVotes += 1;
}
if (input == 3)
{
neutral += 1;
totalVotes += 1;
}
else {
JOptionPane.showMessageDialog(null, "invalid response "
+ input);
}
}
}
}
inputString,inputat each line of code? Also, note that yourelsestatement will trigger for any values ofinputexcept for 3.