+ Reply to Thread
Results 1 to 4 of 4

Thread: Java:Tutorial - User Input

  1. #1
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    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; 07-18-2010 at 02:40 PM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Ananta2010 is offline Newbie
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Re: Java:Tutorial - User Input

    hey...i copied this program and ran it on jdk1.4......it compiled correctly but did not run....came up with a long error message sayin wrong class name...i double checked it...no use....pls. help

  4. #3
    Join Date
    Sep 2009
    Location
    USA
    Posts
    3,400
    Blog Entries
    5
    Rep Power
    37

    Re: Java:Tutorial - User Input

    Quote Originally Posted by Ananta2010 View Post
    hey...i copied this program and ran it on jdk1.4......it compiled correctly but did not run....came up with a long error message sayin wrong class name...i double checked it...no use....pls. help
    I just removed the package math; part and it worked XD

    Also +rep to John for a simple tutorial.
    Root Beer == System Administrator's Beer
    Download the new operating system programming kit! (some assembly required)

  5. #4
    Liam Flynn is offline Newbie
    Join Date
    Aug 2010
    Posts
    1
    Rep Power
    0

    Re: Java:Tutorial - User Input

    Just something that bugged me but the kelvin value should have been celciusValue + 273 instead of -273. Otherwise spot on for getting me back to scratch for uni, cheers

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Getting input from user
    By restin84 in forum Linux Programming and Scripting
    Replies: 3
    Last Post: 10-04-2011, 05:35 AM
  2. NASM User input help 2
    By untitled_1 in forum Assembly
    Replies: 3
    Last Post: 11-07-2010, 12:46 AM
  3. NASM User input help
    By untitled_1 in forum Assembly
    Replies: 2
    Last Post: 11-02-2010, 02:30 PM
  4. user input
    By Ananta2010 in forum Java Help
    Replies: 4
    Last Post: 06-16-2010, 08:03 AM
  5. [C++] Validating user input
    By Xochiquetzal in forum C and C++
    Replies: 2
    Last Post: 07-08-2007, 03:18 PM

Tags for this Thread

Bookmarks

Posting Permissions

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