Tags » Inputstreamreader

write a program to reverse string in java

program to reverse string  :


package com.pkjavacode.com;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class ComparableExample {

/**
* @auther pradeep
*/
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.print("Enter String to reverse :");
String str = br.readLine();
String rev = "";
int n = str.length();
for (int i = n - 1; i >= 0; i--) {
rev = rev + str.charAt(i);

}
System.out.print("String after reverse are :" + rev);
}

catch (Exception e) {
System.out.println("" + e.getMessage());
}
}

}

… 11 more words
Java

print triangle shape

Print triangle :


package com.pkjavacode.com;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
*
* @author pradeep
*/
public class TriangleShape {

public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.println("Enter a number :");
int n = Integer.parseInt(br.readLine());
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n - i; j++) {
System.out.print(" ");
}
for (int j = 0; j <= i; j++) {
System.out.print("*");
}
for (int j = 0; j < i; j++) {
System.out.print("*");
}
System.out.println();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

Output :

Enter a number :
4
*
***
*****
*******
*********

Java

Array through user input in java

Array through user input : 

A user can enter an array length which you can see in below code.


package pkg;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
*
* @author pradeep
*/
public class Example {

public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.print("Enter lengh of array :");
int n = Integer.parseInt(br.readLine());
for (int i = 0; i <= n; i++) {
System.out.print(" " + i);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

… 6 more words
Java