Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package javastringmethods_hendri;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.util.Scanner;
- public class JavaStringMethods_Hendri {
- static BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
- static Scanner sc = new Scanner(System.in);
- public static void tampil_menu() {
- int pil = 0;
- System.out.println("\n=============================================");
- System.out.println("Aplikasi Contoh Operasi dengan String Methods");
- System.out.println("=============================================\n");
- System.out.println("1. Demo charAt()");
- System.out.println("2. Demo concat()");
- System.out.println("3. Demo substring()");
- System.out.println("...");
- System.out.println("9. Keluar Aplikasi");
- System.out.print("Pilih Menu (1-9): ");
- try {
- pil = Integer.valueOf(rd.readLine());
- } catch (Exception e) {
- System.out.println("Kesalahan input !");
- }
- switch (pil) {
- case 1: demo_charAt(); break;
- case 2: demo_concat(); break;
- case 3: demo_substring(); break;
- case 9: System.exit(0);
- default: System.err.println("Pilihan tidak tersedia !");
- }
- }
- public static void main(String[] args) {
- while (true) {
- tampil_menu();
- }
- }
- private static void demo_charAt() {
- String str;
- int index;
- try {
- System.out.print("Masukkan teks: ");
- str = sc.nextLine();
- System.out.print("Ketikkan index yang diinginkan: ");
- index = sc.nextInt();
- System.out.println("Index ke-"+index+" adalah "+str.charAt(index));
- } catch (Exception e) {
- System.out.println("Terjadi Exception: "+ e.getMessage());
- }
- }
- private static void demo_concat() {
- String str1,str2;
- try {
- System.out.print("Masukkan teks 1: ");
- str1 = sc.nextLine();
- System.out.print("Masukkan teks 2: ");
- str2 = sc.nextLine();
- System.out.println("Hasil concat teks 1 dan teks 2: "+str1.concat(str2));
- } catch (Exception e) {
- System.out.println("Terjadi Exception: "+ e.getMessage());
- }
- }
- private static void demo_substring() {
- System.out.println("Belum diimplementasikan.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement