SlideShare ist ein Scribd-Unternehmen logo
1 von 6
Downloaden Sie, um offline zu lesen
ANSWER 05-06                                                      Computer Programming using Java             1   2     Computer Programming using Java                                                           ANSWER 05-06


     CHAPTER                                   การดําเนินการกับสตริง                                                  โจทย์ข้อที 4 [ระดับง่ าย]
 ANS-05
 ANS-                                            (String Operations)                                                   import java.util.Scanner;
                                                                                                                       public class InputWords {
                                                                                                                         public static void main(String[] args) {
                                                                                                                           Scanner kb = new Scanner(System.in);
โจทย์ข้อที 1 [ระดับง่ าย]                                                                                                  String text = "";
 ข้ อ ประเภทตัวแปร ค่ าทีเก็บในตัวแปร                     ข้ อ ประเภทตัวแปร ค่ าทีเก็บในตัวแปร                             while (true) {
                                                                                                                             System.out.print("Enter Word: ");
  1. int                  6                               11. int           -1                                               String w = kb.next();
                                                                                                                             if (w.toLowerCase().equals("stop")) break;
  2. int                  5                               12. int           7
                                                                                                                             text += w + " "; /* text = text + w + " "; */
  3. int                  1                               13. String        JavaChula                                      }
                                                                                                                           System.out.println(text);
  4. int                  3                               14. String        I Lo                                         }
                                                                                                                       }
  5. String               CHULA chula                     15. String        love java
  6.  char                v                               16.  boolean      false
  7. int                  4                               17. boolean       true
                                                                                                                      โจทย์ข้อที 5 [ระดับปานกลาง]
                                                                                                                       import java.util.Scanner;
  8. int                  9                               18. boolean       true                                       public class RemoveSpaceFromSentence {
  9. int                  8                               19. int           -20                                          public static void main(String[] args) {
                                                                                                                           Scanner kb = new Scanner(System.in);
 10. int                  8                               20. [Error]       [Error]                                        System.out.print("Enter Sentence: ");
                                                                                                                           String s = kb.nextLine();
                                                                                                                           String sn = "";
                                                                                                                           for (int i = 0; i < s.length(); i++) {
                                                                                                                             String t = s.substring(i, i + 1);
โจทย์ข้อที 2 [ระดับง่ าย]                                                                                                    if (!t.equals(" ")) {
                                                                                                                               sn = sn + t;
 ข้ อ ประเภทตัวแปร ค่ าทีเก็บในตัวแปร                     ข้ อ ประเภทตัวแปร ค่ าทีเก็บในตัวแปร                               }
  1. int                  2000                             6. [Error]       [Error]                                        }
                                                                                                                           System.out.println(sn);
  2. [Error]              [Error]                          7. String        2000.0                                       }
                                                                                                                       }
  3. double               2000.0                           8. String        1000.0
  4. double               1000.0                          *9. boolean       false
  5. String               2000                            10. boolean       true                                      โจทย์ข้อที 6 [ระดับปานกลาง]
                                                                                                                       import java.util.Scanner;
                                                                                                                       public class ReverseSentence {
                                                                                                                         public static void main(String[] args) {
โจทย์ ข้อที 3 [ระดับง่ าย]                                                                                                 Scanner kb = new Scanner(System.in);
                                                                                                                           System.out.print("Enter Sentence: ");
1. p = p.toUpperCase();                                                                                                    String s = kb.nextLine(), rs = "";
      r = r.toUpperCase();                                                                                                 for (int i = s.length() - 1; i >= 0; i--) {
                                                                                                                             String t = s.substring(i, i + 1);
2.    boolean y = p.substring(4, 5).equals(r.substring(4, 5));                                                               rs = rs + t;
                                                                                                                           }
                                                                                                                           System.out.println(rs);
3.    double m = Double.parseDouble(p.substring(1, 6));                                                                  }
                                                                                                                       }




© สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)           © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
ANSWER 05-06                                                      Computer Programming using Java             3   4     Computer Programming using Java                                                           ANSWER 05-06


โจทย์ข้อที 7 [ระดับปานกลาง]                                                                                           โจทย์ข้อที 10 [ระดับยาก]
 import java.util.Scanner;                                                                                             import java.util.Scanner;
 public class FullName {                                                                                               public class Palindrome {
   public static void main(String[] args) {                                                                              public static void main(String[] args) {
     Scanner kb = new Scanner(System.in);                                                                                  Scanner kb = new Scanner(System.in);
     System.out.print("Full Name: ");                                                                                      System.out.print("Text: ");
     String fullName = kb.nextLine();                                                                                      String t = kb.nextLine(), s = "", r = "";
     int i = fullName.trim().indexOf(" ");                                                                                 for (int i = 0; i < t.length(); i++) {
     if (i == -1) {                                                                                                          String x = t.substring(i, i + 1);
       System.out.println("Incorrect Name");                                                                                 if (!x.equals(" ")) {
     } else {                                                                                                                  s = s + x;
       String firstName = fullName.substring(0, i);                                                                            r = x + r;
       String lastName = fullName.substring(i + 1).trim();                                                                   }
       System.out.println("First Name: " + firstName.toUpperCase());                                                       }
       System.out.println("Last Name: " + lastName.toLowerCase());                                                         if (s.equalsIgnoreCase(r)) {
     }                                                                                                                       System.out.println("It is palindrome");
   }                                                                                                                       } else {
 }                                                                                                                           System.out.println("It is not palindrome");
                                                                                                                           }
                                                                                                                         }
                                                                                                                       }
โจทย์ข้อที 8 [ระดับปานกลาง]
1) String s1 = "<title>First   Web Page</title>";
     String s2 = "<a href="http://www.javachula.co.cc">JavaChula</a>";                                              โจทย์ข้อที 11 [ระดับยาก]
                                                                                                                      import java.util.Scanner;
