2

I've been playing around with this code for a little bit now and can't seem to find the correct way to sort it out. I used a program without JOptionPane and it worked and tried to use the same sequence but it didn't work. Do I need to add something else? The assignment is to have the user enter 3 integers and print the average with input and output dialog boxes. I've done the average and input/output dialog boxes before but putting it all together is harder than I thought.

import java.util.Scanner;
import javax.swing.JOptionPane;
import java.text.*;
import java.text.DecimalFormat;
public class Number3
{

  public static void main(String[] args)
  {
    System.out.println("Enter 3 numbers: ");
    Scanner input = new Scanner (System.in);
    DecimalFormat decimalFormat = new DecimalFormat("0.00##");

    int num1;
    int num2;
    int num3;
    double avg;

    num1=input.nextInt();
    num2=input.nextInt();
    num3=input.nextInt();

    avg=(double)(num1+num2+num3)/3.0;
    System.out.println("The average is: " + decimalFormat.format(avg));

  }
}
Sam
  • 35
  • 1
  • 6
  • Possible duplicate of [*Simple popup java form with at least two fields*](https://stackoverflow.com/q/3002787/230513). – trashgod Jun 04 '17 at 03:08
  • I didn’t get it, sorry. Is your program working as it stands, and you want to use `JOptionPane` instead? Or was it the other way around? Never write “it didn’t work” in a Stack Overflow question. Instead tell us precisely how the program behaved differently from the expected. Quote any error message verbatim. – Ole V.V. Jun 04 '17 at 04:44

4 Answers4

2

I don't know what you find hard here. I think you are looking for this:

DecimalFormat decimalFormat = new DecimalFormat("0.00##");
int num1;
int num2;
int num3;
double avg;

num1= Integer.valueOf(JOptionPane.showInputDialog("Enter #1"));
num2= Integer.valueOf(JOptionPane.showInputDialog("Enter #2"));
num3= Integer.valueOf(JOptionPane.showInputDialog("Enter #3"));

avg=(double)(num1+num2+num3)/3.0;
JOptionPane.showMessageDialog(null,  "The average is: " + decimalFormat.format(avg));

Please note that this code could be written better but for the sake of answering I just replaced the JOptionPane in your code where you need them.

Uday
  • 1,165
  • 9
  • 12
1

It's really not that much harder. On the input side, using one of the showInputDialog(...) methods of JOptionPane is almost an exact replacement for input.nextInt();. The only difference it that showInputDialog(...) returns the user's input as String, not an int, so you'll have to use Integer.parseInt to convert the returned String into an int. As for the output, showMessageDialog(...) is an almost exact replacement for System.out.println(...); just use the --- as the message text argument.

Kevin Anderson
  • 4,568
  • 3
  • 13
  • 21
0
public static void main(String[] args) {
    int num, count=0;
    double total =0, avg;

    for(int i = 1; i <= 3; i++){
        num = Integer.valueOf(JOptionPane.showInputDialog("Enter number "+ count++));
        total += num;
    }

    avg = total / count;

    JOptionPane.showMessageDialog(null,  "The average is: " + (double)Math.round(avg * 100) / 100);
}
Blasanka
  • 21,001
  • 12
  • 102
  • 104
0
    /*
    *AverageOfThreeNumnber.java
    *calculating the Average Of Four Numnberand diaply the output
    *using JOptionpane method in java
    */
    import javax.swing.JOptionpane;
    public class AverageOfThreeNumbers {
    public static void main(String[] args) {
    
     int fristNumber; // FRIST INTEGER NUMBER
     int SecondNumber; // SECOND INTEGER NUMBER
     int ThridNumber; // THRID INTEGER NUMBER
     
     int sum; // SUM OF THE FOUR INTEGER NUMBERS
     double avarage; // AVERAGE OF THE FOUR NUMBERS

     String input; // INPUT VALUE
     String result; // OUTPUT GENERATING STRING

    // ACCEPT INTEGER NUMBERS FROM THE USER
  

    input = JOptionpane.showInputDialog(null, "Enter frist nmuber: ");
      FristNumber=Integer.parse.Int(Input);
    
      input = JOptionpane.showInputDialog(null, "Enter Second nmuber: ");
      SecondNumberr=Integer.parse.Int(Input);

  

      input = JOptionpane.showInputDialog(null, "Enter Thrid nmuber: ");
      ThridNumber=Integer.parse.Int(Input);
    
        
     
    //CALCULATE SUM
  sum = fristNumber + SecondNumber + ThridNumber;

    //CALCULATE AVERAGE
  average = sum/4.0

    //BUILD OUTPUT STRING AND DISPLAY OUTPUT
  result = "Average of" + fristNumber + ", " + SecondNumber + " And " + ThridNumber +" is = " + average;

    JOptionpane.showMessageDialog(null, result, "Average of 3 Numbers", JOptionpane.INFORMATION_MESSAGE);

    
 }
}
Chidera
  • 1
  • 2