Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Hey guys need abit of help please stressing like a mad thing

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    32
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Post Hey guys need abit of help please stressing like a mad thing

    Im trying to program a survey and one of the bits on my survey wont work and heres the little bit that wont work please help!!!
    class jon{
    public static void main(String[] args)
    {

    char yes = 'y', no = 'n', answer1;
    int addition=0, subtraction=0;

    if ('y' < 5)
    {
    System.out.println("Do you drink alcohol? Y/N : ");
    answer1 = Keyboard.readChar( );
    }
    else if (answer1 == 'y')
    {
    addition=(addition + 1);
    }
    else if (answer1 == 'n')
    {
    subtraction=(subtraction + 1);
    }
    }
    }
    i keep getting an error like this
    error : variable answer1 might not have been initialized else if (answer1) == 'y')
    but java underlines answer1
    please i dont know whats going on please help
    heres my full program
    its not finished yet because of this program
    class Survey{
    public static void main(String[ ] args)
    {

    char yes = 'y', no = 'n', answer1, answer2, answer3, answer4, answer5, answer6, answer7,answer8, answer9, answer10, answer11, answer12;
    int addition=0, subtraction=0;

    System.out.print("Do you like sports Y/N: ");
    answer1 = Keyboard.readChar( );
    if (answer1 == 'y')
    {
    addition=(addition + 1);
    }
    else if (answer1 =='n')
    {
    subtraction=(subtraction + 1);
    }

    System.out.print("Do you like ireland Y/N: ");
    answer2 = Keyboard.readChar( );
    if (answer2 == 'y')
    {
    addition=(addition + 1);
    }
    else if (answer2 =='n')
    {
    subtraction=(subtraction + 1);
    }
    System.out.print("Do you like movies Y/N: ");
    answer3 = Keyboard.readChar( );
    if (answer3 == 'y')
    {
    addition=(addition + 1);
    }
    else if (answer3 =='n')
    {
    subtraction=(subtraction + 1);
    }
    System.out.print("Do you like europe Y/N: ");
    answer4 = Keyboard.readChar( );
    if (answer4 == 'y')
    {
    addition=(addition + 1);
    }
    else if (answer4 =='n')
    {
    subtraction=(subtraction + 1);
    }
    System.out.print("Do you like america Y/N: ");
    answer5 = Keyboard.readChar( );
    if (answer5 == 'y')
    {
    addition=(addition + 1);
    }
    else if (answer5 =='n')
    {
    subtraction=(subtraction + 1);
    }
    System.out.print("Do you like Manchester United Y/N: ");
    answer6 = Keyboard.readChar( );
    if (answer6 == 'y')
    {
    addition=(addition + 1);
    }
    else if (answer6 =='n')
    {
    subtraction=(subtraction + 1);
    }
    System.out.print("Do you like Dublin Y/N: ");
    answer7 = Keyboard.readChar( );
    if (answer7 == 'y')
    {
    addition=(addition + 1);
    }
    else if (answer7 =='n')
    {
    subtraction=(subtraction + 1);
    }
    System.out.print("Do you like ITB Y/N: ");
    answer8 = Keyboard.readChar( );
    if (answer8 == 'y')
    {
    addition=(addition + 1);
    }
    else if (answer8 =='n')
    {
    subtraction=(subtraction + 1);
    }
    System.out.print("Do you like computers Y/N: ");
    answer9 = Keyboard.readChar( );
    if (answer9 == 'y')
    {
    addition=(addition + 1);
    }
    else if (answer9 =='n')
    {
    subtraction=(subtraction + 1);
    }
    System.out.print("Do you like programming Y/N: ");
    answer10 = Keyboard.readChar( );
    if (answer10 == 'y')
    {
    addition=(addition + 1);
    }
    else if (answer10 =='n')
    {
    subtraction=(subtraction + 1);
    }

    System.out.println("To Question 1o you like sports Y/N : You answered"+" "+ answer1);
    System.out.println("To Question 2o you like ireland Y/N: You answered"+" "+ answer2);
    System.out.println("To Question 3o you like movies Y/N: You answered"+" "+ answer3);
    System.out.println("To Question 4o you like europe Y/N: You answered"+" "+ answer4);
    System.out.println("To Question 5o you like america Y/N: You answered"+" "+ answer5);
    System.out.println("To Question 6o you like Manchester United Y/N:You answered"+" "+ answer6);
    System.out.println("To Question 7o you like Dublin Y/N:You answered"+" "+ answer7);
    System.out.println("To Question 8o you like ITB Y/N:You answered"+" "+ answer8);
    System.out.println("To Question 9o you like computers Y/N:You answered"+" "+ answer9);
    System.out.println("To Question 10o you like programming Y/N:You answered"+" "+ answer10);

    if ( 'y' < 5)
    {
    System.out.println("Thank you for answering the survey, to complete the survey could you answer these two questions ");
    }
    else if ('y' > 5)
    {
    System.out.println("Thank you for completing the survey ");
    }

    else if ('y' < 5)
    {
    System.out.println("Do you drink alcohol? Y/N : ");
    answer11 = Keyboard.readChar( );
    }
    else if (answer11 == 'y')
    {
    addition=(addition + 1);
    }
    else if (answer11 == 'n')
    {
    subtraction=(subtraction + 1);
    }

    System.out.println("Do you eat potatoes? Y/N : ");
    answer12 = Keyboard.readChar( );
    if (answer12 == 'y')
    {
    addition=(addition + 1);
    }
    else if (answer12 == 'n')
    {
    subtraction=(subtraction + 1);
    }

    System.out.println("To Question 11o you drink alcohol? Y/N : You answered"+" "+ answer11);
    System.out.println("To Question 12: Do you eat potatoes? Y/N : You answered"+" "+ answer12);

    if ( 'y' == 2)
    {
    System.out.print("You have a very agreeable person ");
    }
    else if ('n' == 2)
    {
    System.out.print("You like saying no don't you!");
    }
    }
    }
    Last edited by shep; October 29th, 2012 at 10:07 AM.


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Hey guys need abit of help please stressing like a mad thing

    Quote Originally Posted by shep View Post
    class jon {
        public static void main(String[]args) {
     
            char yes = 'y', no = 'n', answer1;
            int addition = 0, subtraction = 0;
     
            if ('y' < 5) {
                System.out.println("Do you drink alcohol? Y/N : ");
                answer1 = Keyboard.readChar();
            }
            else if (answer1 == 'y') {
                addition = (addition + 1);
            }
            else if (answer1 == 'n') {
                subtraction = (subtraction + 1);
            }
        }
    }
    i keep getting an error like this
    error : variable answer1 might not have been initialized else if (answer1) == 'y')
    Aren't you getting other compiler messages too? I mean, where is "Keyboard" defined?

    Post the entire class file (including import statements) and post all compiler messages. Don't abbreviate. Don't paraphrase. Post all of the complete, exact messages.

    Cheers!

    Z

  3. #3
    Member
    Join Date
    Oct 2012
    Posts
    32
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Hey guys need abit of help please stressing like a mad thing

    im sorry i tried to copy the error messages but it would not let me but ill put up the keyboard.class do u know how to upload .class file i tried but it said unvalid file

  4. #4
    Member
    Join Date
    Oct 2012
    Posts
    32
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Hey guys need abit of help please stressing like a mad thing

    well here's the real program i have a couple of mistakes but i can sort it but not the error i keep getting
    class Survey{
    public static void main(String[ ] args)
    {

    char yes = 'y', no = 'n', answer1, answer2, answer3, answer4, answer5, answer6, answer7,answer8, answer9, answer10, answer11=0, answer12=0;
    int addition=0, subtraction=0;

    System.out.print("Do you like sports Y/N: ");
    answer1 = Keyboard.readChar( );
    if (answer1 == 'y')
    {
    addition=(addition + 1);
    }
    else if (answer1 =='n')
    {
    subtraction=(subtraction + 1);
    }

    System.out.print("Do you like ireland Y/N: ");
    answer2 = Keyboard.readChar( );
    if (answer2 == 'y')
    {
    addition=(addition + 1);
    }
    else if (answer2 =='n')
    {
    subtraction=(subtraction + 1);
    }
    System.out.print("Do you like movies Y/N: ");
    answer3 = Keyboard.readChar( );
    if (answer3 == 'y')
    {
    addition=(addition + 1);
    }
    else if (answer3 =='n')
    {
    subtraction=(subtraction + 1);
    }
    System.out.print("Do you like europe Y/N: ");
    answer4 = Keyboard.readChar( );
    if (answer4 == 'y')
    {
    addition=(addition + 1);
    }
    else if (answer4 =='n')
    {
    subtraction=(subtraction + 1);
    }
    System.out.print("Do you like america Y/N: ");
    answer5 = Keyboard.readChar( );
    if (answer5 == 'y')
    {
    addition=(addition + 1);
    }
    else if (answer5 =='n')
    {
    subtraction=(subtraction + 1);
    }
    System.out.print("Do you like Manchester United Y/N: ");
    answer6 = Keyboard.readChar( );
    if (answer6 == 'y')
    {
    addition=(addition + 1);
    }
    else if (answer6 =='n')
    {
    subtraction=(subtraction + 1);
    }
    System.out.print("Do you like Dublin Y/N: ");
    answer7 = Keyboard.readChar( );
    if (answer7 == 'y')
    {
    addition=(addition + 1);
    }
    else if (answer7 =='n')
    {
    subtraction=(subtraction + 1);
    }
    System.out.print("Do you like ITB Y/N: ");
    answer8 = Keyboard.readChar( );
    if (answer8 == 'y')
    {
    addition=(addition + 1);
    }
    else if (answer8 =='n')
    {
    subtraction=(subtraction + 1);
    }
    System.out.print("Do you like computers Y/N: ");
    answer9 = Keyboard.readChar( );
    if (answer9 == 'y')
    {
    addition=(addition + 1);
    }
    else if (answer9 =='n')
    {
    subtraction=(subtraction + 1);
    }
    System.out.print("Do you like programming Y/N: ");
    answer10 = Keyboard.readChar( );
    if (answer10 == 'y')
    {
    addition=(addition + 1);
    }
    else if (answer10 =='n')
    {
    subtraction=(subtraction + 1);
    }

    System.out.println("To Question 1o you like sports Y/N : You answered"+" "+ answer1);
    System.out.println("To Question 2o you like ireland Y/N: You answered"+" "+ answer2);
    System.out.println("To Question 3o you like movies Y/N: You answered"+" "+ answer3);
    System.out.println("To Question 4o you like europe Y/N: You answered"+" "+ answer4);
    System.out.println("To Question 5o you like america Y/N: You answered"+" "+ answer5);
    System.out.println("To Question 6o you like Manchester United Y/N:You answered"+" "+ answer6);
    System.out.println("To Question 7o you like Dublin Y/N:You answered"+" "+ answer7);
    System.out.println("To Question 8o you like ITB Y/N:You answered"+" "+ answer8);
    System.out.println("To Question 9o you like computers Y/N:You answered"+" "+ answer9);
    System.out.println("To Question 10o you like programming Y/N:You answered"+" "+ answer10);

    if ( 'y' < 5)
    {
    System.out.println("Thank you for answering the survey, to complete the survey could you answer these two questions ");
    }
    else if ('y' > 5)
    {
    System.out.println("Thank you for completing the survey ");
    }

    else if ('y' < 5)
    {
    System.out.println("Do you drink alcohol? Y/N : ");
    answer11 = Keyboard.readChar( );
    }
    else if (answer11 == 'y')
    {
    addition=(addition + 1);
    }
    else if (answer11 == 'n')
    {
    subtraction=(subtraction + 1);
    }

    System.out.println("Do you eat potatoes? Y/N : ");
    answer12 = Keyboard.readChar( );
    if (answer12 == 'y')
    {
    addition=(addition + 1);
    }
    else if (answer12 == 'n')
    {
    subtraction=(subtraction + 1);
    }

    System.out.println("To Question 11o you drink alcohol? Y/N : You answered"+" "+ answer11);
    System.out.println("To Question 12: Do you eat potatoes? Y/N : You answered"+" "+ answer12);

    if ( 'y' == 2)
    {
    System.out.print("You have a very agreeable person ");
    }
    else if ('n' == 2)
    {
    System.out.print("You like saying no don't you!");
    }
    }
    }

  5. #5
    Member
    Join Date
    Oct 2012
    Posts
    32
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Hey guys need abit of help please stressing like a mad thing

    Heres a zip file for the program and the class file
    Attached Files Attached Files

  6. #6
    Member
    Join Date
    Oct 2012
    Posts
    32
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Hey guys need abit of help please stressing like a mad thing

    In the program i am suppose to have 10 questions for the user to answer and if the user answer less than 5 yes then i got to ask them to answer two more questions

  7. #7
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Hey guys need abit of help please stressing like a mad thing

    Quote Originally Posted by shep View Post
    ... the error i keep getting...
    I hate to repeat myself, but
    Quote Originally Posted by Zaphod_b
    post all compiler messages. Don't abbreviate. Don't paraphrase. Post all of the complete, exact messages.
    The idea is not for us to debug the program but for us to try to help you see how to get to the bottom of things. The exact error messages, including line numbers are important for debugging.


    Cheers!

    Z
    Last edited by Zaphod_b; October 29th, 2012 at 12:02 PM.

  8. #8
    Member
    Join Date
    Oct 2012
    Posts
    32
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Hey guys need abit of help please stressing like a mad thing

    the error message seems to be that at line 155 System.out.println("To Question 11: Do you drink alcohol? Y/N : You answered"+" "+ answer11);

  9. #9
    Member
    Join Date
    Oct 2012
    Posts
    32
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Hey guys need abit of help please stressing like a mad thing

    Heres the program again

    class Survey2{
    public static void main(String[ ] args)
    {

    char yes = 'y', no = 'n', answer1, answer2, answer3, answer4, answer5, answer6, answer7,answer8, answer9, answer10, answer11, answer12;
    int addition=0, subtraction=0;

    System.out.print("Do you like sports Y/N: ");
    answer1 = Keyboard.readChar( );
    if (answer1 == 'y')
    {
    addition=(addition + 1);
    }
    else if (answer1 =='n')
    {
    subtraction=(subtraction + 1);
    }

    System.out.print("Do you like ireland Y/N: ");
    answer2 = Keyboard.readChar( );
    if (answer2 == 'y')
    {
    addition=(addition + 1);
    }
    else if (answer2 =='n')
    {
    subtraction=(subtraction + 1);
    }
    System.out.print("Do you like movies Y/N: ");
    answer3 = Keyboard.readChar( );
    if (answer3 == 'y')
    {
    addition=(addition + 1);
    }
    else if (answer3 =='n')
    {
    subtraction=(subtraction + 1);
    }
    System.out.print("Do you like europe Y/N: ");
    answer4 = Keyboard.readChar( );
    if (answer4 == 'y')
    {
    addition=(addition + 1);
    }
    else if (answer4 =='n')
    {
    subtraction=(subtraction + 1);
    }
    System.out.print("Do you like america Y/N: ");
    answer5 = Keyboard.readChar( );
    if (answer5 == 'y')
    {
    addition=(addition + 1);
    }
    else if (answer5 =='n')
    {
    subtraction=(subtraction + 1);
    }
    System.out.print("Do you like Manchester United Y/N: ");
    answer6 = Keyboard.readChar( );
    if (answer6 == 'y')
    {
    addition=(addition + 1);
    }
    else if (answer6 =='n')
    {
    subtraction=(subtraction + 1);
    }
    System.out.print("Do you like Dublin Y/N: ");
    answer7 = Keyboard.readChar( );
    if (answer7 == 'y')
    {
    addition=(addition + 1);
    }
    else if (answer7 =='n')
    {
    subtraction=(subtraction + 1);
    }
    System.out.print("Do you like ITB Y/N: ");
    answer8 = Keyboard.readChar( );
    if (answer8 == 'y')
    {
    addition=(addition + 1);
    }
    else if (answer8 =='n')
    {
    subtraction=(subtraction + 1);
    }
    System.out.print("Do you like computers Y/N: ");
    answer9 = Keyboard.readChar( );
    if (answer9 == 'y')
    {
    addition=(addition + 1);
    }
    else if (answer9 =='n')
    {
    subtraction=(subtraction + 1);
    }
    System.out.print("Do you like programming Y/N: ");
    answer10 = Keyboard.readChar( );
    if (answer10 == 'y')
    {
    addition=(addition + 1);
    }
    else if (answer10 =='n')
    {
    subtraction=(subtraction + 1);
    }

    System.out.println("To Question 1o you like sports Y/N : You answered"+" "+ answer1);
    System.out.println("To Question 2o you like ireland Y/N: You answered"+" "+ answer2);
    System.out.println("To Question 3o you like movies Y/N: You answered"+" "+ answer3);
    System.out.println("To Question 4o you like europe Y/N: You answered"+" "+ answer4);
    System.out.println("To Question 5o you like america Y/N: You answered"+" "+ answer5);
    System.out.println("To Question 6o you like Manchester United Y/N:You answered"+" "+ answer6);
    System.out.println("To Question 7o you like Dublin Y/N:You answered"+" "+ answer7);
    System.out.println("To Question 8o you like ITB Y/N:You answered"+" "+ answer8);
    System.out.println("To Question 9o you like computers Y/N:You answered"+" "+ answer9);
    System.out.println("To Question 10o you like programming Y/N:You answered"+" "+ answer10);

    if ( 'y' < 5)
    {
    System.out.println("Thank you for answering the survey, to complete the survey could you answer these two questions ");
    }
    else if ('y' > 5)
    {
    System.out.println("Thank you for completing the survey ");
    }

    else if ('y' < 5)
    {
    System.out.println("Do you drink alcohol? Y/N : ");
    answer11 = Keyboard.readChar( );
    }
    else if (answer11 == 'y')
    {
    addition=(addition + 1);
    }
    else if (answer11 == 'n')
    {
    subtraction=(subtraction + 1);
    }

    System.out.println("Do you eat potatoes? Y/N : ");
    answer12 = Keyboard.readChar( );
    if (answer12 == 'y')
    {
    addition=(addition + 1);
    }
    else if (answer12 == 'n')
    {
    subtraction=(subtraction + 1);
    }

    System.out.println("To Question 11o you drink alcohol? Y/N : You answered"+" "+ answer11);
    System.out.println("To Question 12: Do you eat potatoes? Y/N : You answered"+" "+ answer12);

    if ( 'y' == 2)
    {
    System.out.print("You have a very agreeable person ");
    }
    else if ('n' == 2)
    {
    System.out.print("You like saying no don't you!");
    }
    }
    }

  10. #10
    Member
    Join Date
    Oct 2012
    Posts
    32
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Hey guys need abit of help please stressing like a mad thing

    the error message is :variable answer11 might have not be initialized

  11. #11
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Hey guys need abit of help please stressing like a mad thing

    OK, here are the lines immediately before the error line: (I fixed the formatting a little.) Note that nowhere in your program before these lines is answer11 given a value.
            if ( 'y' < 5)
            {
                System.out.println("Thank you for answering the survey, to complete the survey could you answer these two questions  ");
            }
            else if ('y' > 5)
            {
                System.out.println("Thank you for completing the survey  ");
            }
     
            else if ('y' < 5)
            {
                System.out.println("Do you drink alcohol?  Y/N :      ");
                answer11 = Keyboard.readChar( );
            }
            else if (answer11 == 'y')
            {
                addition=(addition + 1);
            }
            else if (answer11 == 'n')
            {
                subtraction=(subtraction + 1);
            }
     
            System.out.println("Do you eat potatoes?   Y/N :       ");
            answer12 = Keyboard.readChar( );
            if  (answer12 == 'y')
            {
                addition=(addition + 1);
            }
            else if (answer12 == 'n')
            {
                subtraction=(subtraction + 1);
            }
     
            System.out.println("To Question 11:Do you drink alcohol?  Y/N :   You answered"+" "+ answer11);

    The compiler is telling you that answer11 might not have been initialized. Can you guarantee that answer11 is always given a value?

    Now, regardless of that error message, did you actually look at the code? I mean really look at it?

    What the heck are these statements supposed to be doing?
            if ( 'y' < 5)
            {
                System.out.println("Thank you for answering the survey, to complete the survey could you answer these two questions  ");
            }
            else if ('y' > 5)
            {
                System.out.println("Thank you for completing the survey  ");
            }
     
            else if ('y' < 5)
            {
                System.out.println("Do you drink alcohol?  Y/N :      ");
                answer11 = Keyboard.readChar( );
            }
    .
    .
    .

    The literal constant 'y' has some fixed value. (Probably greater than 5, but so what?) I mean, no matter what the value of 'y' is, how could answer11 be given a value? That's what the compiler is complaining about.

    The thing that the compiler didn't tell you is that the sequence of operations, depending on the value of 'y' is absolute nonsense.

    What logic are you intending to use here?

    Bottom line: You could get rid of that annoying error message by initializing answer11 to some particular character where it is declared, but analyzing why the error message occurs (that is, by Looking At The Code) might actually lead to some insight into fixing the program.

    Cheers!

    Z
    Last edited by Zaphod_b; October 29th, 2012 at 12:45 PM.

  12. #12
    Member
    Join Date
    Oct 2012
    Posts
    32
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Hey guys need abit of help please stressing like a mad thing

    i know what the problem is but does anyone know how to add all the answers up at the end and say "Thank you, you entered 7 Yes and 3 No"

  13. #13
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Hey guys need abit of help please stressing like a mad thing

    At the beginning of the program declare ints yesCounter and noCounter. Set them to zero initially.

    Then, every time you get user input that is 'y', increment yesCounter. Every time you get user input that is 'n', increment noCounter.

    Or some such thing.


    Cheers!

    Z

Similar Threads

  1. Hey guys!!
    By Toni8810 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 8th, 2012, 07:27 AM
  2. Hey guys! :D
    By goingforcoffee in forum Member Introductions
    Replies: 0
    Last Post: April 4th, 2012, 11:01 AM
  3. Hey guys
    By felixchon in forum Member Introductions
    Replies: 1
    Last Post: March 13th, 2011, 08:50 AM
  4. hey guys....
    By prince joseph in forum Member Introductions
    Replies: 2
    Last Post: March 27th, 2010, 11:15 PM