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

Thread: Need help with loop

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with loop

    It is a loop program that counts Yes votes with 'Y', no votes with "N" and to quit is "Q" and after quit is chosen it shows number of yes votes and number of no votes, all in a dialog box. Here it is, when I enter Y, y, N or n it crashes, but Q or q works

    import javax.swing.JOptionPane;

    public class program05 {

    public static void main(String[] args) {

    int yes = 0;

    int no = 0;

    String reply;

    char firstChar;

    int buttonPressed = 1;

    while (buttonPressed != 0) {

    reply = JOptionPane.showInputDialog(null,
    "Enter 'Y' to vote yes, 'N' to vote no, or 'Q' to quit.");

    firstChar = reply.charAt(0);

    if (firstChar == 'Y' || firstChar == 'y') {

    int yesVotes = Integer.parseInt (reply);

    yes += yesVotes;
    }

    else if (firstChar == 'N' || firstChar == 'n') {

    int noVotes = Integer.parseInt (reply);

    no += noVotes;

    }

    else if (firstChar == 'Q' || firstChar == 'q') {

    buttonPressed = JOptionPane.showConfirmDialog(null, "Really quit?");
    }
    }

    JOptionPane.showMessageDialog
    (null, "Total Yes votes is " + yes + " and total No votes is " + no);
    }
    }



    here is the exception message

    Exception in thread "main" java.lang.NumberFormatException: For input string: "y"
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:492)
    at java.lang.Integer.parseInt(Integer.java:527)
    at program05.main(program05.java:26)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 5 seconds)


  2. #2
    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 help with loop

    The exception message should tell you exactly what the problem is. Please post it in its entirety.

    Hint: think about exactly what you're trying to parse into an int.

    Also, please have a look at the forum FAQ at some point to learn about use of code tags so that your code will retain its formatting when posted in the forum allowing us to be able to read it better and understand it better.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with loop

    Here it is:
    Exception in thread "main" java.lang.NumberFormatException: For input string: "y"
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:492)
    at java.lang.Integer.parseInt(Integer.java:527)
    at program05.main(program05.java:26)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 5 seconds)

  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 help with loop

    Yep exactly, what's on line 26 of program05.java?

    this:
    int yesVotes = Integer.parseInt (reply);

    What's the error telling you?
    this:
    Exception in thread "main" java.lang.NumberFormatException: For input string: "y"
    And so the error message makes complete sense. The user is entering "y" for yes, and you're trying to parse that "y" as an integer, which makes no sense at all. The solution, learn from the error message, and don't do this, don't try to parse a non-numeric String as a number. Instead re-consider your logic.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with loop

    How do I make a non-numeric letter represent an integer value? It can only be "Y" "N" or "Q"

  6. #6
    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 help with loop

    Quote Originally Posted by JohnEliot View Post
    How do I make a non-numeric letter represent an integer value? It can only be "Y" "N" or "Q"
    You can't and even if you could, why would you want to? All you want to do is add 1 to a counter variable

Similar Threads

  1. For loop, the first command in the loop does not get executed the 2nd time..
    By lina_inverse in forum Loops & Control Statements
    Replies: 1
    Last Post: October 16th, 2012, 09:00 PM
  2. [SOLVED] Please help with my while loop that turned into infinite loop!
    By Hazmat210 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 10th, 2012, 11:22 PM
  3. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 08:57 PM
  4. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 12:05 PM
  5. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 05:27 PM

Tags for this Thread