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

Thread: I can't display a proper error message when i prompt the user enters a number less than 1, and request the user to re-enter a value

  1. #1
    Junior Member
    Join Date
    Sep 2021
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post I can't display a proper error message when i prompt the user enters a number less than 1, and request the user to re-enter a value

    package sumproductapp;

    import javax.swing.JOptionPane;

    /**
    *
    * @author SETH
    */
    public class SumProductApp {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    // declare variables
    final int SIZE = 5;
    int [] numbers = new int[SIZE];
    String strNum;
    int num,sum = 0, product = 1;

    //prompt the user to enter a number
    strNum = JOptionPane.showInputDialog(null, "Please enter number " , "Input", JOptionPane.PLAIN_MESSAGE);
    //convert the string number to a numeric value
    num = Integer.parseInt(strNum);

    //populate the array --> fill the array with numbers
    for(int i = 1; i < SIZE; i++){
    if(i==0){
    JOptionPane.showMessageDialog(null,"Invalid input data", "Wrong input", JOptionPane.ERROR_MESSAGE);

    JOptionPane.showInputDialog(null,"Please re-enter a number " ,"Input", JOptionPane.PLAIN_MESSAGE);
    //store the number at an index/position of the array
    numbers[i] = num;

    while(num>0){


    }



    }





    }

    //determine the sum of the elements
    for(int i = 1; i < SIZE; i++){
    //get a number at an index
    num = numbers[i];

    //determine sum
    sum = sum + num;
    }

    //determine the product
    for(int i = 1; i < SIZE; i++ ){
    //get a number at an index
    num = numbers[i];

    //determine the product
    product = product * num;
    }

    //display the sum and product
    JOptionPane.showMessageDialog(null, "The sum is " + sum + "\n" +
    "The product is " + product,
    "Outcome", JOptionPane.INFORMATION_MESSAGE);
    }

    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,878
    Thanks
    58
    Thanked 2,690 Times in 2,640 Posts

    Default Re: I can't display a proper error message when i prompt the user enters a number less than 1, and request the user to re-enter a value

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    Where is the test for a value less than 1? I do not see any tests of the user's input.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: February 24th, 2014, 04:35 PM
  2. [SOLVED] GUI user input error message
    By deeevo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 24th, 2013, 02:00 AM
  3. Java to HTML, enable user to enter a number of menu items
    By @passat in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 3rd, 2012, 08:42 AM
  4. How To Ask the User to Enter Another Number Using the Number 1
    By Pettsa in forum Object Oriented Programming
    Replies: 6
    Last Post: May 3rd, 2012, 03:44 AM
  5. How to display the results + repeat to the number entered by user?
    By TheBattousaixx in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 17th, 2011, 11:56 PM

Tags for this Thread