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;
    }
}

 

posted on 2013-02-14 21:59  bailun  阅读(1114)  评论(0编辑  收藏  举报