+ Reply to Thread
Results 1 to 1 of 1

Thread: Java:Tutorial - User Input

  1. #1
    Co-Administrator John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John's Avatar
    Join Date
    Jul 2006
    Age
    21
    Posts
    5,883
    Blog Entries
    25

    Java:Tutorial - User Input

    Personally I’m a little bored with the basics; especially since they hardly do anything of use. This tutorial will attempt to show you how to prompt the user for input and then have your program perform a basic operation with that input. Before reading this tutorial, you should have a basic understanding of flow control and operators.

    The first thing I am going to do is create a new package called math. By naming conventions, all characters in package names are lowercase. Within the math package I am going to create a new class called CelsiusToKelvin. Again sticking with the naming conventions, the class name starts with an uppercase letter and each subsequent word is capitalized. We are going to create our constructor and our main method which instantiates our class. Note I included comments which are denoted by two forward slashes “//” everything to the right of those is considered a comment and is ignored by the compiler. At this point your code should look something like this, with out the comments:

    Code:
    package math;//package declaration
    public class CelsiusToKelvin {//class header
    
         public CelsiusToKelvin(){ //constructor header
         }
    
         public static void main (String[] args){//main
         new CelsiusToKelvin();//instantiation
         }
    }
    At this point, it is a good idea to step back and examine the properties in the domain of your application. Our goal is to create a program that prompts the user for input, takes his input stores it as a variable, performs a basic calculation and creates a new converted variable. We can assume we are going to need two variables, a Kelvin value and a Celsius variable. So not let’s declare them:
    Code:
    package math;//package declaration
    public class CelsiusToKelvin {//class header
         
         double celsiusValue = 0.0; //declares celsiusValue as a double variable
         double kelvinValue = 0.0; //declares kelvinValue as a double variable
        
         public CelsiusToKelvin(){ //constructor header
         }
    
         public static void main (String[] args){//main
         new CelsiusToKelvin();//instantiation
         }
    }
    Now we want to “get” data from a user. To do that we take advantage of a class available to us

    Code:
     String userInput = JOptionPane.showInputDialog("Enter a Celsius Value");
    At this point it’s a good idea to create a debug statement to make sure your program is actually receiving input from the user. So lets just print what they enter by adding the System.out.println after the input dialog.

    Code:
    package math;//package decleration
    
    import javax.swing.JOptionPane;
    
    public class CelsiusToKelvin {//class header
    
         double celsiusValue = 0.0; //declares celsiusValue as a double variable
         double kelvinValue = 0.0; //declares kelvinValue as a double variable
        
         public CelsiusToKelvin(){ //constructor header
              String userInput = JOptionPane.showInputDialog("Enter a Celsius Value");
              System.out.println(userInput);
         }
         
         public static void main (String[] args){//main
         new CelsiusToKelvin();//instantiation
         }
    }
    However, if you haven’t noticed we have a problem. When the user enters a value, it is returned as a string, and because Java is pickey, you cant perform mathematical operations on strings. So we now need to make use of another class available to us.
    We now add this line:

    Code:
     celsiusValue = Double.parseDouble(userInput);
    We are now going to program our equation and we are finished.

    Code:
    package math;//package decleration
    
    import javax.swing.JOptionPane;
    
    public class CelsiusToKelvin {//class header
    
         double celsiusValue = 0.0; //declares celsiusValue as a double variable
         double kelvinValue = 0.0; //declares kelvinValue as a double variable
    
         public CelsiusToKelvin(){ //constructor header
    
              String userInput = JOptionPane.showInputDialog("Enter a Celsius Value");
              //System.out.println(userInput);
              celsiusValue = Double.parseDouble(userInput);
              kelvinValue = celsiusValue - 273.0;
              System.out.println(kelvinValue);
         }
    
         public static void main (String[] args){//main
         new CelsiusToKelvin();//instantiation
         }
    }
    Last edited by John; 12-09-2006 at 08:30 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 4 users browsing this thread. (0 members and 4 guests)

     

Similar Threads

  1. [C++] Validating user input
    By Xochiquetzal in forum C and C++
    Replies: 2
    Last Post: 07-08-2007, 05:18 PM
  2. Please Help With A C Program!!
    By siren in forum C and C++
    Replies: 7
    Last Post: 04-17-2007, 08:45 AM
  3. Replies: 0
    Last Post: 01-11-2007, 02:10 PM
  4. Java:Tutorial - Make Your Button Work
    By John in forum Java Tutorials
    Replies: 0
    Last Post: 01-11-2007, 02:04 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts