Day27:枚举enum、包装类、字符操作、StringBuffer练习、Date及Calendar时间类、文件操作练习

枚举enum练习

package com.biubiubiu.demo01enum;

public enum Genders {,}

package com.biubiubiu.demo01enum;

public class Student {
	public Genders sex;
	
	public static void main(String[] args) {
		Student stu = new Student();
		stu.sex=Genders.;
	}
}

包装类介绍以及常用方法练习

package com.biubiubiu.demo02baozhuang;
/**
 * 包装类介绍
 * @author 11142
 *
 */
public class Demo01 {
	public static void main(String[] args) {
		//所有包装类都可将与之对应的基本数据类型作为参数,来构造它们的实例
		Integer i=new Integer(34);
		Double d=new Double(98.7);
		Boolean b=new Boolean(true);
		Character c=new Character('a');
		System.out.println(i+"\t"+d+"\t"+b+"\t"+c);
		
		//除Character类外,其他包装类可以一个字符串为参数构造它们的实例
		//编译错误
		//Character c2=new Character("a");
		Integer i2=new Integer("34");
		Double d2=new Double("98.7");
		Boolean b2=new Boolean("true");
		System.out.println(i2+"\t"+d2+"\t"+b2);
		
		//Boolean类构造方法参数为String类型时,若该字符串内容为true(不考虑大小写),则该Boolean对象表示true,否则表示false
		Boolean b3=new Boolean("TRue");
		Boolean b4=new Boolean("false");
		Boolean b5=new Boolean("love");
		System.out.println(b3+"\t"+b4+"\t"+b5);
		
		//当包装类构造方法参数为String 类型时,字符串不能为null,且该字符串必须可解析为相应的基本数据类型的数据,否则编译通过,运行时NumberFormatException异常
		Integer i3=new Integer(null);
		Double d4=new Double("包装类");
		System.out.println(i3+"\t"+d4);
	}

}

package com.biubiubiu.demo02baozhuang;
/**
 * 包装类常用方法
 * @author 11142
 *
 */
public class Demo02 {
	public static  void main(String[] args){
		//XXXValue():包装类转换成基本类型
		Integer integerId=new Integer(25);
		int intId=integerId.intValue();
		System.out.println(intId);
		
		Boolean booleanVal=new Boolean(true);
		boolean bool2=booleanVal.booleanValue();
		System.out.println(bool2);
		System.out.println("*************************");
		
		//toString():以字符串形式返回包装对象表示的基本类型数据
		String sex=Character.toString('男');
		String id=Integer.toString(89);
		System.out.println(sex);
		System.out.println(id);
		String sex2='男'+"";
		String id2=89+"";
		System.out.println(sex2);
		System.out.println(id2);
		System.out.println("*************************");
		
		//parseXXX():把字符串转换为相应的基本数据类型数据(Character除外)
		int i=Integer.parseInt("89");
		System.out.println(i);
		//boolean flag=Boolean.parseBoolean("true");
		//boolean flag=Boolean.parseBoolean("TRue");
		//boolean flag=Boolean.parseBoolean("love");
		boolean flag=Boolean.parseBoolean("false");
		System.out.println(flag);
		System.out.println("*************************");
		
		//所有包装类valueOf(type value):基本类型->包装类
		Integer intValue=Integer.valueOf(21);
		System.out.println(intValue);
		Boolean bool=Boolean.valueOf(false);
		System.out.println(bool);
		System.out.println("*************************");
		
		//除Character类外,其他包装类valueOf(String s):字符串->包装类
		intValue=Integer.valueOf("32");
		//bool=Boolean.valueOf("true");
		bool=Boolean.valueOf("love");
		//编译错误
		//Character c=Character.valueOf("a");
		System.out.println(intValue);
		System.out.println(bool);
		System.out.println("*************************");
		
		
		
		//基本类型和包装类的自动转换:装箱和拆箱
		//装箱
		Integer intObject=5;
		//拆箱
		int intValue2=intObject;
		System.out.println(intObject+"\t"+intValue2);
	}
}

案例练习字符串操作

package com.biubiubiu.demo03Verify;

import java.util.Scanner;

/**
 * 作业提交 
 * 字符串提取:indexOf() lastIndexOf() substring(begin) substring(begin,end)
 * concat():字符串连接(追加)
 * split():字符串拆分(分割字符串)
 * @author 11142
 *
 */
public class Demo01 {
	public static void main(String[] args) {
		// 声明变量
		boolean fileCorrect = false; // 标识文件名是否正确
		boolean emailCorrect = false; // 标识E-mail是否正确
		System.out.print("---欢迎进入作业提交系统---");
		Scanner input = new Scanner(System.in);
		System.out.println("请输入Java文件名: ");
		String fileName = input.next();
		System.out.print("请输入你的邮箱:");
		String email = input.next();

		// 检查Java文件名
		int index = fileName.lastIndexOf("."); // "."的位置
		if (index != -1 && index != 0 && fileName.substring(index + 1, fileName.length()).equals("java")) {

			fileCorrect = true; // 标识文件名正确
		} else {
			System.out.println("文件名无效。");
		}
		// 检查你的邮箱格式
		if (email.indexOf('@') != -1 && email.indexOf('.') > email.indexOf('@')) {
			emailCorrect = true; // 标识E-mail正确
		} else {
			System.out.println("E-mail无效。");
		}
		// 输出检测结果
		if (fileCorrect && emailCorrect) {
			System.out.println("作业提交成功!");
		} else {
			System.out.println("作业提交失败!");
		}
	}
}

StringBuffer小练习

package com.biubiubiu.demo04StringBuffer;

import java.util.Scanner;

/**
 * StringBuffer小练习
 * 
 * @author 11142
 *
 */
public class TestInster {
	/**
	 * 每隔三位插入逗号
	 * 
	 */
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		// 接收数字串,存放于StringBuffer类型的对象中
		System.out.print("请输入一串数字: ");
		String nums = input.next();
		StringBuffer str = new StringBuffer(nums);
		// 从后往前每隔三位添加逗号
		for (int i = str.length() - 3; i > 0; i = i - 3) {
			str.insert(i, ',');
		}
		System.out.print(str);
	}
}

Date时间类

package com.biubiubiu.demo05Date;

import java.text.SimpleDateFormat;

/**
 * 获取当前时间,并按格式输出
 * 
 * @author 11142
 *
 */
public class Date {
	public static void main(String[] args) {
		Date date = new Date(); // 创建日期对象
		SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 定制日期格式
		System.out.println("当前时间为:" + formater.format(date));
	}
}

Calendar日期类

package com.biubiubiu.demo06Calendar;

import java.util.Calendar;

/**
 * 日期类的使用
 * @author 11142
 *
 */
public class Test {
	public static void main(String[] args) {
		Calendar t = Calendar.getInstance();
		System.out.println("今天是"+t.get(Calendar.YEAR)+"年"
				+(t.get(Calendar.MONTH)+1)+"月"+t.get(Calendar.DAY_OF_MONTH)+"日");
		//Calendar.DAY_OF_WEEK 中 Sunday是1
		System.out.println("今天是星期"+(t.get(Calendar.DAY_OF_WEEK)-1));
	}
}

文件操作:创建文件、查看文件相关信息、删除文件

package com.biubiubiu.demo07File;

import java.io.File;
import java.io.IOException;

/**
 * 文件操作:创建文件、查看文件相关信息、删除文件
 * @author 11142
 *
 */
public class FileDemo {
	public static void main(String[] args) {
		FileDemo fileDemo = new FileDemo();
//		File file = new File("d:\\11\\test.txt");
		File file = new File("test.txt");
//		fileDemo.create(file);
//		fileDemo.showFileInfo(file);
		fileDemo.delete(file);
	}
	
	//创建文件
	public void create(File file) {
		if(!file.exists()) {
			try {
				file.createNewFile();
				System.out.println("文件已创建!");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	//查看文件相关信息
	public void showFileInfo(File file) {
		if(file.exists()) {
			//如果文件存在,则查看其信息
			if(file.isFile()) {
				//是文件
				System.out.println("该文件名:" + file.getName());
				System.out.println("相对路径:" + file.getPath());
				System.out.println("绝对路径:" + file.getAbsolutePath());
				System.out.println("文件大小是:" + file.length() + "字节");
				
			}
			if(file.isDirectory()) {
				//是目录
				System.out.println("此文件是目录!");
			}
		}else {
			System.out.println("文件不存在!");
		}
	}
	
	//删除文件
	public void delete(File file) {
		if(file.exists()) {
			file.delete();
			System.out.println("文件已删除~");
		}
	}
	
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值