HarrJ

Day 09 string A

Feb 9th, 2024 (edited)
796
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.91 KB | None | 0 0
  1. package week1B;
  2.  
  3. public class Day09A {
  4.     public static void main(String[] args) {
  5.         String txtSource = "The quick brown fox jumps the lazy dog.";
  6.         int currentIndex = 0;
  7.        
  8.         System.out.println("current source: " + txtSource);
  9.         currentIndex = txtSource.toLowerCase().indexOf("the");
  10.         System.out.println("find index of \"the\": " + currentIndex);
  11. //        txtSource = txtSource.toLowerCase();
  12.         currentIndex = txtSource.toLowerCase().indexOf("the",currentIndex+1);
  13.         System.out.println("find index of \"the\": " + currentIndex);
  14.        
  15.         txtSource = "She sells sea shells by the sea shore. The shells she sells are surely sea shells. So if she sells shells on the sea shore, I'm sure she sells seashore shells.";
  16.        
  17.         System.out.println("current source: " + txtSource);
  18.         int sourceLen = txtSource.length();
  19.         currentIndex = 0;
  20.         int count = 0;
  21.         System.out.println("code below answers where are all the word sea in text");
  22.         while (count < sourceLen) {            
  23.             System.out.print("the word sea is on index ");
  24.             currentIndex = txtSource.toLowerCase().indexOf("sea", count);
  25.             System.out.println(currentIndex);
  26.             count = currentIndex + 1;
  27.             if (currentIndex == -1) {
  28.                 break;
  29.             }
  30.         }
  31.        
  32.         System.out.println("--------------------------");
  33.         txtSource = "Name,Team,Position,Height(inches),Weight(lbs),Age";
  34.         String[] sourceConv = txtSource.split(",");
  35.         for (String str : sourceConv) {
  36.             System.out.print(str + " | ");
  37.         }
  38.         System.out.println();
  39.         //scenario makulit ka
  40.         // di mo alam yung split
  41.         // kaya kaya ito gawin sa indexOf?
  42.         txtSource = "Adam Donachie,BAL,Catcher,74,180,22.99";
  43.         sourceConv = txtSource.split(",");
  44.         for (String str : sourceConv) {
  45.             System.out.print(str + " | ");
  46.         }
  47.         System.out.println();
  48.  
  49.         String[] teamRoster = {
  50.             "Name,Team,Position,Height(inches),Weight(lbs),Age"
  51.             ,"Adam Donachie,BAL,Catcher,74,180,22.99"
  52.             ,"Paul Bako,BAL,Catcher,74,215,34.69"
  53.             ,"Sean Tracey,CWS,Relief Pitcher,75,210,26.29"
  54.             ,"Nick Masset,CWS,Relief Pitcher,76,190,24.79"
  55.             ,"Jose Molina,ANA,Catcher,74,220,31.74"
  56.             ,"Jeff Mathis,ANA,Catcher,72,180,23.92"
  57.             ,"Coco Crisp,BOS,Outfielder,72,185,27.33"
  58.             ,"David Ortiz,BOS,Designated Hitter,76,230,31.28"
  59.             ,"Mark Kiger,OAK,Shortstop,71,180,26.75"
  60.             ,"Antonio Perez,OAK,Third Baseman,71,170,27.09"
  61.         };
  62.         for (String player : teamRoster) {
  63.             String[] pInfo = player.split(",");
  64.             for (String info : pInfo) {
  65.                 System.out.print(info + " | ");
  66.             }
  67.             System.out.println();
  68.         }
  69.     }
  70. }
  71.  
  72. //-----------------------
  73. package week1B;
  74.  
  75. public class Day09B {
  76.     public static void main(String[] args) {
  77.         String prodName = "Burger w/ Fries";
  78.         double price = 70;
  79.         String printout = String.format("%s", prodName);
  80.        
  81.         System.out.println(printout);
  82.        
  83.         prodName = "Spag";
  84.         price = 60;
  85.         printout = String.format("%s > %f", prodName, price);      
  86.         System.out.println(printout);
  87.        
  88.         prodName = "Coke Float";
  89.         price = 65;
  90.         printout = String.format("%s > P_%6.2f", prodName, price);      
  91.         System.out.println(printout);
  92.        
  93.         String pattern = "%-20s == P_%7.2f";
  94.         prodName = "Chicken Bucket 6pcs";
  95.         price = 305;
  96.         printout = String.format(pattern, prodName, price);      
  97.         System.out.println(printout);
  98.        
  99.         prodName = "Spag Platter";
  100.         price = 275;
  101.         System.out.printf(pattern,prodName, price);
  102.        
  103.     }
  104. }
  105.  
Advertisement
Add Comment
Please, Sign In to add comment