0

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);
            }
        }


    }
}
1
  • 5
    Have you tried to debug it? Debug is the key, it helps in 95% of cases. What are values of inputString, input at each line of code? Also, note that your else statement will trigger for any values of input except for 3. Commented Mar 8, 2017 at 16:31

3 Answers 3

2

It's because you're not using else's properly. If you look at your code, your final if is

if (input == 3)
        {
            neutral += 1;
            totalVotes += 1; 
        }
        else {
            JOptionPane.showMessageDialog(null, "invalid response "
                                    + input);
        }

meaning if input != 3, show an invalid response.

To fix this, change the if's to else if (input == 2)... (and the same for == 3).

Sign up to request clarification or add additional context in comments.

5 Comments

Ok, so basically the else is only applying to the final if statement?
That's right. At the moment, all your if's are entirely seperate conditions, and the computer is going through each one individually.
What would you recommend I use to accomplish this?
As I put in my answer, "To fix this, change the if's to else if (input == 2)... (and the same for "== 3").". :)
@Josh don`t forget to check this as an answer if this helped solve your problem
1

As pointed out by Steve, the if's are not properly put. I think you meant to put else if's instead of just standalone if's.

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;
            }else if (input == 2)
            {
                disagree += 1;
                totalVotes += 1;
            } else if (input == 3)
            {
                neutral += 1;
                totalVotes += 1; 
            }
            else {
                JOptionPane.showMessageDialog(null, "invalid response "
                                    + input);
            }
        }

    }
}

Comments

0

Since you know input cannot be equal to 1 & 2 & 3 at the same time you should use else if's, along with the first if and final else. Your current code checks if input is equal to 1, if it is great. then you check if it's equal to 2, but your previous statement alright concluded that input was equal to 1, therefore you don't need to check for == 2, or == 3. Using if/else if/else chained together will only satisfy a single condition when chained together. once you hit a condition that satisfies your condition you skip the rest of the conditions.

if (input == 1)
{
    agree += 1;
    totalVotes += 1;
}
else if (input == 2)
{
    disagree += 1;
    totalVotes += 1;
}
else if (input == 3)
{
    neutral += 1;
    totalVotes += 1; 
}
else {
    JOptionPane.showMessageDialog(null, "invalid response " + input);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.