java练习程序1

 /*import javax.swing.JFrame;*/ /*import javax.swing.JOptionPane;*/ public class TestFrame{ public static void main(String[] args) throws Exception{ /*JFrame frame1 = new JFrame(); frame1.setTitle("Windows 1"); frame1.setSize(200,150); frame1.setLocation(200,100); frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame1.setVisible(true); JFrame rx = new JFrame(); rx.setTitle("rx's Windows"); rx.setSize(300,300); rx.setLocation(0,0); rx.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); rx.setVisible(true);*/ /*String s = "Welcome to java"; String s1 = new String("Welcome to java"); String s2 = s1.intern(); String s3 = "Welcome to java"; System.out.println("s1 == s is" + (s1 == s)); System.out.println("s2 == s is" + (s2 == s)); System.out.println("s.intern() == s3.intern() is" + (s.intern() == s3.intern())); String s4 = "Welcome"; String s5 = "j a v a"; System.out.println(); System.out.println(s4.toLowerCase()); System.out.println(s4.toUpperCase()); System.out.println(s5.trim()); System.out.println(s4.replace('e','A')); System.out.println(s4.replaceAll("e","E"));*/ /*System.out.println("Welcome to java".indexOf('W')); System.out.println("Welcome to java".indexOf('o')); System.out.println("Welcome to java".indexOf('o',5)); System.out.println("Welcome to java".indexOf("come")); System.out.println("Welcome to java".indexOf("java",5)); System.out.println("Welcome to java".indexOf("Java",5)); System.out.println("Welcome to java".lastIndexOf('W')); System.out.println("Welcome to java".lastIndexOf('o')); System.out.println("Welcome to java".lastIndexOf('o',5)); System.out.println("Welcome to java".lastIndexOf("come")); System.out.println("Welcome to java".lastIndexOf("java",5)); System.out.println("Welcome to java".lastIndexOf("Java",5));*/ /*char[] chars = "Java".toCharArray(); getChars(int srcBegin,int srcEnd,char[] dst,int dstBegin)*/ /*String str = String.valueOf(new char[]{'j','a','v','a'}); System.out.println(str);*/ /*检测回文串*/ /**Main method*/ /*String s = JOptionPane.showInputDialog("Enter a string"); //Declare and initalize output string String output = ""; if(isPalindrome(s)) output = s + " is a palindrome"; else output = s + "is not a palindrome"; //Display the result JOptionPane.showMessageDialog(null,output); } /**Check if a string is a palindrome*/ /*public static boolean isPalindrome(String s){ //The index of the first character in the string int low = 0; //The index of the last character in the string int high = s.length() - 1; while(low < high){ if (s.charAt(low) != s.charAt(high)) return false; low ++; high --; } return true;*/ /*统计字符串中的每个字母*/ /**Main method*/ //Prompt the user to enter a string /*String s = JOptionPane.showInputDialog("Enter a string :"); //Invoke the countLetters method to count each letter int[] counts = countLetters(s.toLowerCase()); //Declare and intialize output string String output =""; //Display results for(int i =0; i < counts.length; i++){ if (counts[i] != 0) output += (char)('a' + i) + " appear " + counts[i] + ((counts[i] ==1)?" time/n":" times/n"); } //Display the result JOptionPane.showMessageDialog(null,output); } //Count each letter in the string 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)))//declare the char whether is a letter counts[s.charAt(i)-'a']++;//the place of char } return counts;*/ //如果是多任务访问,使用StringBuffer。如果是单任务访问,StringBuilder,会更有效。 //StringBuffer() //StringBuffer(capacity:int) //StringBuffer(str:string) //+append(data:char[]):StringBuffer //+append(data:char[],offset:int,len:int):StringBuffer //+append(str:string):StringBuffer //+capacity():int //+charAt(index:int):char //+delete(startindex:int,endindex:inr):StringBuffer //+deleteCharAt(index:int):StringBuffer //+insert(offset:int,data:char[]):StringBuffer /**Main method*/ /*String s = JOptionPane.showInputDialog("Enter a string"); //Declare and initialize output string String output = "Ignoring nonalphanumeric characters,/nis" + s + " a palindrome? " + isPalindrome(s); //Display the result JOptionPane.showMessageDialog(null,output); } /**Return true if a string is a palindrome*/ /*public static boolean isPalindrome(String s){ //Create a new string by eliminating nonalphanumeric chars String s1 = filter(s); //Create a new string that is the reversal of s1 String s2 =reverse(s1); //Compare if the reversal is the name as the original string return s2.equals(s1); } /**Create a new string by eliminating nonalphanumeric chars*/ /*public static String filter(String s){ //Create a string buffer StringBuffer strBuf = new StringBuffer(); //Examine each char in the string to skip alphanumberic char for(int i = 0;i <s.length();i++){ if(Character.isLetterOrDigit(s.charAt(i))){ strBuf.append(s.charAt(i)); } } //Return a new filtered string return strBuf.toString(); } /**Create a new string by reversing a specified string*/ /*public static String reverse(String s){ StringBuffer strBuf = new StringBuffer(s); strBuf.reverse(); return strBuf.toString();*/ /*System.out.println("Java is fun".matches("Java.*"));*/ /*System.out.println("Java Java Java".replaceAll("v//w","wi"));*/ /*String[] tokens = "Java1HTML2Perl".split("//d"); System.out.println(tokens);*/ /*PrintWriter类可用来向文本中写数据*/ /*java.io.File file = new java.io.File("score.txt"); if(file.exists()){ System.out.println("File already exists"); System.exit(0); } //Create a file java.io.PrintWriter output = new java.io.PrintWriter(file); //Write formatted output to the file output.print("John T Smith "); output.println("90"); output.print("Eric K Jones "); output.print(85); //Close the file output.close();*/ //use Scanner to input numbers or chars java.io.File file = new java.io.File("score.txt"); java.util.Scanner input = new java.util.Scanner(file); //Read data from a 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); } //close the file input.close(); } }

你可能感兴趣的:(java,String,File,filter,character,output)