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 7 of 7

Thread: Need advice

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Need advice

    Hi,
    total newbie here. Started learning programming only 2 weeks ago. I need help with assignment. I need to create voting programme which will ask 20 students to pick their 1 favorite soccer team from 5 options. I set my for loop to run 20 times. Problem is if students type in invalid vote, it counts it, so at the end i'm getting only 19 valid votes. What i'm looking for is basically if student types in invalid vote(not 1 to 5) to give him option to vote again. Hopefully you guys will understand what i'm trying to do. This is what i got so far.
    int RealMadrid = 0;
    int Valencia = 0;
    int Chelsea = 0;
    int Barcelona  = 0;
    int ManchesterCity = 0;
     
     
    for (int i = 0; i<20; i++) { // starts loop
     
    String vote = JOptionPane.showInputDialog(null, " Please vote for your favorite soccer team "
            + "\n 1. Real Madrid"
            + "\n 2. Valencia"
            + "\n 3. Chelsea"
            + "\n 4. Barcelona"
            + "\n 5. Manchester City", "Team input", JOptionPane.QUESTION_MESSAGE);
     
    if (vote.equals("1"))
    {
    RealMadrid++;
    JOptionPane.showMessageDialog(null,"Real Madrid", "Student vote", JOptionPane.INFORMATION_MESSAGE);
    }
    else if (vote.equals("2"))
    { Valencia++;
        JOptionPane.showMessageDialog(null,"Valencia", "Student vote", JOptionPane.INFORMATION_MESSAGE );
    }
    else if (vote.equals("3"))
    {  Chelsea++;
       JOptionPane.showMessageDialog(null, "Chelsea", "Student vote", JOptionPane.INFORMATION_MESSAGE);
    }
    else if (vote.equals("4"))
    {  Barcelona++;
       JOptionPane.showMessageDialog(null, "Barcelona", "Student vote", JOptionPane.INFORMATION_MESSAGE );
    }
    else if (vote.equals("5"))
    {  ManchesterCity++;
       JOptionPane.showMessageDialog(null, "Manchester City", "Student vote", JOptionPane.INFORMATION_MESSAGE);
    }
     
    else 
    {JOptionPane.showMessageDialog(null, "Invalid vote", "Warning", JOptionPane.WARNING_MESSAGE);}
     
     
    }// end of loop
     
    JOptionPane.showMessageDialog(null," The number of students who like Real Madrid is " + RealMadrid, "Team results", JOptionPane.INFORMATION_MESSAGE);
    JOptionPane.showMessageDialog(null," The number of students who like Valencia is " + Valencia, "Team results", JOptionPane.INFORMATION_MESSAGE);
    JOptionPane.showMessageDialog(null," The number of students who like Chelsea is " + Chelsea, "Team results", JOptionPane.INFORMATION_MESSAGE);
    JOptionPane.showMessageDialog(null," The number of students who like Barcelona is " + Barcelona, "Team results", JOptionPane.INFORMATION_MESSAGE);
    JOptionPane.showMessageDialog(null," The number of students who like Manchester City is " + ManchesterCity, "Team results", JOptionPane.INFORMATION_MESSAGE);
     
     
     
     
    if (RealMadrid > 10){
    JOptionPane.showMessageDialog(null,"Students love Real Madrid ", "Team results", JOptionPane.INFORMATION_MESSAGE);
    }
    else {
    JOptionPane.showMessageDialog(null,"Students don't like Real Madrid ", "Team results", JOptionPane.INFORMATION_MESSAGE);
    }
     
     
     
     
    if (Valencia > 10) {
    JOptionPane.showMessageDialog(null,"Students love Valencia ", "Team results", JOptionPane.INFORMATION_MESSAGE);
    }
    else {
    JOptionPane.showMessageDialog(null,"Students don't like Valencia ", "Team results", JOptionPane.INFORMATION_MESSAGE);
    }
     
     
     
     
    if (Chelsea > 10) {
    JOptionPane.showMessageDialog(null,"Students love Chelsea ", "Team results", JOptionPane.INFORMATION_MESSAGE);
    }
    else {
    JOptionPane.showMessageDialog(null,"Students don't like Chelsea ", "Team results", JOptionPane.INFORMATION_MESSAGE);
    }
     
     
     
     
    if (Barcelona > 10) {
    JOptionPane.showMessageDialog(null,"Students love Barcelona ", "Team results", JOptionPane.INFORMATION_MESSAGE);
    }
    else {
    JOptionPane.showMessageDialog(null,"Students don't like Barcelona ", "Team results", JOptionPane.INFORMATION_MESSAGE);
    }
     
     
     
     
    if (ManchesterCity > 10) {
    JOptionPane.showMessageDialog(null,"Students love Manchester City ", "Team results", JOptionPane.INFORMATION_MESSAGE);
    }
    else {
    JOptionPane.showMessageDialog(null,"Students don't like Manchester City ", "Team results", JOptionPane.INFORMATION_MESSAGE);
    } * *


    Please do not give me the solutions, just advice. I want to be able to do it myself.
    Thanks very much.
    If i broke any forum rules then i'm really sorry.
    Last edited by kave2; October 5th, 2012 at 08:06 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need advice

    I'm not allowed to use while and do while Statements

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Need advice

    Then you may be hosed.

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

    Default Re: Need advice

    When it reaches the end of the loop, it increments the loop counter and goes back to the top to see whether to terminate the loop, right? Every time.

    However...

    Say you get an invalid response. What can you do about it? How about decrement the loop counter? And then continue to the end of the loop?


    Or some such thing...

    Cheers!

    Z
    Last edited by Zaphod_b; October 5th, 2012 at 10:33 PM.

  6. The Following User Says Thank You to Zaphod_b For This Useful Post:

    kave2 (October 12th, 2012)

  7. #6
    Junior Member
    Join Date
    Oct 2012
    Posts
    2
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Need advice

    Reading Zaphod_b solution gave me an idea.
    Make an infinite loop because you cannot know the number of times it will be an invalid vote.
    Have an index which you will increment each time there is a valid vote.
    Whan index reaches = 20, break the loop.
    I hope this will work.

  8. #7
    Junior Member
    Join Date
    Oct 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need advice

    Hi guys, it took me long time to come back to say thank you. I have been quite busy. Studying and full time job is quite difficult :-) Special thanks to Zaphod_b.

    To Alexdan: I could use for loop only and no break statement :-)
    Last edited by kave2; October 12th, 2012 at 07:49 PM.

Similar Threads

  1. Need some serious advice
    By Rituparna in forum The Cafe
    Replies: 4
    Last Post: September 25th, 2013, 08:26 AM
  2. Hello, need some advice.
    By mango7777 in forum Member Introductions
    Replies: 1
    Last Post: June 15th, 2012, 08:10 AM
  3. NEED ADVICE AND HELP about JPA
    By addrian in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 24th, 2012, 02:05 AM
  4. A little advice please:
    By SDKC in forum Java Theory & Questions
    Replies: 1
    Last Post: December 8th, 2010, 08:15 PM
  5. i need some advice ....
    By mdstrauss in forum Java Theory & Questions
    Replies: 8
    Last Post: July 24th, 2009, 02:29 PM