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: validate input with do-while

  1. #1
    Junior Member
    Join Date
    May 2020
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default validate input with do-while

    Hello guys,

    Please, follow the new thread in the post with my new question. I modified the post, following the rules of the forum

    I will appreciate any help
    Last edited by chucho11028; May 9th, 2020 at 12:18 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    23,001
    Thanks
    53
    Thanked 2,445 Times in 2,396 Posts

    Default Re: check null values with double

    I get an error,
    Please copy the full text of the error message and paste it here. It has important info about the error.

    The code must handle all values returned by the showInputDialog method, including null.
    What should the program do if the user cancels the dialog?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2020
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default validate input with do-while

    Hello everyone

    I have edit my post because I have tried something else
    I have a main class where I have asking to introduce some values
    In teh second class I use this values to provide an answer
    So, in the main class maincoche.java I use JOptionPane to introduce an String value

    it should be si or SI or Si, etc (yes). This value is sent to a SETTER in a second class named coche.java, the setter name is config_seats(String asientos_cuero) which receive teh string value

    Here I add the value
    	mifurgoneta.config_seats(JOptionPane.showInputDialog("Tiene asientos de cuero"));
    	System.out.println(mifurgoneta.get_asientos());


    here is my setter

    public void config_seats(String asientos_cuero)
    	{
     
     
    			//do{
     
    				if (asientos_cuero.equalsIgnoreCase("si") ||  asientos_cuero.equalsIgnoreCase("s"))
    			{
    				this.asientos_cuero = true;
    			}
     
     
    			else
    			{
    				this.asientos_cuero = false;
    			}
     
    		//}while(!asientos_cuero.equalsIgnoreCase("si") ||  !asientos_cuero.equalsIgnoreCase("s"));
    	}


    So my question is:
    How can I use a do-while in order to repeat the code if I introduce a wrong value differnet to (si or SI or Si)?
    the condition IF only handle the case sensitive for si but I think I need do - while in case I introduce any other character like sdjdjsdh or 17261 or cancel.

    when I use the do-while the program doesn't print the value and keep runing without any action. It is like I can not validate something coming from anotehr class

    Here you have the complete code


    main class

    package poo;
     
     
    import javax.swing.*;
     
     
    public class maincoche {
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
     
    		//sintaxis   clase + name_oF_object + new constructor
    		coche micoche1 =  new coche();
     
    		micoche1.establece_color("Rojo");
     
     
    		//sintaxis   clase + name_oF_object + new constructor
    		furgoneta mifurgoneta = new furgoneta(7,1000);
     
     
    		mifurgoneta.establece_color(JOptionPane.showInputDialog("Cual es el color :"));
    		System.out.println(mifurgoneta.dime_color());
     
    		mifurgoneta.config_seats(JOptionPane.showInputDialog("Tiene asientos de cuero"));
    		System.out.println(mifurgoneta.get_asientos());
     
     
     
     
     
     
     
    		/*
    		coche micoche = new coche();
     
     
    		micoche.establece_color(JOptionPane.showInputDialog("Introduce color")); 
     
    		System.out.println(micoche.provide_generaldata());
     
     
    		System.out.println(micoche.dime_color());
     
     
     
     
     
     
     
    		micoche.config_seats(JOptionPane.showInputDialog("Tiene asientos de cuero?"));
     
    		System.out.println(micoche.get_asientos());
     
     
    		micoche.conf_climatizador(JOptionPane.showInputDialog("Tiene climatizador?"));
    		System.out.println(micoche.get_climatizador());
     
     
    		*/
     
     
     
     
     
     
     
    	}
     
    }


    second class
    package poo;
     
    public class coche {
     
    	//the key private encapsula las propiedades para que no sean modificadas desde otra calse
    	private int ruedas;
    	private int largo;
    	private int ancho;
    	private int motor;
    	private int peso_plataforma;
     
    	private String color1;
     
     
     
    	private String color;
    	private int peso_total;
    	boolean asientos_cuero, climatizador;
     
     
     
    	//constructor
    	public coche()
    	{
    		ruedas=4;
    		largo=2000;
    		ancho=300;
    		motor=1600;
    		peso_plataforma=500;
    	}
     
    	//metodo getter obtain value propertie  
    	//sintaxis es Public + tipodedato + nameofmethod() + usa return
    	public String  provide_generaldata()
    	{
     
     
    		return "El coche tiene: " + ruedas + " ruedas" + " Con un largo de " + largo/1000 + 
    				" metros " + " y peso de " + peso_plataforma + " kg";
     
    	}
     
    	//**************************************
    	//method setter
    	public void set_color()
    	{
    		color1= "rojo";
    	}
     
    	//method getter
    	public String provide_color()
    	{
    		return "El color is: " + color1;
    	}
    	//******************************8
     
    	//setters modifica valor propiedades
    	//sintaxis public+void+nombre_method no  usa return
     
    	public void establece_color(String color_coche)
    	{
    		color=color_coche;
     
    	}
     
    	//method getter
    	public String dime_color()
    	{
    		return "El color del coche es : " + color;
    	}
     
     
     
    	//setter
    	public void config_seats(String asientos_cuero)
    	{
     
     
    			//do{
     
    				if (asientos_cuero.equalsIgnoreCase("si") ||  asientos_cuero.equalsIgnoreCase("s"))
    			{
    				this.asientos_cuero = true;
    			}
     
     
    			else
    			{
    				this.asientos_cuero = false;
    			}
     
    		//}while(!asientos_cuero.equalsIgnoreCase("si") ||  !asientos_cuero.equalsIgnoreCase("s"));
    	}
     
    	//getter
    	public String get_asientos()
    	{
    		if(asientos_cuero==true) {
     
    			return "el coche tiene asientos de cuero";
    		}
    		else {
    			return "no tiene cuero";
    		}
    	}
     
     
    	public void conf_climatizador(String climatizador) {
     
    		if(climatizador.equalsIgnoreCase("si") || climatizador.equalsIgnoreCase("s"))
    		{
    			this.climatizador = true;
    		}
    		else
    		{
    			this.climatizador = false;
    		}
    	}
     
     
    	public String get_climatizador()
    	{
    		if(climatizador==true) {
     
    			return "el coche tiene climatizador";
    		}
    		else
    		{
    			return "no tiene climatizador";
    		}
    	}
     
    }
    Last edited by chucho11028; May 9th, 2020 at 12:16 PM. Reason: I got a new approach to find my solution

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    23,001
    Thanks
    53
    Thanked 2,445 Times in 2,396 Posts

    Default Re: check null values with double

    Did you try adding code to handle what is returned by the showInputDialog method?

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

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

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Testing double null
    By javaStooge in forum What's Wrong With My Code?
    Replies: 48
    Last Post: May 4th, 2014, 09:54 PM
  2. How to check if a session object exists (or is not null)?
    By BimmyJim in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 6th, 2014, 10:10 AM
  3. Insert != null check issues
    By JamEngulfer221 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 4th, 2012, 12:30 PM
  4. how to check is DOUBLE JTEXTFIELD is empty?
    By myexternall11 in forum Java Theory & Questions
    Replies: 2
    Last Post: February 22nd, 2012, 10:56 AM
  5. how to check null
    By veens in forum JDBC & Databases
    Replies: 7
    Last Post: November 16th, 2011, 06:54 PM