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.


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

Results 1 to 2 of 2

Thread: Using JOptionPane with if-else statements

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Thanked 0 Times in 0 Posts

    Question Using JOptionPane with if-else statements


    I have recently completed a homework assignment where I had to code for someone to enter a sentence and terms to search for in that sentence. The output then tells you if the term was found in the sentence, how many times it occurs and the percentage that term occurs in the sentence. I have it working just fine via the console (using Eclipse), but (mostly just for fun) I wanted to use a dialog box because I think it looks better and I want to use it more for future assignments. The input works fine using JOptionPane.showInputDialog but at the end I have an if-else statement and since that statement may produce multiple outputs, I'm not sure how to code the JOptionPane.showMessageDialog to show all the possible outputs from the if-else statement. I can get it to show a new dialog box for each if statement that is true but I would like to have them all in one box, each on a new line (word 'x' occurs 3 times 14%; word 'y' occurs once 8%; word 'z' does not occur 0%, There are 10 words in the sentence). I'm sure this is possible, just haven't been able to figure it out on my own. Any help would be great as I'd like to do this more in future assignments.

    Here is the code for the if-else statements currently using system.out.println():

    if (termCount >= 0) {
    double percentOccurs = termCount/coun *100;
    DecimalFormat percentTwoDecimals = new DecimalFormat("0.00");

    if ((termCount > 0) && (termCount < 2))
    System.out.println("\'" + termArray[i] + "\' occurs once " + percentTwoDecimals.format(percentOccurs) + "%");

    else if (termCount >= 2) {
    int newTermCount = (int) termCount;
    System.out.println("\'" + termArray[i] + "\' occurs " + newTermCount + " times " + percentTwoDecimals.format(percentOccurs) + "%"); }

    else if (termCount < 1)
    System.out.println("\'" + termArray[i] + "\' does not occur 0.00%");
    }System.out.println("There are " + sentenceArray.length + " words in the this sentence.");


  2. #2
    Join Date
    Jul 2012
    Thanked 19 Times in 19 Posts

    Default Re: Using JOptionPane with if-else statements

    you can read here JOptionPane
    and look at here JOptionPane (Java Platform SE 6)

Similar Threads

  1. how to use this variable ? (JOptionPane)
    By erdy_rezki in forum Java Theory & Questions
    Replies: 5
    Last Post: April 24th, 2012, 11:07 PM
  2. While JOptionPane equal Yes????
    By maximus20895 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 20th, 2010, 08:57 PM
  3. Display in JOptionPane
    By t-rank in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 19th, 2010, 12:09 AM
  4. Change to JOptionPane
    By Liuric in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 1st, 2010, 12:07 AM
  5. JOptionPane using If and Else
    By Liuric in forum Member Introductions
    Replies: 7
    Last Post: October 1st, 2010, 12:05 AM

Tags for this Thread