Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials
  

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Change the user input to integer

                         

Isn't it a very tedious job to enter the values each time before the compilation in the method itself. Now if we want to enter an integer value after the compilation of a program and force the JVM to ask for an input, then we should use Integer.parseInt(string str). 

As we know that JVM reads each data type in a String format. Now consider a case where we to enter a value in  int primitive type, then we have to use int x = Integer.parseInt("any String") , because the value we have entered is in integer but it will be interpreted as a String so, we have to change the string into integer. Here Integer is a name of a class in java.lang package and parseInt() is a method of Integer class which converts String to integer. If we want to enter a integer in a method or class using keyboard, then we have to use a method parseInt().

In this program we are going to calculate a area of a rectangle by using two classes named Rectangle and EnterValuesFromKeyboard. In the first class we have used two methods, show(int x, int y) and calculate(). First method show() is taking two variables as input and second method calculate() calculates the area of a rectangle. In the second class which is also our main class we declare a will declare our main method. Inside this method we will create a object of a Rectangle class. Now we ask the user to input two values and stored those values in the variables. These entered values will be changed into integer by using parseInt() method. Now these variables are passed in the method show() and the area will be calculated by calculate() method. These methods will be called by the instance of Rectangle class because it is the method of Rectangle class. 

Here is the code of the program

class Rectangle{
  int length, breadth;
  void show(int x, int y){
    length = x;
    breadth = y;
  }
  int calculate(){
    return(length * breadth);
  }
}
public class EnterValuesFromKeyboard{
  public static void main(String[] args) {
    Rectangle rectangle = new Rectangle();
    int a = Integer.parseInt(args[0]);
    int b = Integer.parseInt(args[1]);
    rectangle.show(a, b);
    System.out.println(" you have entered these values : " +  a  + " and " +  b);
    int area = rectangle.calculate();
    System.out.println(" area of a rectange is  : " + area);
  }
}

Output of the program is given below:

C:\java>java EnterValuesFromKeyboard 4 5
you have entered these values : 4 and 5
area of a rectange is : 20

Download this example

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

4 comments so far (post your own) View All Comments Latest 10 Comments:

yo, that is a great example. It helped me understand it better now

Posted by cuddling on Monday, 07.7.08 @ 19:31pm | #66166

hi, i really thank t the peoples who posted this example . its really helping

Posted by peter on Saturday, 05.31.08 @ 22:50pm | #61602

your tutorials are good and help me in one critical situation of my project...

Thank you..
ShantI
Bangalore

Posted by prasanthi on Tuesday, 09.11.07 @ 18:47pm | #26760

Your tutorials are extremely good and helpful.
Keep it Up!
Thanks and Regards

Kapil Tandon
Delhi

Posted by Kapil Tandon on Saturday, 12.23.06 @ 17:54pm | #1503

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.