Java学习问题解决方法积累

1 字符串转化为数字

1.1纯数字字符串转化为数字

Integer.valueOf函数:

int  number  = Integer.valueOf("123456");

异常处理:

public static void main(String[] args) {
		try {
			int number  = Integer.valueOf("123.456"); //待转化字符串为浮点数
			System.out.println(number);
		}
		catch(NumberFormatException ae) { 
			System.err.println("数字异常:"+ae);	
		}
/*输出:
数字异常:java.lang.NumberFormatException: For input string: "123.456"
*/
1.2 字符类型字符串转换为整数(ASCII码)
String a = "ABC";
	        //将String对象中的每一个转化成字符类型
	        char[] b = a.toCharArray();
	        //转换成ASCLL
	        for (char c : b) {
	        	System.out.println(Integer.valueOf(c));
	        }		

2.转义字符表

在这里插入图片描述

3.分割字符串

string.split();

public static void main(String[] args) {
		
		String a= "12	34	56	23	34";    //每个数字之间一个Tab键
		String b[]=a.split("\t");
		System.out.println(b.length);
		for(String i : b) {
			System.out.print(i+",");
		}
	}	
/*输出:
5
12,34,56,23,34,
*/

4、编译时出现:Cannot make a static reference to the non-static method

当在main中调用以下函数:

public class MagicSquare {
     boolean get()
    {
    	System.out.println("wrong");
        return false;
    }
    public static void main(String[] args)
    {
    	boolean a = get();
       System.out.print(a);
    }
}

出现
Exception in thread “main” java.lang.Error: Unresolved compilation problem:
Cannot make a static reference to the non-static method get() from the type MagicSquare
原因:main为public static,调用了非static函数,改为以下

public class MagicSquare {
     public static boolean get()
    {
    	System.out.println("wrong");
        return false;
    }
    public static void main(String[] args)
    {
    	boolean a = get();
       System.out.print(a);
    }
}

5、LineNumberReader读取行数时易错点

  • 一定要加上*lineNumberReader.skip(Long.MAX_VALUE);*跳过最大行数,直接到最后一行。
FileReader file = new FileReader("src\\txt\\1.txt");
LineNumberReader lineNumberReader = new LineNumberReader(file);
lineNumberReader.skip(Long.MAX_VALUE);
int line = lineNumberReader.getLineNumber() + 1;
  • 执行完以上代码后,如果readLine(),则不会读取任何文件中的内容。因为上述代码执行完后,已经读取到文件最后一个换行符\n,因此不能接着读取文件信息。

6、try的分割作用

  • 在try{}中定义的变量,在try{}外不能使用:
public class MagicSquare{//类名
   public static void main(String[] args){
       try {
       	int i = 2;
       }catch(NumberFormatException e) {
       	System.err.print(e);
       }
       
       System.out.println(i);
   }
}

出现问题:i cannot be resolved to a variable

  • i定义在try{}前但未初始化
public class MagicSquare{//类名
    public static void main(String[] args){
    	int i;
        try {
           i = 2;
        }catch(NumberFormatException e) {
        	System.err.print(e);
        }
        
        System.out.println(i);
    }
}

出错:The local variable i may not have been initialized

  • 正确代码:
public class MagicSquare{//类名
    public static void main(String[] args){
    	int i=1;
        try {
           i = 2;
        }catch(NumberFormatException e) {
        	System.err.print(e);
        }
        
        System.out.println(i);
    }
}
/*输出:
28
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值