Advertisement
HarrJ

Day 11 string class more

Nov 20th, 2023 (edited)
1,690
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.93 KB | None | 0 0
  1. public class Day11A {
  2.     public static void main(String[] args) {
  3.         // Day11A callMe = new Day11A();
  4.         char chABC = 't';
  5.         String txtRef = "The quick brown fox jumps over the lazy dog";
  6.         printLetterCount(chABC, txtRef);
  7.         for (char ch1 = 'a'; ch1 <= 'z';ch1++){
  8.             System.out.println(ch1);
  9.         }
  10.     }
  11.    
  12.     static void printLetterCount(char chFind, String txtSample) {
  13.         int letterCount = 0;
  14.         int nextIndex = 0;
  15.         int charCount = 0;
  16.         for (int i = 0; i < txtSample.length(); i++) {
  17.             nextIndex = txtSample.toLowerCase().indexOf(chFind,i);
  18.             i = nextIndex;
  19.             if (i == -1) {
  20.                 break;
  21.             }
  22.             charCount++;
  23.         }
  24.         System.out.println(chFind + " : " + charCount);
  25.     }
  26. }
  27.  
  28.  
  29. //---------------------------------
  30. public class Day11B {
  31.     public static void main(String[] args) {
  32.         String practiceText = "Username;Identifier;First name;Last name";
  33.        
  34.         String[] resultArray = practiceText.split(";");
  35.        
  36.         for (String string : resultArray) {
  37.             System.out.print(string + "   | ");
  38.         }
  39.         System.out.println();
  40.        
  41.         String[] csvSample = { "booker12;9012;Rachel;Booker"
  42.             ,"grey07;2070;Laura;Grey"
  43.             ,"johnson81;4081;Craig;Johnson"
  44.             ,"jenkins46;9346;Mary;Jenkins"
  45.             ,"smith79;5079;Jamie;Smith"};
  46.        
  47.         for (String csvString : csvSample) {
  48.             String[] csvToArray = csvString.split(";");
  49.             for (String string : csvToArray) {
  50.                 System.out.print(string + "   | ");
  51.             }
  52.             System.out.println();
  53.         }
  54.     }
  55.    
  56. }
  57.  
  58. //---------------------------------
  59.  
  60. public class Day11D {
  61.     public static void main(String[] args) {
  62.         String name = "Terry";
  63.         String hobby = "Plastic Model Making";
  64.         byte age = 12;
  65.         String resultMessage;
  66.         resultMessage = String.format("Good day I’m %s age %d", name, age);
  67.         System.out.println(resultMessage);
  68.         resultMessage = String.format("My hobbies are %s", hobby);
  69.         System.out.println(resultMessage);
  70.        
  71.         String txtCSV = "Username;Identifier;First name;Last name";
  72.         String[] csvArray = txtCSV.split(";");
  73.        
  74.         System.out.printf("%-12s | %-10s | %-12s | %-12s |%n"
  75.                 ,csvArray[0],csvArray[1],csvArray[2],csvArray[3]);
  76.        
  77.         txtCSV = "jenkins46;9346;Mary;Jenkins";
  78.         csvArray = txtCSV.split(";");
  79.        
  80.         System.out.printf("%-12s | %-10s | %-12s | %-12s |%n"
  81.                 ,csvArray[0],csvArray[1],csvArray[2],csvArray[3]);
  82.        
  83.         String[] csvSample = { "booker12;9012;Rachel;Booker"
  84.             ,"grey07;2070;Laura;Grey"
  85.             ,"johnson81;4081;Craig;Johnson"
  86.             ,"smith79;5079;Jamie;Smith"};
  87.     }
  88. }
  89. //---------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement