Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Change the user input to integer 
 

In this program we are going to calculate a area of a rectangle by using two classes named Rectangle and EnterValuesFromKeyboard.

 

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

                         

» View all related tutorials
Related Tags: c file array class list ui lists method get name using this oo root example where to exam drive store

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

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.

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

Current Comments

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

program using parseint() method shows error , Arrayindexoutof bound

Posted by manohar on Monday, 12.1.08 @ 23:23pm | #82263

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

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

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 | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

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

Copyright © 2008. All rights reserved.