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

Thread: Adding Input Validation

  1. #1
    Junior Member
    Join Date
    Dec 2017
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Adding Input Validation

    How can I add input validation
    "invalid input, number has to be positive"
    dialog after
    an input of 0 or negative
    for length and width?

    {
     
    String source=ae.getActionCommand();
     
    switch (source) {
     
    case "Calculate the Area":
    System.out.println("Calc 1 Complete");
    area=Double.parseDouble(tLength.getText())
    *Double.parseDouble(tWidth.getText());
    break;

  2. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    258
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default Re: Adding Input Validation

    Hi, you can use if condition
    if(tLength.getText()<=0 && tWidth.getText()<=0)
    {
        System.out.println("invalid input, number has to be positive");
    }
    Whatever you are, be a good one

  3. The Following User Says Thank You to John Joe For This Useful Post:

    Snipe (December 6th, 2017)

  4. #3
    Junior Member
    Join Date
    Dec 2017
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Adding Input Validation

    Quote Originally Posted by John Joe View Post
    Hi, you can use if condition
    if(tLength.getText()<=0 && tWidth.getText()<=0)
    {
        System.out.println("invalid input, number has to be positive");
    }
    Thank you for responding. The problem I am having when I enter this is an error "Bad operand for binary operator '<='".

    I believe because tLength and tWidth are both String, is there a way to do this while both are String?

  5. #4
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    258
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default Re: Adding Input Validation

    Quote Originally Posted by Snipe View Post
    I believe because tLength and tWidth are both String, is there a way to do this while both are String?
    Yes, You need to convert to int
    if((Integer.parseInt(tLength.getText().toString())<=0) && ((Integer.parseInt(tWidth.getText().toString())<=0)))
    {
           System.out.println("invalid input, number has to be positive");
    }
    Whatever you are, be a good one

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

    Snipe (December 6th, 2017)

Similar Threads

  1. [SOLVED] Average Rainfall Main Class Using nested for loops,input validation - Average is off
    By CyberOps in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 24th, 2014, 04:36 AM
  2. Input Validation - Take no Symbols or Numbers
    By ProgrammablePeter in forum Java Theory & Questions
    Replies: 4
    Last Post: December 29th, 2013, 03:01 AM
  3. How to use a while loop to perform input validation?
    By namenamename in forum Java Theory & Questions
    Replies: 11
    Last Post: October 5th, 2013, 07:24 AM
  4. Replies: 1
    Last Post: August 9th, 2013, 10:26 AM
  5. Input Validation
    By nic in forum AWT / Java Swing
    Replies: 4
    Last Post: November 18th, 2009, 09:54 AM

Tags for this Thread