Java语言程序设计-基础篇-第八版-第九章

package chapter9字符串和文本IO;

import java.util.Scanner;

public class PalindromeIgnoreNonAlphanumeric {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String s = input.nextLine();
        System.out.println("Ignoring non-alphanumeric characters, \nis " + s
                + " a palindrome? " + isPalindrome(s));
    }

    public static boolean isPalindrome(String s) {
        String s1 = filter(s);
        String s2 = reverse(s1);
        return s2.equals(s1);
    }

    public static String filter(String s) {
        StringBuilder stringBuilder = new StringBuilder();

        for (int i = 0; i < s.length(); i++) {
            if (Character.isLetterOrDigit(s.charAt(i))) {
                stringBuilder.append(s.charAt(i));
            }
        }
        return stringBuilder.toString();
    }

    public static String reverse(String s) {
        StringBuilder stringBuilder = new StringBuilder(s);
        stringBuilder.reverse();
        return stringBuilder.toString();
    }
}
package chapter9字符串和文本IO;

import java.util.Scanner;

public class ReadData {
    public static void main(String[] args) throws Exception {
        java.io.File file = new java.io.File("scores.txt");
        Scanner input = new Scanner(file);

        while (input.hasNext()) {
            String firstName = input.next();
            String mi = input.next();
            String lastName = input.next();
            int score = input.nextInt();
            System.out.println(firstName + " " + mi + " " + lastName + " "
                    + score);
        }
        input.close();
    }
}
package chapter9字符串和文本IO;
import java.util.Scanner;
import javax.swing.JFileChooser;
public class ReadFileUsingJFileChooser {
    public static void main(String[] args) throws Exception {
        JFileChooser fileChooser = new JFileChooser();
        
        if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
            java.io.File file = fileChooser.getSelectedFile();
            Scanner input = new Scanner(file);
            
            while (input.hasNext()) {
                System.out.println(input.nextLine());
            }
            input.close();
        }
        else {
            System.out.println("No file selected");
        }
    }
}
package chapter9字符串和文本IO;

import java.io.*;
import java.util.*;

public class ReplaceText {
    public static void main(String[] args) throws Exception {
        if (args.length != 4) {
            System.out
                    .println("Usage: java ReplaceText sourceFile targetFile oldStr newStr");
            System.exit(0);
        }

        File sourceFile = new File(args[0]);
        if (!sourceFile.exists()) {
            System.out.println("Source file " + args[0] + " does not exist");
            System.exit(0);
        }

        File targetFile = new File(args[1]);
        if (targetFile.exists()) {
            System.out.println("Target file " + args[1] + " already exists");
            System.exit(0);
        }
        
        Scanner input = new Scanner(sourceFile);
        PrintWriter output = new PrintWriter(targetFile);
        
        while (input.hasNext()) {
            String s1 = input.nextLine();
            String s2 = s1.replaceAll(args[2], args[3]);
            output.println(s2);
        }
        
        input.close();
        output.close();
    }
}
package chapter9字符串和文本IO;

public class TestFileClass {
    public static void main(String[] args) {
        java.io.File file = new java.io.File("image/baby.png");
        System.out.println("Does it exist? " + file.exists());
        System.out.println("The file has " + file.length() + " byters ");
        System.out.println("Can it be read? " + file.canRead());
        System.out.println("Can it be written? " + file.canWrite());
        System.out.println("Is it a directory? " + file.isDirectory());
        System.out.println("Is it a file? " + file.isFile());
        System.out.println("Is it absolute? " + file.isAbsolute());
        System.out.println("Is it hidden? " + file.isHidden());
        System.out.println("Absolute path is " + file.getAbsolutePath());
        System.out.println("Last modified on "
                + new java.util.Date(file.lastModified()));

    }
}
package chapter9字符串和文本IO;

public class WriteData {
    public static void main(String[] args) throws Exception {
        java.io.File file = new java.io.File("scores.txt");
        if (file.exists()) {
            System.out.println("File already exists");
            System.exit(0);
        }
        java.io.PrintWriter output = new java.io.PrintWriter(file);
        output.print("John T Smith ");
        output.println(90);
        output.print("Eric K Jones ");
        output.println(85);

        output.close();
    }
}

 

package chapter9字符串和文本IO;

import java.util.Scanner;

public class CountEachLetter {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String s = input.next();
        int[] counts = countLetters(s.toLowerCase());
        for (int i = 0; i < counts.length; i++) {
            if (counts[i] != 0)
                System.out.println((char) ('a' + i) + " appears  " + counts[i]
                        + ((counts[i] == 1) ? " time" : " times"));
        }
    }

    public static int[] countLetters(String s) {
        int[] counts = new int[26];

        for (int i = 0; i < s.length(); i++) {
            if (Character.isLetter(s.charAt(i)))
                counts[s.charAt(i) - 'a']++;
        }
        return counts;
    }
}
package chapter9字符串和文本IO;
import java.util.Scanner;
public class HexToDecimalConversion {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a hex number: ");
        String hex = input.nextLine();
        
        System.out.println("The decimal value for hex number " 
                + hex + " is " + hexToDecimal(hex.toUpperCase()));
    }
    public static int hexToDecimal(String hex) {
        int decimalValue = 0;
        for (int i = 0; i < hex.length(); i++) {
            char hexChar = hex.charAt(i);
            decimalValue = decimalValue * 16 + hexCharToDecimal(hexChar);
        }
        return decimalValue;
    }
    public static int hexCharToDecimal(char ch) {
        if (ch >= 'a' && ch <= 'F')
            return 10 + ch - 'A';
        else
            return ch - '0';
    }
}

 

package chapter9字符串和文本IO;

public class Calculator {
    public static void main(String[] args) {
        if (args.length != 3) {
            System.out
                    .println("Usage: java Calculator operand1 operator operand2");
            System.exit(0);
        }
        int result = 0;
        switch (args[1].charAt(0)) {
        case '+':
            result = Integer.parseInt(args[0]) + Integer.parseInt(args[2]);
            break;
        case '-':
            result = Integer.parseInt(args[0]) - Integer.parseInt(args[2]);
            break;
        case '*':
            result = Integer.parseInt(args[0]) * Integer.parseInt(args[2]);
            break;
        case '/':
            result = Integer.parseInt(args[0]) / Integer.parseInt(args[2]);
            break;
        }
        System.out.println(args[0] + ' ' + args[1] + ' ' + args[2] + " = "
                + result);
    }
}
package chapter9字符串和文本IO;
import java.util.Scanner;
public class CheckPalindrome {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a string: ");
        String s = input.next();
        
        if (isPalindrome(s))
            System.out.println(s + " is a palindrome");
        else 
            System.out.println(s + " is not a palindrome");
    }
    public static boolean isPalindrome(String s) {
        int low = 0;
        int high = s.length() - 1;
        
        while (low < high) {
            if (s.charAt(low) != s.charAt(high))
            return false;
            
            low++;
            high--;
        }
        return true;
    }
}

 

转载于:https://www.cnblogs.com/bailun/archive/2013/02/14/2912587.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值