Advertisement
gurumutant

Java String Methods Demo dengan menu

Feb 12th, 2023
898
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.49 KB | Source Code | 0 0
  1. package javastringmethods_hendri;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5. import java.util.Scanner;
  6.  
  7. public class JavaStringMethods_Hendri {
  8.     static BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
  9.     static Scanner sc = new Scanner(System.in);
  10.    
  11.     public static void tampil_menu() {
  12.         int pil = 0;
  13.         System.out.println("\n=============================================");
  14.         System.out.println("Aplikasi Contoh Operasi dengan String Methods");
  15.         System.out.println("=============================================\n");
  16.         System.out.println("1. Demo charAt()");
  17.         System.out.println("2. Demo concat()");
  18.         System.out.println("3. Demo substring()");
  19.         System.out.println("...");
  20.         System.out.println("9. Keluar Aplikasi");
  21.         System.out.print("Pilih Menu (1-9): ");
  22.         try {
  23.             pil = Integer.valueOf(rd.readLine());
  24.         } catch (Exception e) {
  25.             System.out.println("Kesalahan input !");
  26.         }            
  27.         switch (pil) {
  28.             case 1: demo_charAt(); break;
  29.             case 2: demo_concat(); break;
  30.             case 3: demo_substring(); break;
  31.             case 9: System.exit(0);
  32.             default: System.err.println("Pilihan tidak tersedia !");
  33.         }
  34.     }
  35.    
  36.     public static void main(String[] args) {
  37.         while (true) {
  38.             tampil_menu();
  39.         }
  40.     }
  41.  
  42.     private static void demo_charAt() {
  43.         String str;
  44.         int index;
  45.         try {
  46.             System.out.print("Masukkan teks: ");
  47.             str = sc.nextLine();
  48.             System.out.print("Ketikkan index yang diinginkan: ");
  49.             index = sc.nextInt();
  50.             System.out.println("Index ke-"+index+" adalah "+str.charAt(index));
  51.         } catch (Exception e) {
  52.             System.out.println("Terjadi Exception: "+ e.getMessage());
  53.         }
  54.     }
  55.  
  56.     private static void demo_concat() {
  57.         String str1,str2;
  58.         try {
  59.             System.out.print("Masukkan teks 1: ");
  60.             str1 = sc.nextLine();
  61.             System.out.print("Masukkan teks 2: ");
  62.             str2 = sc.nextLine();
  63.             System.out.println("Hasil concat teks 1 dan teks 2: "+str1.concat(str2));
  64.         } catch (Exception e) {
  65.             System.out.println("Terjadi Exception: "+ e.getMessage());
  66.         }
  67.     }
  68.  
  69.     private static void demo_substring() {
  70.         System.out.println("Belum diimplementasikan.");
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement