Java编程思想第13章部分答案

5、

package ch13;
import java.util.*;
public class Explain {
int a;
long b;
float c;
double d;
String e;
static String f="3 400 2.1 2.2 hi";
Explain(String s){
	Scanner std=new Scanner(s);
	a=std.nextInt();
	b=std.nextLong();
	c=std.nextFloat();
	d=std.nextDouble();
	e=std.next();
	}
public String toString() {
	return a+""+b+""+c+""+d+""+e;
}
public static void main(String[] args){
Explain ex=new Explain(f);
System.out.println(ex);
}
}

6、

package ch12;
import java.util.*;
public class Opq {    
	int c1=3;
    long c2=40;
    float c3=14f;
    double c4=15.2f;
public String toString() {
	return String.format("c1 is %d,c2 is %d,c3 is %f,c4 is %f",c1,c2,c3,c4);
}
public static void main(String[] args) {
Opq o=new Opq();
System.out.println(o);
}
}

7、

package ch12;
import java.util.*;
public class Opq {
static boolean tp(String sen){
	return sen.matches("[A-Z]+.*[\\.]");
}
public static void main(String[] args) {
	System.out.println(tp("Adff."));
	System.out.println(tp("Rdff."));
	System.out.println(tp("sffh"));
}
}

8、

package ch12;
import java.util.*;
public class Opq {
static String knights="Then,when you have found the shrubbery,"
		+ "you must cut down the mightiest tree in the forest..."+
		"with... a herring!";
public static void split(String regex) {
	System.out.println(Arrays.toString(knights.split(regex)));
}
public static void main(String[] args) {
	split("(the|you)");
}
}

9、

package ch12;
import java.util.*;
public class Opq {
static String knights="Then,when you have found the shrubbery,"
		+ "you must cut down the mightiest tree in the forest..."+
		"with... a herring!";
public static void split(String regex) {
	System.out.println(Arrays.toString(knights.split(regex)));
}
public static void main(String[] args) {
	System.out.println(knights.replaceAll("(A|E|I|O|U|a|e|i|o|u)", "_"));
}
}

13、

package ch12;
import java.util.regex.*;

public class StartEnd {
public static String POEM=
"Twas brillig, and the slithy toves\n"+
"Did gyre and gimble in the wabe.\n"+
"All mimsy were the borogoves,\n"+
"And the mome raths outgrade.\n\n"+
"Beware the Jabberwock, my son,\n"+
"The jaws that bite, the claws that catch.\n"+
"Beware the Jubjub bird, and shun\n"+
"The frumious Bandersnatch.";
private static class Display{
	private boolean regexPrinted=false;
	private String regex;
	Display(String regex){this.regex=regex;}
	void display(String message) {
		if(!regexPrinted) {
			System.out.println(regex);
			regexPrinted=true;
		}
		System.out.println(message);
	}
}
static void examine(String s,String regex) {
	Display d=new Display(regex);
	Pattern p=Pattern.compile(regex);
	Matcher m=p.matcher(s);
	while(m.find())
		d.display("find()'"+m.group()+"'start="+m.start()+"end="+m.end());
	if(m.lookingAt())
		d.display("lookingAt() start="+m.start()+"end="+m.end());
	if(m.matches())
		d.display("matches() start="+m.start()+"end="+m.end());
}
public static void main(String[] args) {
	for(String in:POEM.split("\n")) {
		System.out.println("input:"+in);
		for(String regex:new String[] {"\\w*ere\\w*","\\w*e","The.*"})
			examine(in,regex);
	}
}
}

14、

package ch12;
import java.util.regex.*;
import java.util.*;
public class SplitDemo {
public static void main(String[] args) {
	String input="This!!unusual use!!of exclamation!!points";
	//System.out.println(Arrays.toString(Pattern.compile("!!").split(input)));
	//System.out.println(Arrays.toString(Pattern.compile("!!").split(input,3)));
	System.out.println(Arrays.toString(input.split("!!")));
	System.out.println(Arrays.toString(input.split("!!",3)));
}
}

15、

package ch12;
import java.util.regex.*;
import net.mindview.util.*;
public class JGrep {
public static void main(String[] args) throws Exception{
	if(args.length<2) {
		System.out.println("Usage:java JGrep file regex");
		System.exit(0);
	}
	//String ha=args[1];
	Pattern p=Pattern.compile(args[1],Pattern.CASE_INSENSITIVE|Pattern.MULTILINE);	
	int index=0;
	Matcher m=p.matcher("");
	String line2="";
	for(String line:new TextFile(args[0])) {
		line2=line2+"\n"+line;
	}
	//System.out.println(line);
	//String line=String(new TextFile(args[0]));
		m.reset(line2);
		while(m.find())
			System.out.println(index++ +":"+m.group()+":"+m.start());
}
}

17、

package ch13;
import java.util.regex.*;
import net.mindview.util.*;
/*show it
for everybody
 */
//i want to look it
/*
*send to show
 */
//again
public class Explain {
public static void main(String[] args) throws Exception{
	String s=TextFile.read("src/ch13/Explain.java");
	Matcher m1=Pattern.compile("/\\*(.*?)\\*/",Pattern.DOTALL).matcher(s);
	while(m1.find())
		System.out.println(m1.group(1));
	Matcher m2=Pattern.compile("//(.*)").matcher(s);
	while(m2.find())
		System.out.println(m2.group(1));
}
}

19、

package ch13;
import java.util.regex.*;
import net.mindview.util.*;
import java.io.*;
/*"what" "and" "haow"

 */
public class Explain {
class Ex{}
public static void main(String[] args) throws Exception{
	if(args.length < 1) {
		System.out.println("Usage: fileName");
		System.exit(0);
	}
	Pattern p = Pattern.compile("class (\\w+)");
	// Iterate through the lines of the input file:
	int index = 0;
	Matcher m = p.matcher(""); // creates emtpy Matcher object
	for(String line : new TextFile(args[0])) {
		m.reset(line);
		while(m.find())
			System.out.println(index++ + ": " + m.group(1));
	}
}
}

20、

package ch13;
import java.util.*;
public class Explain {
int a;
long b;
float c;
double d;
String e;
static String f="3 400 2.1 2.2 hi";
Explain(String s){
	Scanner std=new Scanner(s);
	a=std.nextInt();
	b=std.nextLong();
	c=std.nextFloat();
	d=std.nextDouble();
	e=std.next();
	}
public String toString() {
	return a+""+b+""+c+""+d+""+e;
}
public static void main(String[] args){
Explain ex=new Explain(f);
System.out.println(ex);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

returnadsss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值