Jump to content
    import javax.swing.JOptionPane; //Importing class for later use.

 

 

public class Survey

 

{

public static void main(String[] args)

{

Double percentAgree; //Percent of people that agree.

Double percentDisagree; //Percent of people that disagree.

Double percentNoOpinion; //Percent of people that have no opinion.

int amountAgree;//Number of people who agree.

int amountDisagree;//Number of people who disagree.

int amountNoOpinion;//Amount of people with no opinion.

int input;//What the user inputs when prompted.

final int AGREE = 1;

final int DISAGREE = 2;

final int NO_OPINION = 3;

String jOptinput;

 

jOptinput =JOptionPane.showInputDialog("Response:\n(1=agree,2=disagree,3=no opinion,-1=exit)");

input = Integer.parseInt(jOptinput);

 

while (input != -1)

{

jOptinput =JOptionPane.showInputDialog("Response:\n(1=agree,2=disagree,3=no opinion,-1=exit)");

input = Integer.parseInt(jOptinput);

}

 

if (input > 3 || input < -1)

{

JOptionPane.showMessageDialog(null, "Invalid Response: "+input);

return;

}

if (input == 1)

{

amountAgree += 1;

}

else if (input == 2)

{

amountDisagree += 1;

}

else if (input == 3)

{

amountNoOpinion += 1;

}

 

 

System.out.println("RESPONSE\tFREQUENCY\tPERCENT\t\tHISTOGRAM");

System.out.println(" Agree\t\t\t" + amountAgree);

System.out.println(" Disagree\t\t" + amountDisagree);

System.out.println(" No Opinion\t\t" + amountNoOpinion);

}

}

Java keeps telling me that the amount variables are uninitialized, but I have no idea how to fix the error. I've tried initializing them to 0 at the top but after it compiles then it never goes to the if statements.

Link to comment
https://linustechtips.com/topic/318266-java-switch-question/
Share on other sites

Link to post
Share on other sites

For your first while loop, it should be while (input != -1 || input !=1 || input !=2 || input != 3)

 

or put the if statements within the while loop. The way it is now, if you put in anything other than -1 it will stay in the while loop forever. 

      import javax.swing.JOptionPane; //Importing class for later use.  public class Survey {public static void main(String[] args){Double percentAgree = 0; //Percent of people that agree.Double percentDisagree = 0; //Percent of people that disagree.Double percentNoOpinion = 0; //Percent of people that have no opinion.int amountAgree = 0;//Number of people who agree.int amountDisagree = 0;//Number of people who disagree.int amountNoOpinion = 0;//Amount of people with no opinion.int input = 0;//What the user inputs when prompted.final int AGREE = 1;final int DISAGREE = 2;final int NO_OPINION = 3;String jOptinput = ""; jOptinput =JOptionPane.showInputDialog("Response:\n(1=agree,2=disagree,3=no opinion,-1=exit)");input = Integer.parseInt(jOptinput); while (input == 0){      jOptinput =JOptionPane.showInputDialog("Response:\n(1=agree,2=disagree,3=no opinion,-1=exit)");      input = Integer.parseInt(jOptinput);       if (input > 3 || input < -1)                JOptionPane.showMessageDialog(null, "Invalid Response: "+input);        if (input == 1){             amountAgree += 1;            input = 0;        }        else if (input == 2){     amountDisagree += 1;     input = 0;          }        else if (input == 3){      amountNoOpinion += 1;      input = 0; }}//while loop  System.out.println("RESPONSE\tFREQUENCY\tPERCENT\t\tHISTOGRAM");System.out.println(" Agree\t\t\t" + amountAgree);System.out.println(" Disagree\t\t" + amountDisagree);System.out.println(" No Opinion\t\t" + amountNoOpinion);}}

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
https://linustechtips.com/topic/318266-java-switch-question/#findComment-4326098
Share on other sites

Link to post
Share on other sites

Just a tip for the future; if you use pastebin.com and give us a link to it, it keeps the formatting and is much easier to read and see problems with.

Or you can use a great piece of software called puush. (http://www.puush.me). It's a great piece of software that allows for automatic screenshot uploading that will put the link in your clipboard, as well as text document uploading.

 

Pressing CTRL + Shift + 5 will automatically upload what you have on your clipboard into a formatted text document. 

http://puu.sh/hoxZF/b243e2b090.txt 

Software developer, part-time potato. CEO @ RSN. http://rushservers.net/

Link to comment
https://linustechtips.com/topic/318266-java-switch-question/#findComment-4326144
Share on other sites

Link to post
Share on other sites

Just a tip for the future; if you use pastebin.com and give us a link to it, it keeps the formatting and is much easier to read and see problems with.

Or You	Can	 Use	Fantastic CodeBlock

As for your issue, you need to initialize them at the top. Not sure what your code is suppose to do but it will keep asking for response until you enter -1.

At which point it will check the if statement, which is false since -1 is not (input > 3 || input < -1). And then check if it is equal to 1, then 2 then 3, and then print and end.

 

I suggest you follow djdwosk97 advice. Actually try the following:

import javax.swing.JOptionPane; //Importing class for later use.public class Survey{    public static void main(String[] args)    {        Double percentAgree = 0; //Percent of people that agree.        Double percentDisagree = 0; //Percent of people that disagree.        Double percentNoOpinion = 0; //Percent of people that have no opinion.        int amountAgree = 0;//Number of people who agree.        int amountDisagree = 0;//Number of people who disagree.        int amountNoOpinion = 0;//Amount of people with no opinion.        int input = 0;//What the user inputs when prompted.        final int AGREE = 1;        final int DISAGREE = 2;        final int NO_OPINION = 3;        String jOptinput = "";            while (input != -1)        {            jOptinput = JOptionPane.showInputDialog("Response:\n(1=agree,2=disagree,3=no opinion,-1=exit)");            input = Integer.parseInt(jOptinput);                        if (input == 1)            {                amountAgree += 1;            }            else if (input == 2)            {                amountDisagree += 1;            }            else if (input == 3)            {                amountNoOpinion += 1;            }            else if (input != -1)                JOptionPane.showMessageDialog(null, "Invalid Response: "+input);            }        }                    System.out.println("RESPONSE\tFREQUENCY\tPERCENT\t\tHISTOGRAM");        System.out.println(" Agree\t\t\t" + amountAgree);        System.out.println(" Disagree\t\t" + amountDisagree);        System.out.println(" No Opinion\t\t" + amountNoOpinion);    }}

Its a classic case of what we call a feature. Works as intended.

Edited by KuroSetsuna29
Link to comment
https://linustechtips.com/topic/318266-java-switch-question/#findComment-4326210
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×