Java 编程中Tips积累
1、 remove与迭代同时进行出错
- 找到List中为“6.”的数并删除,如果使用
for循环进行边迭代边删除,会出错。改为迭代器,如下
List<String> subjects = ["6.023","6.02","6.9","8"];
Iterator<String> iter = subjects.iterator();
while(iter,hasNext()){
String subject = iter,next();
if(subject.startwith("6.")){
iter.remove();
}
}
2.读文件
package com;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import javax.print.DocFlavor.CHAR_ARRAY;
import com.google.common.primitives.Chars;
/*
1、首先创建FileReader对象
2、将FileReader传递给BufferedReader
3、采用BufferedReader的readLine()方法和read()方法来读取文件内容
4、最后一定要的finally语句中关闭BufferedReader15 */
public class MathYsf3{
public static void main(String[] args){
BufferedReader br = null;
BufferedReader br2 = null;
try {
br = new BufferedReader(new FileReader("C:\\Users\\91911\\Desktop\\test.txt"));
// 第一种读取文件方式
System.out.println("Reading the file using readLine() method: ");
String contentLine ;
List<String> arr1 = new ArrayList<>();
while (br.readLine() != null) {
contentLine = br.readLine();
//读取每一行,并输出
System.out.println(contentLine);
//将每一行追加到arr1
arr1.add(contentLine);
}
//输出数组
System.out.println(arr1);
// 第二种读取文件方式,read()返回的是byte,只能读数
br2 = new BufferedReader(new FileReader("C:\\Users\\91911\\Desktop\\test.txt"));
System.out.println("Reading the file using read() method: ");
int num = 0;
char ch;
while ((num = br2.read()) != -1) {
ch = (char) num;
System.out.print(ch);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
if (br2 != null) {
br2.close();
}
} catch (IOException e) {
System.out.println("Error in closing the BufferedReader");
}
}
}
}
注:当读文件为string时,采用readline(),而不是read()
3、分割字符串
- 一种方式如“ ”(空格)分割:
String str = "one two three, four";
String[] tokens = str.split(" ");
for (String s: tokens)
System.out.println(s);
- 不同方式分割," " 和 ,分割:
String str = "one tw,o th,ree fo,ur";
String[] tokens = str.split(" |,");
for (String s: tokens)
System.out.println(s);
4、读取文件得到单个字符串
BufferedReader file = new BufferedReader(new FileReader("test.txt"));
List<String> words =new ArrayList<>();
//每个字符串空格隔开
while(file.readLine() != null) {
String[] str = file.readLine().split(" ");
for(String t:str) {
words.add(t);
}
}
5.no tests found with test runner ‘JUnit5’
将import org.junit.Test; 换成import org.junit.jupiter.api.Test;
完美!
6.关于在eclipse中使用Spring+JUnit时,JUnit找不到测试类的问题
被测试类在/src中,测试类在/test中
在“Build path”中设置被测试类的输出路径为/target/classes,测试类的输出路径为/target/test-classes即可:
点击红框选定的,
7、命令行输入:
Scanner in=new Scanner(System.in);
//如果是输入字符串
String gamename = in.nextLine();
//如果是输入整数
int x = in.nextInt();
- 如何 连续进行命令行输入:
整个程序在最终结束之前使用一个Scanner对象in,并保持开放。进行命令行输入的读取
每次输入后,通过
in=new Scanner(System.in);
读取命令行输入。
将in 作为参数传递给方法。使其在具体方法中可以进行连续地获取命令行输入
在程序最终结束后,再in.close();
8、Comparable方法
- Comparable<T>类方法:
public int compareTo<T>(T o);
- 重写的compareTo方法
public class main implements Comparable{
...
...
@Override
public int compareTo(T 0){
...
}
9、字符串与整数,浮点数转化
- 字符串转化为整数,浮点数
String s = "100";
//方法一
int a = Integer.parseInt(String s);
Long.parseLong(String s);
Float.parseFloat(String s);
Double.parseDouble(String s)
//方法二
int a = Integer.valueOf(s).intValue();
- 整数转化为字符串
int i=11;
//方法一
String s=i+"";
//方法二
String s=String.valueOf(i);
//方法三
String s=Integer.toString(i);
10、限制小数点位数
- 使用DecimalFormat类
double d = 0.200;
DecimalFormat df = new DecimalFormat("0.00");
System.out.println(df.format(d));
double d = 3.123;
DecimalFormat df = new DecimalFormat("#.##");
System.out.println(df.format(d));
- 转化为String
double d = 0.6544;
String s=String.format("%.2f",d);
System.out.println(s);
- 使用BigDecimal类:若小数点后均为零,则保留一位小数,并且有四舍五入的规则
double d = 1.000;
BigDecimal bd=new BigDecimal(d);
double d1=bd.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
System.out.println(d1);