2)   int start = s1.indexOf("<title>") + 7;
                                                                                                                      public class NumberAndCharacter {
                                                                                                                        public static void main(String[] args) {
     int end = s1.indexOf("</title>");
     String title = s1.substring(start, end);                                                                                Scanner kb = new Scanner(System.in);
                                                                                                                             System.out.print("STRING: ");
3)                                                                                                                           String s = kb.nextLine();
     int start = s2.indexOf(""") + 1;                                                                                       int max = -1, min = 10, sum = 0, digit = 0, text = 0;
     int end = s2.lastIndexOf(""");                                                                                         for (int i = 0; i < s.length(); i++) {
     String url = s2.substring(start, end);                                                                                    String ch = s.substring(i, i + 1);
                                                                                                                               if (ch.compareTo("0") >= 0 && ch.compareTo("9") <= 0) {
                                                                                                                                 int n = Integer.parseInt(ch);
                                                                                                                                 if (n > max) {
โจทย์ข้อที 9 [ระดับปานกลาง]                                                                                                        max = n;
 int first = d.indexOf("/");                                                                                                     }
 int last = d.lastIndexOf("/");                                                                                                  if (n < min) {
 System.out.println(d.substring(0, first));                                                                                        min = n;
 System.out.println(d.substring(first + 1, last));                                                                               }
 System.out.println(d.substring(last + 1));                                                                                      sum += n;
                                                                                                                                 digit++;
                                                                                                                               } else {
                                                                                                                                 text++;
                                                                                                                               }
                                                                                                                             } //End of for
                                                                                                                             System.out.println("MAX VALUE: " + max);
                                                                                                                             System.out.println("MIN VALUE: " + min);
                                                                                                                             System.out.println("AVERAGE VALUE (" + sum + "/" + digit + "): " +
                                                                                                                                               ((double) sum / (double) digit));
                                                                                                                             System.out.println("TOTAL CHARACTER: " + text);

                                                                                                                        } //End of main
                                                                                                                      } //End of class

© สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)           © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
ANSWER 05-06                                                      Computer Programming using Java             5   6     Computer Programming using Java                                                           ANSWER 05-06


โจทย์ข้อที 12 [ระดับยาก]                                                                                              โจทย์ข้อที 13 [ระดับเทพ]
import java.util.Scanner;                                                                                             import java.util.Scanner;
public class NoobChat {                                                                                               public class GodNoobChat {
  public static void main(String[] args) {                                                                              public static void main(String[] args) {
       Scanner kb = new Scanner(System.in);                                                                                  Scanner kb = new Scanner(System.in);
       System.out.print("Message: ");                                                                                        String msg = "", lMsg = "", rMsg = "";
       String message = kb.nextLine();                                                                                       System.out.println("Message: ");
       String m = message.toLowerCase();                                                                                     while (true) {
       String badWord = "";                                                                                                    lMsg = kb.nextLine();
       int bw = 0;                                                                                                             if (lMsg.equals("...")) break;
                                                                                                                               msg += lMsg + "n";
       if (m.indexOf("shit")           >= 0) {                                                                               }
         badWord += "Shit ";           bw++;                                                                                 for (int i = 0; i < msg.length(); i++) {
       }                                                                                                                       if (!msg.substring(i, i + 1).equals(" ") &&
       if (m.indexOf("fuck")           >= 0) {                                                                                     !msg.substring(i, i + 1).equals("n"))
         badWord += "Fuck ";           bw++;                                                                                     rMsg += msg.substring(i, i + 1);
       }                                                                                                                     }
       if (m.indexOf("java")           >= 0) {
         badWord += "Java ";           bw++;                                                                                 int countShit = 0, countFuck = 0, countJava = 0;
       }                                                                                                                     String temp = rMsg.toLowerCase();
                                                                                                                             while (temp.indexOf("shit") >= 0) {
       if (bw == 0) {                                                                                                          countShit++;
         System.out.println(message);                                                                                          temp = temp.substring(temp.indexOf("shit") + 4);
       } else if (bw == 1) {                                                                                                 }
         System.out.println(badWord + "is Bad Word.");                                                                       temp = rMsg.toLowerCase();
       } else {                                                                                                              while (temp.indexOf("fuck") >= 0) {
         System.out.println(badWord + "are Bad Words.");                                                                       countFuck++;
       }//End of if                                                                                                            temp = temp.substring(temp.indexOf("fuck") + 4);
                                                                                                                             }
  } //End of main                                                                                                            temp = rMsg.toLowerCase();
} //End of class                                                                                                             while (temp.indexOf("java") >= 0) {
                                                                                                                               countJava++;
                                                                                                                               temp = temp.substring(temp.indexOf("java") + 4);
                                                                                                                             }

                                                                                                                             System.out.println("Count "Shit": " + countShit);
                                                                                                                             System.out.println("Count "Fuck": " + countFuck);
                                                                                                                             System.out.println("Count "Java": " + countJava);

                                                                                                                             String badWord = ""; int bw               = 0;
                                                                                                                             if (countShit > 0) { badWord              += "Shit "; bw++; }
                                                                                                                             if (countFuck > 0) { badWord              += "Fuck "; bw++; }
                                                                                                                             if (countJava > 0) { badWord              += "Java "; bw++; }
                                                                                                                             if (bw == 0) {
                                                                                                                               System.out.println(msg);
                                                                                                                             } else if (bw == 1) {
                                                                                                                               System.out.println(badWord              + "is bad word.");
                                                                                                                             } else {
                                                                                                                               System.out.println(badWord              + "are bad word.");
                                                                                                                             }

                                                                                                                        } //End of main
                                                                                                                      } //End of class




© สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)           © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
ANSWER 05-06                                                      Computer Programming using Java             7   8     Computer Programming using Java                                                           ANSWER 05-06


  CHAPTER                                   การดําเนินการกับแฟมข้อมูล
                                                                ้                                                     โจทย์ข้อที 3 [ระดับง่ าย]
 ANS-06
 ANS-                                            (File Operations)                                                     import java.util.Scanner;
                                                                                                                       import java.io.*;
                                                                                                                       public class NumberOfWord {
โจทย์ข้อที 1 [ระดับง่ าย]                                                                                                public static void main(String[] args) throws IOException {
     in.nextInt()            in.nextDouble()              in.nextLine()                 in.next()                          Scanner in = new Scanner(new File("data.txt"));
                                                                                                                           int numOfWord = 0;
 1                          1.0                        1   2    3                 1                                        while(in.hasNext()) {
 2                          2.0                        4                          2                                          in.next();
 3                          3.0                        5 6                        3                                          numOfWord++;
 4                          4.0                        7.0 8 9D 0                 4                                        }
                                                                                                                           System.out.println("Number of Words: " + numOfWord);
 5                          5.0                                                   5
                                                                                                                           in.close();
 6                          6.0                                                   6                                      }
 [Error]                    7.0                                                   7.0                                  }
                            8.0                                                   8
                            [Error]                                               9D
                                                                                  0
                                                                                                                      โจทย์ข้อที 4 [ระดับง่ าย]
   จํานวนรอบของ while        จํานวนรอบของ while         จํานวนรอบของ while         จํานวนรอบของ while                  import java.util.Scanner;
                                                                                                                       import java.io.*;
 7 รอบ                      9 รอบ                      4 รอบ                      10 รอบ                               public class NumberOfCharacter {
                                                                                                                         public static void main(String[] args) throws IOException {
                                                                                                                           Scanner in = new Scanner(new File("data.txt"));
                                                                                                                           int numOfChar = 0;
                                                                                                                           while(in.hasNext()) {
                                                                                                                             numOfChar += in.nextLine().length();
โจทย์ข้อที 2 [ระดับง่ าย]                                                                                                  }
 import java.util.Scanner;                                                                                                 System.out.println("Number of Chars: " + numOfChar);
 import java.io.*;                                                                                                         in.close();
 public class NumberOfLine {                                                                                             }
   public static void main(String[] args) throws IOException {                                                         }
     Scanner in = new Scanner(new File("data.txt"));
     int numOfLine = 0;
     while(in.hasNext()) {
       in.nextLine();                                                                                                 โจทย์ข้อที 5 [ระดับปานกลาง]
       numOfLine++;                                                                                                   import java.util.Scanner;
     }                                                                                                                import java.io.*;
     System.out.println("Number of Lines: " + numOfLine);                                                             public class CountEngStudent {
     in.close();                                                                                                        public static void main(String[] args) throws IOException {
   }
 }                                                                                                                           Scanner in = new Scanner(new File("std.txt"));
                                                                                                                             int count = 0;
                                                                                                                             while (in.hasNext()) {
                                                                                                                               String s = in.nextLine();
                                                                                                                               if (s.substring(s.length() - 2).equals("21")) count++;
                                                                                                                             }
                                                                                                                             in.close();
                                                                                                                             System.out.println("Engineering Students: " + count);

                                                                                                                        } //End of main
                                                                                                                      } //End of class




© สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)           © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
ANSWER 05-06                                                      Computer Programming using Java             9   10     Computer Programming using Java                                                           ANSWER 05-06


โจทย์ข้อที 6 [ระดับปานกลาง]                                                                                            โจทย์ข้อที 8 [ระดับยาก]
import java.util.Scanner;
import java.io.*;                                                                                                       import java.util.Scanner;
public class StudentGrade {                                                                                             import java.io.*;
  public static void main(String[] args) throws IOException {                                                           public class CountLoveFromSongFile {
                                                                                                                          public static void main(String[] args) throws IOException {
       Scanner in = new Scanner(new File("score.txt"));                                                                     Scanner in = new Scanner(new File("song.txt"));
       int i = 1;                       ตัวอย่ างการแสดงผลบนจอภาพ                                                           int w1 = 0, w2 = 0;
       while (in.hasNext()) {                                                                                               String s = "", txt = "";
         String id = in.next();                                                                                             while (in.hasNext()) {
         double score = in.nextDouble();                                                                                      s = in.next().toLowerCase();
         in.next(); //faculty                                                                                                 if (s.equals("love")) w1++;
         if(score >= 60.0) {                                                                                                  txt += s;
           System.out.println(i + ".t" + id + "tS");                                                                      }
         } else {                                                                                                           while (txt.indexOf("love") >= 0) {
           System.out.println(i + ".t" + id + "tU");                                                                        w2++;
         }                                                                                                                    txt = txt.substring(txt.indexOf("love") + 4);
         i++;                                                                                                               }
       }                                                                                                                    in.close();
       in.close();                                                                                                          System.out.println("Count Words #1: " + w1);
                                                                                                                            System.out.println("Count Words #2: " + w2);
  } //End of main                                                                                                         }
} //End of class                                                                                                        }


โจทย์ข้อที 7 [ระดับยาก]                                                                                                โจทย์ข้อที 9 [ระดับยาก]
import java.util.Scanner;                                                                                              import java.util.Scanner;
import java.io.*;                                                                                                      import java.io.*;
public class StudentInfoFromFile {                                                                                     public class FComparison {
  public static void main(String[] args) throws IOException {                                                            public static void main(String[] args) throws IOException {
       Scanner in = new Scanner(new File("student.dat"));                                                                     Scanner in1 = new Scanner(new File("data1.dat"));
       int i = 1;                                                                                                             Scanner in2 = new Scanner(new File("data2.dat"));
       while (in.hasNext()) {                                                                                                 int f1 = 0, f2 = 0, n1 = 0, n2 = 0;
         String id = in.next();                                                                                               in1.nextLine();
         String fname = in.next();                                                                                            while (in1.hasNext()) {
         String lname = in.next();                                                                                              in1.next(); in1.next();
         double grade = in.nextDouble();                                                                                        String grade = in1.next();
         int year = 54 - Integer.parseInt(id.substring(0, 2));                                                                  if (grade.equals("F")) f1++;
         String y = "";                                                                                                         n1++;
         if (year == 1) y = year + "st";                                                                                      } //End of while
         if (year == 2) y = year + "nd";                                                                                      in2.nextLine();
         if (year == 3) y = year + "rd";                                                                                      while (in2.hasNext()) {
         if (year >= 4) y = year + "th";                                                                                        in2.next(); in2.next();
         String shortName = fname.substring(0, 1).toUpperCase() + ".";                                                          String grade = in2.next();
         String status = "";                                                                                                    if (grade.equals("F")) f2++;
         if (grade >= 2.00) status = "Pass";                                                                                    n2++;
         if (grade >= 1.00 && grade < 2.00) status = "Critical";                                                              } //End of while
         if (grade < 1.00) status = "Retired";                                                                                in1.close(); in2.close();
         System.out.println(i++ + ".t" + id + "t" + y + "t" +                                                              System.out.println("F 2/2552: " + ((double) f1 / n1) * 100);
               shortName + " " + lname + "t" +                                                                               System.out.println("F 2/2553: " + ((double) f2 / n2) * 100);
               grade + "t" + status);                                                                                        if (f1 > f2) System.out.println("F (2/2552) > F (2/2553)");
       } //End of while                                                                                                       if (f1 == f2) System.out.println("F (2/2552) = F (2/2553)");
       in.close();                                                                                                            if (f1 < f2) System.out.println("F (2/2552) < F (2/2553)");
  } //End of main                                                                                                        } //End of main
} //End of class                                                                                                       } //End of class

© สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)            © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
ANSWER 05-06                                                      Computer Programming using Java             11
โจทย์ข้อที 10 [ระดับง่ าย]
 import java.io.*;
 import java.util.Scanner;
 public class InputStringToFile {
   public static void main(String[] args) throws IOException {
     Scanner kb = new Scanner(System.in);
     PrintStream out = new PrintStream(new File("sentence.txt"));
     int i = 1;
     while (true) {
       System.out.print("Sentence: ");
       String s = kb.nextLine().toUpperCase();
       if (s.trim().equalsIgnoreCase("stop")) break;
       out.println(i + ": " + s);
       i++;
     }
     System.out.println("File is saved");
     out.close();
   }
 }



โจทย์ข้อที 11 [ระดับยาก]
 import java.util.Scanner;
 import java.io.*;
 public class ReverseTextFile {
   public static void main(String[] args) throws IOException {
     Scanner in = new Scanner(new File("text.txt"));
     PrintStream out = new PrintStream(new File("revtext.txt"));
     while (in.hasNext()) {
       String s = in.nextLine();
       String rev = "";
       for (int i = s.length() - 1; i >= 0; i--) {
         rev += s.substring(i, i + 1);
       }
       out.println(rev);
     }
     in.close();
     out.close();
   }
 }




© สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)

Weitere ähnliche Inhalte

Was ist angesagt?

Java-Answer Chapter 08-09 (For Print)
Java-Answer Chapter 08-09 (For Print)Java-Answer Chapter 08-09 (For Print)
Java-Answer Chapter 08-09 (For Print)Wongyos Keardsri
 
Java-Answer Chapter 10-11 (For Print)
Java-Answer Chapter 10-11 (For Print)Java-Answer Chapter 10-11 (For Print)
Java-Answer Chapter 10-11 (For Print)Wongyos Keardsri
 
Java-Chapter 11 Recursions
Java-Chapter 11 RecursionsJava-Chapter 11 Recursions
Java-Chapter 11 RecursionsWongyos Keardsri
 
Java-Chapter 12 Classes and Objects
Java-Chapter 12 Classes and ObjectsJava-Chapter 12 Classes and Objects
Java-Chapter 12 Classes and ObjectsWongyos Keardsri
 
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นคลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นFinian Nian
 
Java-Chapter 01 Introduction to Java Programming
Java-Chapter 01 Introduction to Java ProgrammingJava-Chapter 01 Introduction to Java Programming
Java-Chapter 01 Introduction to Java ProgrammingWongyos Keardsri
 
Java Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่นJava Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่นThanachart Numnonda
 
Java Programming: โครงสร้างควบคุม
Java Programming: โครงสร้างควบคุมJava Programming: โครงสร้างควบคุม
Java Programming: โครงสร้างควบคุมThanachart Numnonda
 
Java Programming [8/12] : Arrays and Collection
Java Programming [8/12] : Arrays and CollectionJava Programming [8/12] : Arrays and Collection
Java Programming [8/12] : Arrays and CollectionIMC Institute
 
Java Programming: การจัดการกับข้อผิดพลาด
Java Programming: การจัดการกับข้อผิดพลาดJava Programming: การจัดการกับข้อผิดพลาด
Java Programming: การจัดการกับข้อผิดพลาดThanachart Numnonda
 
Java Programming: การเขียนโปรแกรมภาษาจาวาเชิงอ็อบเจกต์
Java Programming: การเขียนโปรแกรมภาษาจาวาเชิงอ็อบเจกต์Java Programming: การเขียนโปรแกรมภาษาจาวาเชิงอ็อบเจกต์
Java Programming: การเขียนโปรแกรมภาษาจาวาเชิงอ็อบเจกต์Thanachart Numnonda
 
Java Programming: คลาสอินพุตและเอาต์พุต
Java Programming: คลาสอินพุตและเอาต์พุตJava Programming: คลาสอินพุตและเอาต์พุต
Java Programming: คลาสอินพุตและเอาต์พุตThanachart Numnonda
 
บทที่ 6 อาร์เรย์
บทที่ 6 อาร์เรย์บทที่ 6 อาร์เรย์
บทที่ 6 อาร์เรย์Theeravaj Tum
 
Java Programming [12/12] : Thread
Java Programming [12/12] : ThreadJava Programming [12/12] : Thread
Java Programming [12/12] : ThreadIMC Institute
 

Was ist angesagt? (20)

Java-Answer Chapter 08-09 (For Print)
Java-Answer Chapter 08-09 (For Print)Java-Answer Chapter 08-09 (For Print)
Java-Answer Chapter 08-09 (For Print)
 
Java-Answer Chapter 08-09
Java-Answer Chapter 08-09Java-Answer Chapter 08-09
Java-Answer Chapter 08-09
 
Java-Chapter 08 Methods
Java-Chapter 08 MethodsJava-Chapter 08 Methods
Java-Chapter 08 Methods
 
Java-Answer Chapter 10-11 (For Print)
Java-Answer Chapter 10-11 (For Print)Java-Answer Chapter 10-11 (For Print)
Java-Answer Chapter 10-11 (For Print)
 
Java-Chapter 11 Recursions
Java-Chapter 11 RecursionsJava-Chapter 11 Recursions
Java-Chapter 11 Recursions
 
Java-Chapter 12 Classes and Objects
Java-Chapter 12 Classes and ObjectsJava-Chapter 12 Classes and Objects
Java-Chapter 12 Classes and Objects
 
Java-Answer Chapter 07
Java-Answer Chapter 07Java-Answer Chapter 07
Java-Answer Chapter 07
 
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นคลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
 
Java-Chapter 01 Introduction to Java Programming
Java-Chapter 01 Introduction to Java ProgrammingJava-Chapter 01 Introduction to Java Programming
Java-Chapter 01 Introduction to Java Programming
 
4.Oop
4.Oop4.Oop
4.Oop
 
5.Methods cs
5.Methods cs5.Methods cs
5.Methods cs
 
3.Expression
3.Expression3.Expression
3.Expression
 
Java Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่นJava Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่น
 
Java Programming: โครงสร้างควบคุม
Java Programming: โครงสร้างควบคุมJava Programming: โครงสร้างควบคุม
Java Programming: โครงสร้างควบคุม
 
Java Programming [8/12] : Arrays and Collection
Java Programming [8/12] : Arrays and CollectionJava Programming [8/12] : Arrays and Collection
Java Programming [8/12] : Arrays and Collection
 
Java Programming: การจัดการกับข้อผิดพลาด
Java Programming: การจัดการกับข้อผิดพลาดJava Programming: การจัดการกับข้อผิดพลาด
Java Programming: การจัดการกับข้อผิดพลาด
 
Java Programming: การเขียนโปรแกรมภาษาจาวาเชิงอ็อบเจกต์
Java Programming: การเขียนโปรแกรมภาษาจาวาเชิงอ็อบเจกต์Java Programming: การเขียนโปรแกรมภาษาจาวาเชิงอ็อบเจกต์
Java Programming: การเขียนโปรแกรมภาษาจาวาเชิงอ็อบเจกต์
 
Java Programming: คลาสอินพุตและเอาต์พุต
Java Programming: คลาสอินพุตและเอาต์พุตJava Programming: คลาสอินพุตและเอาต์พุต
Java Programming: คลาสอินพุตและเอาต์พุต
 
บทที่ 6 อาร์เรย์
บทที่ 6 อาร์เรย์บทที่ 6 อาร์เรย์
บทที่ 6 อาร์เรย์
 
Java Programming [12/12] : Thread
Java Programming [12/12] : ThreadJava Programming [12/12] : Thread
Java Programming [12/12] : Thread
 

Andere mochten auch

How to Study and Research in Computer-related Master Program
How to Study and Research in Computer-related Master ProgramHow to Study and Research in Computer-related Master Program
How to Study and Research in Computer-related Master ProgramWongyos Keardsri
 
Java-Chapter 06 File Operations
Java-Chapter 06 File OperationsJava-Chapter 06 File Operations
Java-Chapter 06 File OperationsWongyos Keardsri
 
Java-Chapter 04 Iteration Statements
Java-Chapter 04 Iteration StatementsJava-Chapter 04 Iteration Statements
Java-Chapter 04 Iteration StatementsWongyos Keardsri
 
Java-Chapter 10 Two Dimensional Arrays
Java-Chapter 10 Two Dimensional ArraysJava-Chapter 10 Two Dimensional Arrays
Java-Chapter 10 Two Dimensional ArraysWongyos Keardsri
 
Java-Chapter 07 One Dimensional Arrays
Java-Chapter 07 One Dimensional ArraysJava-Chapter 07 One Dimensional Arrays
Java-Chapter 07 One Dimensional ArraysWongyos Keardsri
 
Java-Chapter 09 Advanced Statements and Applications
Java-Chapter 09 Advanced Statements and ApplicationsJava-Chapter 09 Advanced Statements and Applications
Java-Chapter 09 Advanced Statements and ApplicationsWongyos Keardsri
 
Java-Chapter 05 String Operations
Java-Chapter 05 String OperationsJava-Chapter 05 String Operations
Java-Chapter 05 String OperationsWongyos Keardsri
 
Java-Chapter 02 Data Operations and Processing
Java-Chapter 02 Data Operations and ProcessingJava-Chapter 02 Data Operations and Processing
Java-Chapter 02 Data Operations and ProcessingWongyos Keardsri
 
การควบคุมทิศทางการทำงานของโปรแกรม
การควบคุมทิศทางการทำงานของโปรแกรมการควบคุมทิศทางการทำงานของโปรแกรม
การควบคุมทิศทางการทำงานของโปรแกรมkorn27122540
 

Andere mochten auch (10)

How to Study and Research in Computer-related Master Program
How to Study and Research in Computer-related Master ProgramHow to Study and Research in Computer-related Master Program
How to Study and Research in Computer-related Master Program
 
Java-Chapter 06 File Operations
Java-Chapter 06 File OperationsJava-Chapter 06 File Operations
Java-Chapter 06 File Operations
 
Java-Chapter 04 Iteration Statements
Java-Chapter 04 Iteration StatementsJava-Chapter 04 Iteration Statements
Java-Chapter 04 Iteration Statements
 
Java-Chapter 10 Two Dimensional Arrays
Java-Chapter 10 Two Dimensional ArraysJava-Chapter 10 Two Dimensional Arrays
Java-Chapter 10 Two Dimensional Arrays
 
Java-Chapter 07 One Dimensional Arrays
Java-Chapter 07 One Dimensional ArraysJava-Chapter 07 One Dimensional Arrays
Java-Chapter 07 One Dimensional Arrays
 
Java-Chapter 09 Advanced Statements and Applications
Java-Chapter 09 Advanced Statements and ApplicationsJava-Chapter 09 Advanced Statements and Applications
Java-Chapter 09 Advanced Statements and Applications
 
Java-Chapter 05 String Operations
Java-Chapter 05 String OperationsJava-Chapter 05 String Operations
Java-Chapter 05 String Operations
 
Java-Chapter 02 Data Operations and Processing
Java-Chapter 02 Data Operations and ProcessingJava-Chapter 02 Data Operations and Processing
Java-Chapter 02 Data Operations and Processing
 
IP address anonymization
IP address anonymizationIP address anonymization
IP address anonymization
 
การควบคุมทิศทางการทำงานของโปรแกรม
การควบคุมทิศทางการทำงานของโปรแกรมการควบคุมทิศทางการทำงานของโปรแกรม
การควบคุมทิศทางการทำงานของโปรแกรม
 

Mehr von Wongyos Keardsri

The next generation intelligent transport systems: standards and applications
The next generation intelligent transport systems: standards and applicationsThe next generation intelligent transport systems: standards and applications
The next generation intelligent transport systems: standards and applicationsWongyos Keardsri
 
SysProg-Tutor 03 Unix Shell Script Programming
SysProg-Tutor 03 Unix Shell Script ProgrammingSysProg-Tutor 03 Unix Shell Script Programming
SysProg-Tutor 03 Unix Shell Script ProgrammingWongyos Keardsri
 
SysProg-Tutor 02 Introduction to Unix Operating System
SysProg-Tutor 02 Introduction to Unix Operating SystemSysProg-Tutor 02 Introduction to Unix Operating System
SysProg-Tutor 02 Introduction to Unix Operating SystemWongyos Keardsri
 
SysProg-Tutor 01 Introduction to C Programming Language
SysProg-Tutor 01 Introduction to C Programming LanguageSysProg-Tutor 01 Introduction to C Programming Language
SysProg-Tutor 01 Introduction to C Programming LanguageWongyos Keardsri
 
Discrete-Chapter 11 Graphs Part III
Discrete-Chapter 11 Graphs Part IIIDiscrete-Chapter 11 Graphs Part III
Discrete-Chapter 11 Graphs Part IIIWongyos Keardsri
 
Discrete-Chapter 11 Graphs Part II
Discrete-Chapter 11 Graphs Part IIDiscrete-Chapter 11 Graphs Part II
Discrete-Chapter 11 Graphs Part IIWongyos Keardsri
 
Discrete-Chapter 11 Graphs Part I
Discrete-Chapter 11 Graphs Part IDiscrete-Chapter 11 Graphs Part I
Discrete-Chapter 11 Graphs Part IWongyos Keardsri
 
Discrete-Chapter 09 Algorithms
Discrete-Chapter 09 AlgorithmsDiscrete-Chapter 09 Algorithms
Discrete-Chapter 09 AlgorithmsWongyos Keardsri
 
Discrete-Chapter 08 Relations
Discrete-Chapter 08 RelationsDiscrete-Chapter 08 Relations
Discrete-Chapter 08 RelationsWongyos Keardsri
 
Discrete-Chapter 07 Probability
Discrete-Chapter 07 ProbabilityDiscrete-Chapter 07 Probability
Discrete-Chapter 07 ProbabilityWongyos Keardsri
 
Discrete-Chapter 06 Counting
Discrete-Chapter 06 CountingDiscrete-Chapter 06 Counting
Discrete-Chapter 06 CountingWongyos Keardsri
 
Discrete-Chapter 05 Inference and Proofs
Discrete-Chapter 05 Inference and ProofsDiscrete-Chapter 05 Inference and Proofs
Discrete-Chapter 05 Inference and ProofsWongyos Keardsri
 
Discrete-Chapter 04 Logic Part II
Discrete-Chapter 04 Logic Part IIDiscrete-Chapter 04 Logic Part II
Discrete-Chapter 04 Logic Part IIWongyos Keardsri
 
Discrete-Chapter 04 Logic Part I
Discrete-Chapter 04 Logic Part IDiscrete-Chapter 04 Logic Part I
Discrete-Chapter 04 Logic Part IWongyos Keardsri
 
Discrete-Chapter 03 Matrices
Discrete-Chapter 03 MatricesDiscrete-Chapter 03 Matrices
Discrete-Chapter 03 MatricesWongyos Keardsri
 
Discrete-Chapter 02 Functions and Sequences
Discrete-Chapter 02 Functions and SequencesDiscrete-Chapter 02 Functions and Sequences
Discrete-Chapter 02 Functions and SequencesWongyos Keardsri
 
Discrete-Chapter 12 Modeling Computation
Discrete-Chapter 12 Modeling ComputationDiscrete-Chapter 12 Modeling Computation
Discrete-Chapter 12 Modeling ComputationWongyos Keardsri
 
Java-Chapter 14 Creating Graphics with DWindow
Java-Chapter 14 Creating Graphics with DWindowJava-Chapter 14 Creating Graphics with DWindow
Java-Chapter 14 Creating Graphics with DWindowWongyos Keardsri
 

Mehr von Wongyos Keardsri (20)

The next generation intelligent transport systems: standards and applications
The next generation intelligent transport systems: standards and applicationsThe next generation intelligent transport systems: standards and applications
The next generation intelligent transport systems: standards and applications
 
SysProg-Tutor 03 Unix Shell Script Programming
SysProg-Tutor 03 Unix Shell Script ProgrammingSysProg-Tutor 03 Unix Shell Script Programming
SysProg-Tutor 03 Unix Shell Script Programming
 
SysProg-Tutor 02 Introduction to Unix Operating System
SysProg-Tutor 02 Introduction to Unix Operating SystemSysProg-Tutor 02 Introduction to Unix Operating System
SysProg-Tutor 02 Introduction to Unix Operating System
 
SysProg-Tutor 01 Introduction to C Programming Language
SysProg-Tutor 01 Introduction to C Programming LanguageSysProg-Tutor 01 Introduction to C Programming Language
SysProg-Tutor 01 Introduction to C Programming Language
 
Discrete-Chapter 11 Graphs Part III
Discrete-Chapter 11 Graphs Part IIIDiscrete-Chapter 11 Graphs Part III
Discrete-Chapter 11 Graphs Part III
 
Discrete-Chapter 11 Graphs Part II
Discrete-Chapter 11 Graphs Part IIDiscrete-Chapter 11 Graphs Part II
Discrete-Chapter 11 Graphs Part II
 
Discrete-Chapter 11 Graphs Part I
Discrete-Chapter 11 Graphs Part IDiscrete-Chapter 11 Graphs Part I
Discrete-Chapter 11 Graphs Part I
 
Discrete-Chapter 10 Trees
Discrete-Chapter 10 TreesDiscrete-Chapter 10 Trees
Discrete-Chapter 10 Trees
 
Discrete-Chapter 09 Algorithms
Discrete-Chapter 09 AlgorithmsDiscrete-Chapter 09 Algorithms
Discrete-Chapter 09 Algorithms
 
Discrete-Chapter 08 Relations
Discrete-Chapter 08 RelationsDiscrete-Chapter 08 Relations
Discrete-Chapter 08 Relations
 
Discrete-Chapter 07 Probability
Discrete-Chapter 07 ProbabilityDiscrete-Chapter 07 Probability
Discrete-Chapter 07 Probability
 
Discrete-Chapter 06 Counting
Discrete-Chapter 06 CountingDiscrete-Chapter 06 Counting
Discrete-Chapter 06 Counting
 
Discrete-Chapter 05 Inference and Proofs
Discrete-Chapter 05 Inference and ProofsDiscrete-Chapter 05 Inference and Proofs
Discrete-Chapter 05 Inference and Proofs
 
Discrete-Chapter 04 Logic Part II
Discrete-Chapter 04 Logic Part IIDiscrete-Chapter 04 Logic Part II
Discrete-Chapter 04 Logic Part II
 
Discrete-Chapter 04 Logic Part I
Discrete-Chapter 04 Logic Part IDiscrete-Chapter 04 Logic Part I
Discrete-Chapter 04 Logic Part I
 
Discrete-Chapter 03 Matrices
Discrete-Chapter 03 MatricesDiscrete-Chapter 03 Matrices
Discrete-Chapter 03 Matrices
 
Discrete-Chapter 02 Functions and Sequences
Discrete-Chapter 02 Functions and SequencesDiscrete-Chapter 02 Functions and Sequences
Discrete-Chapter 02 Functions and Sequences
 
Discrete-Chapter 01 Sets
Discrete-Chapter 01 SetsDiscrete-Chapter 01 Sets
Discrete-Chapter 01 Sets
 
Discrete-Chapter 12 Modeling Computation
Discrete-Chapter 12 Modeling ComputationDiscrete-Chapter 12 Modeling Computation
Discrete-Chapter 12 Modeling Computation
 
Java-Chapter 14 Creating Graphics with DWindow
Java-Chapter 14 Creating Graphics with DWindowJava-Chapter 14 Creating Graphics with DWindow
Java-Chapter 14 Creating Graphics with DWindow
 

Java-Answer Chapter 05-06 (For Print)

  • 1. ANSWER 05-06 Computer Programming using Java 1 2 Computer Programming using Java ANSWER 05-06 CHAPTER การดําเนินการกับสตริง โจทย์ข้อที 4 [ระดับง่ าย] ANS-05 ANS- (String Operations) import java.util.Scanner; public class InputWords { public static void main(String[] args) { Scanner kb = new Scanner(System.in); โจทย์ข้อที 1 [ระดับง่ าย] String text = ""; ข้ อ ประเภทตัวแปร ค่ าทีเก็บในตัวแปร ข้ อ ประเภทตัวแปร ค่ าทีเก็บในตัวแปร while (true) { System.out.print("Enter Word: "); 1. int 6 11. int -1 String w = kb.next(); if (w.toLowerCase().equals("stop")) break; 2. int 5 12. int 7 text += w + " "; /* text = text + w + " "; */ 3. int 1 13. String JavaChula } System.out.println(text); 4. int 3 14. String I Lo } } 5. String CHULA chula 15. String love java 6. char v 16. boolean false 7. int 4 17. boolean true โจทย์ข้อที 5 [ระดับปานกลาง] import java.util.Scanner; 8. int 9 18. boolean true public class RemoveSpaceFromSentence { 9. int 8 19. int -20 public static void main(String[] args) { Scanner kb = new Scanner(System.in); 10. int 8 20. [Error] [Error] System.out.print("Enter Sentence: "); String s = kb.nextLine(); String sn = ""; for (int i = 0; i < s.length(); i++) { String t = s.substring(i, i + 1); โจทย์ข้อที 2 [ระดับง่ าย] if (!t.equals(" ")) { sn = sn + t; ข้ อ ประเภทตัวแปร ค่ าทีเก็บในตัวแปร ข้ อ ประเภทตัวแปร ค่ าทีเก็บในตัวแปร } 1. int 2000 6. [Error] [Error] } System.out.println(sn); 2. [Error] [Error] 7. String 2000.0 } } 3. double 2000.0 8. String 1000.0 4. double 1000.0 *9. boolean false 5. String 2000 10. boolean true โจทย์ข้อที 6 [ระดับปานกลาง] import java.util.Scanner; public class ReverseSentence { public static void main(String[] args) { โจทย์ ข้อที 3 [ระดับง่ าย] Scanner kb = new Scanner(System.in); System.out.print("Enter Sentence: "); 1. p = p.toUpperCase(); String s = kb.nextLine(), rs = ""; r = r.toUpperCase(); for (int i = s.length() - 1; i >= 0; i--) { String t = s.substring(i, i + 1); 2. boolean y = p.substring(4, 5).equals(r.substring(4, 5)); rs = rs + t; } System.out.println(rs); 3. double m = Double.parseDouble(p.substring(1, 6)); } } © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
  • 2. ANSWER 05-06 Computer Programming using Java 3 4 Computer Programming using Java ANSWER 05-06 โจทย์ข้อที 7 [ระดับปานกลาง] โจทย์ข้อที 10 [ระดับยาก] import java.util.Scanner; import java.util.Scanner; public class FullName { public class Palindrome { public static void main(String[] args) { public static void main(String[] args) { Scanner kb = new Scanner(System.in); Scanner kb = new Scanner(System.in); System.out.print("Full Name: "); System.out.print("Text: "); String fullName = kb.nextLine(); String t = kb.nextLine(), s = "", r = ""; int i = fullName.trim().indexOf(" "); for (int i = 0; i < t.length(); i++) { if (i == -1) { String x = t.substring(i, i + 1); System.out.println("Incorrect Name"); if (!x.equals(" ")) { } else { s = s + x; String firstName = fullName.substring(0, i); r = x + r; String lastName = fullName.substring(i + 1).trim(); } System.out.println("First Name: " + firstName.toUpperCase()); } System.out.println("Last Name: " + lastName.toLowerCase()); if (s.equalsIgnoreCase(r)) { } System.out.println("It is palindrome"); } } else { } System.out.println("It is not palindrome"); } } } โจทย์ข้อที 8 [ระดับปานกลาง] 1) String s1 = "<title>First Web Page</title>"; String s2 = "<a href="http://www.javachula.co.cc">JavaChula</a>"; โจทย์ข้อที 11 [ระดับยาก] import java.util.Scanner; 2) int start = s1.indexOf("<title>") + 7; public class NumberAndCharacter { public static void main(String[] args) { int end = s1.indexOf("</title>"); String title = s1.substring(start, end); Scanner kb = new Scanner(System.in); System.out.print("STRING: "); 3) String s = kb.nextLine(); int start = s2.indexOf(""") + 1; int max = -1, min = 10, sum = 0, digit = 0, text = 0; int end = s2.lastIndexOf("""); for (int i = 0; i < s.length(); i++) { String url = s2.substring(start, end); String ch = s.substring(i, i + 1); if (ch.compareTo("0") >= 0 && ch.compareTo("9") <= 0) { int n = Integer.parseInt(ch); if (n > max) { โจทย์ข้อที 9 [ระดับปานกลาง] max = n; int first = d.indexOf("/"); } int last = d.lastIndexOf("/"); if (n < min) { System.out.println(d.substring(0, first)); min = n; System.out.println(d.substring(first + 1, last)); } System.out.println(d.substring(last + 1)); sum += n; digit++; } else { text++; } } //End of for System.out.println("MAX VALUE: " + max); System.out.println("MIN VALUE: " + min); System.out.println("AVERAGE VALUE (" + sum + "/" + digit + "): " + ((double) sum / (double) digit)); System.out.println("TOTAL CHARACTER: " + text); } //End of main } //End of class © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
  • 3. ANSWER 05-06 Computer Programming using Java 5 6 Computer Programming using Java ANSWER 05-06 โจทย์ข้อที 12 [ระดับยาก] โจทย์ข้อที 13 [ระดับเทพ] import java.util.Scanner; import java.util.Scanner; public class NoobChat { public class GodNoobChat { public static void main(String[] args) { public static void main(String[] args) { Scanner kb = new Scanner(System.in); Scanner kb = new Scanner(System.in); System.out.print("Message: "); String msg = "", lMsg = "", rMsg = ""; String message = kb.nextLine(); System.out.println("Message: "); String m = message.toLowerCase(); while (true) { String badWord = ""; lMsg = kb.nextLine(); int bw = 0; if (lMsg.equals("...")) break; msg += lMsg + "n"; if (m.indexOf("shit") >= 0) { } badWord += "Shit "; bw++; for (int i = 0; i < msg.length(); i++) { } if (!msg.substring(i, i + 1).equals(" ") && if (m.indexOf("fuck") >= 0) { !msg.substring(i, i + 1).equals("n")) badWord += "Fuck "; bw++; rMsg += msg.substring(i, i + 1); } } if (m.indexOf("java") >= 0) { badWord += "Java "; bw++; int countShit = 0, countFuck = 0, countJava = 0; } String temp = rMsg.toLowerCase(); while (temp.indexOf("shit") >= 0) { if (bw == 0) { countShit++; System.out.println(message); temp = temp.substring(temp.indexOf("shit") + 4); } else if (bw == 1) { } System.out.println(badWord + "is Bad Word."); temp = rMsg.toLowerCase(); } else { while (temp.indexOf("fuck") >= 0) { System.out.println(badWord + "are Bad Words."); countFuck++; }//End of if temp = temp.substring(temp.indexOf("fuck") + 4); } } //End of main temp = rMsg.toLowerCase(); } //End of class while (temp.indexOf("java") >= 0) { countJava++; temp = temp.substring(temp.indexOf("java") + 4); } System.out.println("Count "Shit": " + countShit); System.out.println("Count "Fuck": " + countFuck); System.out.println("Count "Java": " + countJava); String badWord = ""; int bw = 0; if (countShit > 0) { badWord += "Shit "; bw++; } if (countFuck > 0) { badWord += "Fuck "; bw++; } if (countJava > 0) { badWord += "Java "; bw++; } if (bw == 0) { System.out.println(msg); } else if (bw == 1) { System.out.println(badWord + "is bad word."); } else { System.out.println(badWord + "are bad word."); } } //End of main } //End of class © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
  • 4. ANSWER 05-06 Computer Programming using Java 7 8 Computer Programming using Java ANSWER 05-06 CHAPTER การดําเนินการกับแฟมข้อมูล ้ โจทย์ข้อที 3 [ระดับง่ าย] ANS-06 ANS- (File Operations) import java.util.Scanner; import java.io.*; public class NumberOfWord { โจทย์ข้อที 1 [ระดับง่ าย] public static void main(String[] args) throws IOException { in.nextInt() in.nextDouble() in.nextLine() in.next() Scanner in = new Scanner(new File("data.txt")); int numOfWord = 0; 1 1.0 1 2 3 1 while(in.hasNext()) { 2 2.0 4 2 in.next(); 3 3.0 5 6 3 numOfWord++; 4 4.0 7.0 8 9D 0 4 } System.out.println("Number of Words: " + numOfWord); 5 5.0 5 in.close(); 6 6.0 6 } [Error] 7.0 7.0 } 8.0 8 [Error] 9D 0 โจทย์ข้อที 4 [ระดับง่ าย] จํานวนรอบของ while จํานวนรอบของ while จํานวนรอบของ while จํานวนรอบของ while import java.util.Scanner; import java.io.*; 7 รอบ 9 รอบ 4 รอบ 10 รอบ public class NumberOfCharacter { public static void main(String[] args) throws IOException { Scanner in = new Scanner(new File("data.txt")); int numOfChar = 0; while(in.hasNext()) { numOfChar += in.nextLine().length(); โจทย์ข้อที 2 [ระดับง่ าย] } import java.util.Scanner; System.out.println("Number of Chars: " + numOfChar); import java.io.*; in.close(); public class NumberOfLine { } public static void main(String[] args) throws IOException { } Scanner in = new Scanner(new File("data.txt")); int numOfLine = 0; while(in.hasNext()) { in.nextLine(); โจทย์ข้อที 5 [ระดับปานกลาง] numOfLine++; import java.util.Scanner; } import java.io.*; System.out.println("Number of Lines: " + numOfLine); public class CountEngStudent { in.close(); public static void main(String[] args) throws IOException { } } Scanner in = new Scanner(new File("std.txt")); int count = 0; while (in.hasNext()) { String s = in.nextLine(); if (s.substring(s.length() - 2).equals("21")) count++; } in.close(); System.out.println("Engineering Students: " + count); } //End of main } //End of class © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
  • 5. ANSWER 05-06 Computer Programming using Java 9 10 Computer Programming using Java ANSWER 05-06 โจทย์ข้อที 6 [ระดับปานกลาง] โจทย์ข้อที 8 [ระดับยาก] import java.util.Scanner; import java.io.*; import java.util.Scanner; public class StudentGrade { import java.io.*; public static void main(String[] args) throws IOException { public class CountLoveFromSongFile { public static void main(String[] args) throws IOException { Scanner in = new Scanner(new File("score.txt")); Scanner in = new Scanner(new File("song.txt")); int i = 1; ตัวอย่ างการแสดงผลบนจอภาพ int w1 = 0, w2 = 0; while (in.hasNext()) { String s = "", txt = ""; String id = in.next(); while (in.hasNext()) { double score = in.nextDouble(); s = in.next().toLowerCase(); in.next(); //faculty if (s.equals("love")) w1++; if(score >= 60.0) { txt += s; System.out.println(i + ".t" + id + "tS"); } } else { while (txt.indexOf("love") >= 0) { System.out.println(i + ".t" + id + "tU"); w2++; } txt = txt.substring(txt.indexOf("love") + 4); i++; } } in.close(); in.close(); System.out.println("Count Words #1: " + w1); System.out.println("Count Words #2: " + w2); } //End of main } } //End of class } โจทย์ข้อที 7 [ระดับยาก] โจทย์ข้อที 9 [ระดับยาก] import java.util.Scanner; import java.util.Scanner; import java.io.*; import java.io.*; public class StudentInfoFromFile { public class FComparison { public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException { Scanner in = new Scanner(new File("student.dat")); Scanner in1 = new Scanner(new File("data1.dat")); int i = 1; Scanner in2 = new Scanner(new File("data2.dat")); while (in.hasNext()) { int f1 = 0, f2 = 0, n1 = 0, n2 = 0; String id = in.next(); in1.nextLine(); String fname = in.next(); while (in1.hasNext()) { String lname = in.next(); in1.next(); in1.next(); double grade = in.nextDouble(); String grade = in1.next(); int year = 54 - Integer.parseInt(id.substring(0, 2)); if (grade.equals("F")) f1++; String y = ""; n1++; if (year == 1) y = year + "st"; } //End of while if (year == 2) y = year + "nd"; in2.nextLine(); if (year == 3) y = year + "rd"; while (in2.hasNext()) { if (year >= 4) y = year + "th"; in2.next(); in2.next(); String shortName = fname.substring(0, 1).toUpperCase() + "."; String grade = in2.next(); String status = ""; if (grade.equals("F")) f2++; if (grade >= 2.00) status = "Pass"; n2++; if (grade >= 1.00 && grade < 2.00) status = "Critical"; } //End of while if (grade < 1.00) status = "Retired"; in1.close(); in2.close(); System.out.println(i++ + ".t" + id + "t" + y + "t" + System.out.println("F 2/2552: " + ((double) f1 / n1) * 100); shortName + " " + lname + "t" + System.out.println("F 2/2553: " + ((double) f2 / n2) * 100); grade + "t" + status); if (f1 > f2) System.out.println("F (2/2552) > F (2/2553)"); } //End of while if (f1 == f2) System.out.println("F (2/2552) = F (2/2553)"); in.close(); if (f1 < f2) System.out.println("F (2/2552) < F (2/2553)"); } //End of main } //End of main } //End of class } //End of class © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
  • 6. ANSWER 05-06 Computer Programming using Java 11 โจทย์ข้อที 10 [ระดับง่ าย] import java.io.*; import java.util.Scanner; public class InputStringToFile { public static void main(String[] args) throws IOException { Scanner kb = new Scanner(System.in); PrintStream out = new PrintStream(new File("sentence.txt")); int i = 1; while (true) { System.out.print("Sentence: "); String s = kb.nextLine().toUpperCase(); if (s.trim().equalsIgnoreCase("stop")) break; out.println(i + ": " + s); i++; } System.out.println("File is saved"); out.close(); } } โจทย์ข้อที 11 [ระดับยาก] import java.util.Scanner; import java.io.*; public class ReverseTextFile { public static void main(String[] args) throws IOException { Scanner in = new Scanner(new File("text.txt")); PrintStream out = new PrintStream(new File("revtext.txt")); while (in.hasNext()) { String s = in.nextLine(); String rev = ""; for (int i = s.length() - 1; i >= 0; i--) { rev += s.substring(i, i + 1); } out.println(rev); } in.close(); out.close(); } } © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)