Pageviews

Sunday 18 August 2013

How to get user Input in Java

by Unknown  |  in Source Codes at  Sunday, August 18, 2013

We can get the user specified input in Java using the Scanner class.For using the Scanner class in our program,we need to import the following header file:

import java.util.Scanner;

Here in the below program we are using Scanner class for addition of numbers and display the result.For example if you enter number 4 then result would be 4+3+2+1=10.

How to get user Input in Java:

import java.util.Scanner;                                 // import statement
public class Addall 
{
 public static void main(String args[])  
{      
int s=0;     
System.out.println("Enter  number ");      
Scanner q = new Scanner(System.in);          //q= Scanner object
int a = q.nextInt();
for(int i=1;i<=a;i++)
{
s=s+i;
}
System.out.println("Sum is"+s);   
}
}

User input



2 comments:

  1. USERINPUT
    package oop0528;

    import java.util.Scanner;

    public class User_Input {
    Scanner _reader;

    public User_Input() {
    this._reader = new Scanner(System.in);
    }

    public void Start( HandleShape hs ) {
    String sCommand ;
    sCommand = this._reader.nextLine() ;
    while ( Continue( sCommand ) ) {
    if ( !HandleCommand( sCommand, hs ) ) System.out.println("invalid command!!");
    } // while
    } // Start()

    public void End() {

    } // End

    private boolean Continue( String sCommand ) {
    sCommand = sCommand.toLowerCase() ;
    if ( sCommand.equals("quit") ) return false ;
    else return true ;
    } // Continue()

    private boolean HandleCommand( String sCommand, HandleShape hs ) {
    if ( sCommand.equals("circle") ) {
    hs.Add("circle");
    return true ;
    } // if
    else if ( sCommand.equals("square") ) {
    hs.Add("square");
    return true ;
    } // if


    return false;

    } // HandleCommand()

    }


    SQUARE
    /*
    * To change this license header, choose License Headers in Project Properties.
    * To change this template file, choose Tools | Templates
    * and open the template in the editor.
    */

    package oop0528;

    /**
    *
    * @author user
    */
    public class Square extends Shape {
    private double _dHeight ;
    private double _dLength ;

    @Override
    double GetArea() {
    return this._dHeight * this._dLength ;
    }

    @Override
    double GetPerimeter() {
    return ( this._dHeight + this._dLength ) * 2 ;
    }

    public double GetHeight() {
    return this._dHeight ;
    }

    public double GetLength() {
    return this._dLength ;
    }

    }

    CIRCLE
    /*
    * To change this license header, choose License Headers in Project Properties.
    * To change this template file, choose Tools | Templates
    * and open the template in the editor.
    */

    package oop0528;

    /**
    *
    * @author user
    */
    public class Circle extends Shape {
    private double _dRadius ;

    @Override
    double GetArea() {
    return this._dRadius * this._dRadius * Math.PI ;
    }

    @Override
    double GetPerimeter() {
    return this._dRadius * 2 * Math.PI ;
    }

    public double GetRadius() {
    return this._dRadius ;
    }

    }

    SHAPE
    /*
    * To change this license header, choose License Headers in Project Properties.
    * To change this template file, choose Tools | Templates
    * and open the template in the editor.
    */

    package oop0528;

    /**
    *
    * @author user
    */
    public abstract class Shape {

    abstract double GetArea() ;
    abstract double GetPerimeter() ;

    }

    /*
    * To change this license header, choose License Headers in Project Properties.
    * To change this template file, choose Tools | Templates
    * and open the template in the editor.
    */

    HANDLESHAPE

    package oop0528;

    import java.util.ArrayList;

    /**
    *
    * @author user
    */
    public class HandleShape {
    private ArrayList _allShape ;

    public HandleShape() {
    this._allShape = new ArrayList() ;
    }

    public boolean Add( String type ) {
    return false;

    } // Add()

    public double TotalArea() {
    double total = 0 ;
    for ( Shape s : this._allShape ) {
    total += s.GetArea() ;
    } // for

    return total ;
    } // TotalArea()

    }

    MAIN

    /*
    * To change this license header, choose License Headers in Project Properties.
    * To change this template file, choose Tools | Templates
    * and open the template in the editor.
    */

    package oop0528;

    /**
    *
    * @author user
    */
    public class Oop0528 {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    // TODO code application logic here

    User_Input ui = new User_Input() ;
    HandleShape hs = new HandleShape() ;
    ui.Start( hs );
    }

    }

    ReplyDelete
  2. Plz write code for 1 program,you had written a bunch of code,write the code of the program for which you want to get user input

    ReplyDelete

Proudly Powered by Blogger.