Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 31st, 2007, 09:18 AM
Authorized User
 
Join Date: Nov 2007
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to chris1012
Default if statements and null values

hey guys i know am a complete pain but am trying to acomplish something here that is a little tricky

basically am having trouble distinguishing how to work out this line of code my code is shown before what am trying to do is if the object lw function = a null value it goes to a joptionpane message box i've done it so far just without a null value for example when the joption message button cancel is clicked the lwfunction varaible is defined as so on ideally i would like the code to work with an ifstatement but i've tried that and it messes up and say incompatible types when the compiler is run.



    public void lite()
    {
    for (int liteweight=0; liteweight<3; liteweight++)
        {
    lwfunction = JOptionPane.showInputDialog(null, "What Weights the most in the?", "Heaviest Weight", JOptionPane.QUESTION_MESSAGE,null, new String[]{"Weight +5 years","Weight +3 years","Initial Weight"},"");

    if (lwfunction.equals("Weight +5 years"))
        {
        lwfive();
        }
        else if (lwfunction.equals("Weight +3 years"))
                {
                lwthree();
                }
                else if (lwfunction.equals("Initial Weight"))
                {
                    liw();
                }
        }
    Question();
    }

in opposite world i love programming
__________________
in opposite world i love programming
 
Old January 3rd, 2008, 09:10 AM
Friend of Wrox
 
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Remember that null is a primitive in java. Check for that using an ==

Code:
  // Nb I changed the function calls to print statements
  // to save having to rewrite your functions!
  public void lite(JFrame parent) {
    for (int liteweight=0; liteweight<3; liteweight++) { 
      // Create lwfunction object
      Object lwfunction = JOptionPane.showInputDialog(parent, 
          "What Weights the most inthe?", "Heaviest Weight",
          JOptionPane.QUESTION_MESSAGE,null, 
          new String[]{"Weight +5 years","Weight +3 years",
            "Initial Weight"},""
      );  

      // If the Object is null/we had cancel pressed. Null is
      // a primitive in java so we compare using == null 
      if (lwfunction == null)  { // if its null
        System.out.println("Got null/Cancel pressed");
        // return from our function
        return;
      }   

      // We now know our object is not null.
      if (lwfunction.equals("Weight +5 years")) {
        System.out.println("lwfive()"); 
      }   
      else if (lwfunction.equals("Weight +3 years")) {
        System.out.println("lwthree()"); 
      }   
      else if (lwfunction.equals("Initial Weight")) {
        System.out.println("liw()") ; 
      }   
    }   
    System.out.println("Question()"); 
  }
--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to handle null values imaginevictor ASP.NET 1.x and 2.0 Application Design 1 March 20th, 2007 01:52 AM
Null values jmcgranahan BOOK: Access 2003 VBA Programmer's Reference 0 August 29th, 2006 02:25 PM
Passing null values maddy137 ASP.NET 1.0 and 1.1 Basics 1 May 7th, 2006 12:26 PM
CheckBoxList and Null Values steve35719 ASP.NET 2.0 Basics 1 April 21st, 2006 09:42 AM
Handling Null Values nvenkat75 ADO.NET 2 January 16th, 2006 05:46 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.