1、Scanner中:next()和nextLine()的区别:
next()读取到空格符时就结束
nextLine()读取到enter键时就结束
例1:Scanner input = new Scanner(System.in);
String str = input.next();
System.out.println(str);
//在控制台输入hello world时,输出hello
例2:Scanner input = new Scanner(System.in);
String str = input.nextLine();
System.out.println(str);
//在控制台输入hello world时,输出hello world
还有nextInt()、nextByte()、nextShort()、nextFloat()、nextDouble()、nextLong()
2、检测回文串
我呢,可能第一时间想到的是用for循环:
for(int i = 0,j = str.length-1;i<str.length(),j>0;i++,j++){
if(str[i] != str[j])
return false;
}
return true;
多麻烦哪!
用下面这种方法:
low = 0;
high = str.length()-1;
while(low<high){
if(str.charAt(low) != str.charAt(high))
return false;
low ++;
high ++;
}
return true;
学了东西就要用嘛,自己太死板了。
3、进制转换
3.1、十进制转换为十六进制
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int decimal = input.nextInt();
System.out.println(decimalToHex(decimal));
}
public static String decimalToHex(int decimal){
String hex = "";
while(decimal != 0){
int hexValue = decimal%16;
hex = toHexChar(hexValue) + hex; //这里实现了倒排,不能写成hex += toHexChar(hexValue);
decimal = decimal/16;
}
return rex;
}
public static char toHexChar(int hexValue){
if(hexValue <= 9 && hexValue >= 0)
return (char)(hexValue + '0'); //ASCII编码
else
return (char)(hexValue -10 + 'A');
}
//在控制台输入1234
//输出结果:4D2
3.2、十六进制转换为十进制
public static void main(String[] args){
Scanner input = new Scanner(System.in);
String hex = input.nextLine();
System.out.println(hexToDecimal(hex.toUpperCase()));
}
public static int hexToDecimal(String hex){
int decimalValue = 0;
for(int i = 0;i<hex.length();i++){
char ch = hex.charAt(i);
decimalValue = decimalValue * 16 +hexCharToDecimal(ch);
}
return decimalValue;
}
public static int hexCharToDecimal(char ch){
if(ch>='A' && ch<='F')
return ch+10 -'A';
else
return ch - '0';
}
//在控制台输入:4D2
//输出结果:1234
※java中提供了进制之间转换的静态方法:
①十进制转二进制:
Integer.toBinaryString(100);//1100100
②十进制转八进制:
//int decimal = 1234;
Integer.toOctalString(100);//144
③十进制转十六进制:
Integer.toHexString(100);//64
④二进制转十进制:
Integer.valueOf("1100100",2);//100
⑤八进制转十进制:
Integer.valueOf("144",8);//100
⑥十六进制转十进制:
Integer.valueOf("64",16);//100
4、字符类Character
Character是基本类型char的包装类,
它有三个实例方法:charValue()、compareTo()、equals(),charValue用于获取对象的值,后面两个用于比较。
它也有七个静态方法:
①Character.isDigit(char ch):指定字符是否是一个数字
Character.isDigit(char ch) //不是String的方法,别混淆了
②Character.isLeter(char ch):指定字符是否是一个字母
③Character.isLetterOrDigit(char ch)
④Character.isLowerCase(char ch):指定字符是否是小写字母
⑤Character.isUpperCase(char ch)
⑥Character.toLowerCase(char ch):转换成小写
在字符串中,String str = "WELCOME";
str.toLowerCase();
大小写转换在String中也有。⑦Character.toUpperCase(char ch)
例:忽略大小写统计字符串中每个字符出现的次数
步骤①:全部转换成小写字母
步骤②:int数组,大小为26,用于分别统计每个字母的次数,位置相对应
步骤③:isLetter,count计数
public static void main(String[] args){
Scanner input = new Scanner(System.in);
String string = input.nextLine();
int[] counts = countLetters(string.toLowerCase());
for(int i = 0;i<counts.length;i++){
if(counts[i] != 0)
System.out.println(" " + (char)('a' + i) + "出现了" + counts[i] + “次”);
}
}
public static int[] countLetters(String str){
int[] counts = new int[26];
for(int i = 0;i<str.length();i++){
if(Character.isLetter(str.charAt(i)))
counts[str.charAt(i) - 'a'] ++;
}
return counts;
}
5、StringBuilder/StringBuffer
String对象一旦创建,对象内容是不能改变的。而可以向StringBuffer和StringBuilder对象中添加、删除或追加新的内容。
StringBuffer类和StringBuilder类与String类相似,但更灵活。能用String的地方都能用StringBuffer和StringBuilder。
5.1、StringBuffer与StringBuilder的区别:
StringBuffer:线程同步(即线程安全),可以处理多任务并发访问
StringBuilder:线程不同步(即线程不安全),处理单任务访问
除了这些外,两者是 一样的,二者可以替换,方法也是一样的。
5.2、以StringBuilder来学习:
三种构造方法来创建字符串生成器:
修改字符串生成器中的值:StringBuilder sb = new StringBuilder(); //容量默认16
StringBuilder sb = new StringBuilder(30); //30为容量
StringBuilder sb = new StringBuilder(“welcome to java”);
append():追加新内容(可以是基本数据类型的值,也可以是String类型值)
StringBuilder sb = new StringBuilder();
sb.append("welcome to java"); //也可以一个一个进行追加
System.out.print(sb.toString()); //显示字符串生成器的内容用sb.toString()
insert():插入新内容(可以是基本数据类型的值,也可以是String类型值)
StringBuilder sb = new StringBuilder();
sb.append("welcome to java ");
sb.insert(11," html and "); //结果为:welcome to html and java//在下表11处之前插入
char[] ch = {'h','a','o'};
sb.insert(26,ch);
System.out.print(sb.toString()); //结果为:welcome to html and java hao
delete():删除指定下标处的值
reverse():将字符串生成器中的值反转,即从后往前输出
replace():替换字符串生成器中的某一段值
setCharAt():设置特定下标处的某一个字符值
还有一些其他的方法,就不列举了
5.3、使用StringBuilder来检测回文串
说明:忽略既非字符又非字母的字符
public static void main(String[] args){
Scanner input = new Scanner(System.in);
String str = input.nextLine();
System.out.print(isPalindrome(str));
}
public static boolean isPalindrome(String str){
String string1 = filter(str);
String string2 = reverse(string1);
if(string2.equals(string1))
return false;return true;
}
public static String filter(String str){
StringBuilder sb = new StringBuilder();
int i = 0;
while(i<str.length()){
if(Character.isLetterOrDigit(str.charAt(i)))
i++;sb.append(str.charAt(i));
}
return sb.toString();
}
public static String reverse(String str){
StringBuilder sb = new StringBuilder(str);
sb.reverse();
return sb.toString();
}
6、文件类File以及I/O流
6.1、File类是文件名及其目录路径的一个包装类,提供了一些基本的方法,如
exists()、isDirectory()、isFile()、getName()、getPath()等
创建File对象:
File file = new File("c:\\book\\welcome.java");
File对象只是封装了文件或路径的属性,但它并没有创建文件的方法,也没有读写文件内容的方法。使用I/O类来常见对象完成I/O操作。
6.2、I/O类:
写数据:PrintWriter类:创建文件并通过print()方法写入数据到文件中
public static void main(String[] args) throws FileNotFoundException{
File file = new File("F:\\file\\age.txt");
if(file.exists()){
System.out.println("已存在");
System.exit(0);
}
PrintWriter output = new PrintWriter(file);
output.print("Alice ");
output.print(20);
output.close();
}
读数据:Scanner类:nextInt()、nextByte()、nextShort()、nextFloat()、nextDouble()、nextLong()
既可以在控制台读数据,也可以在文件读数据。
//在写文件的时候已经往age.txt文件中写入了一行数据,直接读取
public static void main(String[] args) throws FileNotFoundException{
File file = new File("F:\\file\\age.txt");
Scanner input = new Scanner(file);
while(input.hasNext()){
String name = input.next(); //Alice
int age = input.nextInt(); //20
}
input.close();
}
nextInt()、nextByte()、nextShort()、nextFloat()、nextDouble()、nextLong()和next()都是令牌读取方式,读取用分隔符分隔开的令牌(数据),默认是空格为分隔符。