Lost Password?

  #1 (permalink)  
Old 12-09-2006, 07:25 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 19
Posts: 2,746
Last Blog:
Passwords
Rep Power: 20
John has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud of
Send a message via AIM to John
Default 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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[C++] Validating user input Xochiquetzal C and C++ 2 07-08-2007 05:18 PM
Please Help With A C Program!! siren C and C++ 7 04-17-2007 08:45 AM
Java:Tutorial - Making multiple objects work differently John Java Tutorials 0 01-11-2007 02:10 PM
Java:Tutorial - Make Your Button Work John Java Tutorials 0 01-11-2007 02:04 PM


All times are GMT -5. The time now is 03:46 AM.

Contest Stats

GoogleKeyw ........ 20.00000

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 67%

Ads