0% found this document useful (0 votes)
28 views7 pages

Sample Programs Using Scanner Class

Uploaded by

Harshit Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views7 pages

Sample Programs Using Scanner Class

Uploaded by

Harshit Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Sample programs using Scanner Class, BufferedReader

class and Command Line Argument.

Example 1: Read a Line of Text Using Scanner

import java.util.Scanner;

class Main {
public static void main(String[] args) {

// Creates an object of Scanner


Scanner input = new Scanner(System.in);

System.out.print("Enter your name: ");

// Takes input from the keyboard


String name = input.nextLine();

// Prints name
System.out.println("My name is " + name);

// Closes the scanner


input.close();
}
}

Output

Enter your name: Jack


My name is Jack

Example 2: Java Scanner nextInt()

import java.util.Scanner;

class Main {
public static void main(String[] args) {
// creating a Scanner object
Scanner input = new Scanner(System.in);

System.out.println("Enter an integer: ");

// read an int value


int data1 = input.nextInt();

System.out.println("Using nextInt(): " + data1);

input.close();
}
}

Output

Enter an integer:
22
Using nextInt(): 22

Example 2: Java Scanner nextInt()

import java.util.Scanner;

class Main {
public static void main(String[] args) {

// creating a Scanner object


Scanner input = new Scanner(System.in);

System.out.println("Enter an integer: ");

// read an int value


int data1 = input.nextInt();

System.out.println("Using nextInt(): " + data1);

input.close();
}
}

Output

Enter an integer:
22
Using nextInt(): 22

1. class FibonacciExample1{  
2. public static void main(String args[])  
3. {    
4.  int n1=0,n2=1,n3,i,count=10;    
5.  System.out.print(n1+" "+n2);//printing 0 and 1    
6.     
7.  for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already printed    
8.  {    
9.   n3=n1+n2;    
10.   System.out.print(" "+n3);    
11.   n1=n2;    
12.   n2=n3;    
13.  }    
14.   
15. }}  

Output:
0 1 1 2 3 5 8 13 21 34

Program to copy all elements of one array into another


array
1. public class CopyArray {    
2.     public static void main(String[] args) {        
3.              //Initialize array     
4.         int [] arr1 = new int [] {1, 2, 3, 4, 5};     
5.          //Create another array arr2 with size of arr1    
6.         int arr2[] = new int[arr1.length];    
7.         //Copying all elements of one array into another    
8.         for (int i = 0; i < arr1.length; i++) {     
9.             arr2[i] = arr1[i];     
10.         }      
11.          //Displaying elements of array arr1     
12.         System.out.println("Elements of original array: ");    
13.         for (int i = 0; i < arr1.length; i++) {     
14.            System.out.print(arr1[i] + " ");    
15.         }     
16.             
17.         System.out.println();    
18.             
19.         //Displaying elements of array arr2     
20.         System.out.println("Elements of new array: ");    
21.         for (int i = 0; i < arr2.length; i++) {     
22.            System.out.print(arr2[i] + " ");    
23.         }     
24.     }    
25. }    
Output:
Elements of original array
1 2 3 4 5
Elements of new array:
1 2 3 4 5

Reading data from console by InputStreamReader and


BufferedReader
1. package com.javatpoint;  
2. import java.io.*;  
3. public class BufferedReaderExample{    
4. public static void main(String args[])throws Exception{             
5.     InputStreamReader r=new InputStreamReader(System.in);    
6.     BufferedReader br=new BufferedReader(r);            
7.     System.out.println("Enter your name");    
8.     String name=br.readLine();    
9.     System.out.println("Welcome "+name);    
10. }    
11. }  
Output:
Enter your name
Nakul Jain
Welcome Nakul Jain
Example of CharArrayReader Class:
1. import java.io.CharArrayReader;  
2. public class CharArrayExample{  
3.   public static void main(String[] ag) throws Exception {  
4.     char[] ary = { 'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't' };  
5.     CharArrayReader reader = new CharArrayReader(ary);  
6.     int k = 0;  
7.     // Read until the end of a file  
8.     while ((k = reader.read()) != -1) {  
9.       char ch = (char) k;  
10.       System.out.print(ch + " : ");  
11.       System.out.println(k);  
12.     }  
13.   }  
14. }  
Output
j : 106
a : 97
v : 118
a : 97
t : 116
p : 112
o : 111
i : 105
n : 110
t : 116
Sum of two numbers using command-line arguments
1
class Example2{
2 public static void main(String[] args){

3 int a = Integer.parseInt(args[0]);

4 int b = Integer.parseInt(args[1]);

5 int sum = a + b;

6 System.out.println("The sum is" +sum);


}
7
}
8

Output:
Fibonacci Series program using command-line arguments
1
class Example3{
2
public static void main(String[] args){
3
int n = Integer.parseInt(args[0]);
4 int t1 = 0;
5 int t2 = 1;
6  
7 for(int i = 1; i <=n; i++){

8 System.out.println(t1);

9 int sum = t1+t2;

t1 = t2;
10
t2 = sum;
11
}
12
}
13
}
14
Output:

You might also like