0% found this document useful (0 votes)
23 views

Java Keys

The document contains code snippets about different Java concepts: 1) It shows how to use a Scanner to read input from the command line and store it as a String and integer. 2) It demonstrates how to use a BufferedReader to read input as an integer, double, and String from System.in. 3) It provides examples of widening and narrowing casting between int and double primitive data types. 4) It includes examples of using Scanner to read input into an array, calculating factorials using loops, and reversing a string by swapping characters.

Uploaded by

Anvi
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Java Keys

The document contains code snippets about different Java concepts: 1) It shows how to use a Scanner to read input from the command line and store it as a String and integer. 2) It demonstrates how to use a BufferedReader to read input as an integer, double, and String from System.in. 3) It provides examples of widening and narrowing casting between int and double primitive data types. 4) It includes examples of using Scanner to read input into an array, calculating factorials using loops, and reversing a string by swapping characters.

Uploaded by

Anvi
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

input scanner format

Scanner sc = new Scanner(System.in)


String myString = sc.next();
int myInt = sc.nextInt();
scanner.close();

System.out.println("myString is: " + myString);


System.out.println("myInt is: " + myInt);

input buffer format

import java.io.*;
import java.util.*;
import java.io.IOException;

public class Solution {

public static void main(String args[]) throws IOException {


BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

int i =Integer.parseInt(br.readLine());
double d =Double.parseDouble(br.readLine());
String s = br.readLine();

Widening Casting

int myInt = 9;
double myDouble = myInt;

Narrowing Casting
double myDouble = 9.78d;
int myInt = (int) myDouble;

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */


class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in)
int t;
t=input.nextInt();
while(t>0)
{
int ans;
ans=input.nextInt()-input.nextInt();
System.out.println(ans);
t--;
}
}
}

//creates an array in the memory of length 10


int[] array = new int[10];
System.out.println("Enter the elements of the array: ");
for(int i=0; i<n; i++)
{
//reading array elements from the user
array[i]=sc.nextInt();

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */


class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();

while( T!=0 ){
int n = sc.nextInt();
int a = 1;

for(int i = 1; i <= n; i++ ){


a = a*i;
}
System.out.println(a);
T--;
}
}
}

-------------------------

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */


class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
int N = sc.nextInt();
String S = sc.next();

int z = N/2;
int y = 0;
int n = 0;
while( y <= z){
for(int h = 0 ; h <= N ; y++){
char d = S.charAt(h);
S.charAt(h) = S.charAt(n+1);
S.charAt(n+1) = d;
System.out.print(S);
h = h+2;
}
}

/* for(int c = 0; c < N; c++){


int a = S.charAt(c);

int r = 122 -(a-97);


char s = (char)r;
System.out.print(s);
}*/
}
}

You might also like