0% found this document useful (0 votes)
3 views2 pages

Java Coding Template

The document provides a Java coding template that demonstrates input handling using the Scanner class, including reading strings, integers, and decimals. It also includes examples of StringBuffer methods for string manipulation and common string methods for operations like splitting and replacing substrings. Additionally, it shows how to check if user input is an integer and handle invalid input appropriately.

Uploaded by

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

Java Coding Template

The document provides a Java coding template that demonstrates input handling using the Scanner class, including reading strings, integers, and decimals. It also includes examples of StringBuffer methods for string manipulation and common string methods for operations like splitting and replacing substrings. Additionally, it shows how to check if user input is an integer and handle invalid input appropriately.

Uploaded by

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

Java Coding Round Template:

import java.util.*; // Import for Scanner (input handling) and other utility classes

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("Enter a string:");
String inputString = scanner.nextLine();

System.out.println("Enter an integer:");
int inputInt = scanner.nextInt();

System.out.println("Enter a decimal number:");


double inputDouble = scanner.nextDouble();

System.out.println("Enter two integers:");


int num1 = scanner.nextInt();
int num2 = scanner.nextInt();

int sum = num1 + num2;


double result = inputDouble * 2;

System.out.println("Sum of the two integers: " + sum);


System.out.println("Double of the decimal number: " + result);

scanner.close();
}
}

StringBuffer Methods:

StringBuffer sb = new StringBuffer("Hello");


sb.append(" World"); // "Hello World"
sb.insert(5, ","); // "Hello, World"
sb.replace(0, 5, "Hi"); // "Hi, World"
sb.delete(3, 5); // "Hi World"
sb.reverse(); // "dlroW iH"
String result = sb.toString();

Common String Methods:

String s = "hello,world,java";
String[] parts = s.split(","); // ["hello", "world", "java"]
String replaced = s.replace("java", "python");// "hello,world,python"
String sub = s.substring(0, 5); // "hello"
int len = s.length(); // 17
char c = s.charAt(1); // 'e'
int idx = s.indexOf("world"); // 6
boolean hasJava = s.contains("java"); // true
String upper = s.toUpperCase(); // "HELLO,WORLD,JAVA"
String lower = s.toLowerCase(); // "hello,world,java"
String t = " abc ".trim(); // "abc"

Check if Input is Integer:

Scanner scanner = new Scanner(System.in);


System.out.println("Enter something:");

if (scanner.hasNextInt()) {
int num = scanner.nextInt();
System.out.println("You entered integer: " + num);
} else {
String input = scanner.next(); // Consume the invalid input
System.out.println("Not an integer: " + input);
}

scanner.close();

You might also like