Home Java Beginners Change the user input to integer
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Change the user input to integer

Advertisement
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

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 7, 2007

Ask Questions?    Discuss: Change the user input to integer   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Murugan.M
June 27, 2011
Error on that Example

Though the concepts were correct, When I tried that program it showed me the error D:\ecom\java\bin>javac EnterValuesFromKeyboard.java D:\ecom\java\bin>java EnterValuesFromKeyboard Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at EnterValuesFromKeyboard.main(EnterValuesFromKeyboard.java:14) how can you solve this ERROR?
neelam agrawal
July 4, 2011
ses of public,protected,local variable in java

pls provide all the information abt dis ....
Binnu Jesudasan Gudapati
July 29, 2011
Java Program-Progress Report

import java.io.*; public class Progress_Report { static public void main(String args[])throws IOException { int i,j,count=0; int marks[]=new int[7]; float avg; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); for(i=1;i<=3;i++)//No Of Students { int total=0; System.out.println("Enter Marks Of One Student "+i); for(j=1;j<=6;j++)//No Of Subjects { System.out.println("Enter The Marks Of Subject "+j); marks[j]=Integer.parseInt(br.readLine());//Here its denying input less than three digit total=total+marks[j]; while(marks[j]<35) { count=count+1;//No Of Failed Subjects } } avg=(total/6); System.out.println("Total Of Student "+i+" is "+total); System.out.println("Average Of Student "+i+" "+avg); System.out.println("No Of Subjects Failed "+count); } } } Thanks In Advance....Hope You Reply Me Fast....Mail Me : binnujesudasan@gmail.com
sumudu
April 18, 2012
parseInt().

thanx dear
haha
April 25, 2012
dodo@rocketmail.com

so nice bro.
chinnu
July 4, 2012
java

wen i executed dis program, i got an error tat is "exception in thread "main" java.lang.ArrayIndexOutOfBoundsException"... why it is showing error like dat ??? pls explain dis..
mari
September 12, 2012
Exception

Hi, I'm only a beginner at Java. Running the program as it is given in the example, I get an exception (here after). Can you explain me why please? Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at EnterValuesFromKeyboard.main(EnterValuesFromKeyboard.java:14)
Amrita
January 21, 2013
Change the user input to integer

This code is not working...I am getting Array index out of bounds exception
gaddaffi
December 2, 2013
in switch statement

how do we use integer.parseint in switch statement. boss?
DMCA.com