Tag Archives: java

How to convert a Java string into a number (stringtonumber)

public class StringToNumber {
    public static void main(String[] args) {
        String str = "123";

        Integer num1 = new Integer(str);

        int num2 = Integer.parseInt(str);

        Integer num3 = Integer.valueOf(str);

        System.out.println(num1+"\t"+num2+"\t"+num3);

    }
}

if it’s a single character or a string you have to cut it and convert it to char and then convert it to char otherwise it will be null pointer exception

like above

String a = "ABC";

//将String对象中的每一个下标位的对象保存在数组中

char[] b = a.toCharArray();

//转换成响应的ASCLL

for (char c : b) {

System.out.println(Integer.valueOf(c));

}

principle analysis:

Interger class constructor

directly into the source code, look at this constructor:

 
  1. public Integer(String s) throws NumberFormatException {

  2. this.value = parseInt(s, 10);

  3. }

What is

this this.value?Continue tracing source:

 private final int value;

from this, we can find a characteristic of the packaging class:

inside the wrapper, the corresponding primitive data type of the wrapper class is treated as a private attribute, and the value of the primitive data type is wrapped externally into an object of the corresponding wrapper class.

in the constructor of an Integer, we find that the parseInt (String s) method is called, and we continue our tracing, which leads us to the second method demonstrated at the beginning of this article:

parseInt (String s) method

 
  1. public static int parseInt(String s) throws NumberFormatException {

  2. return parseInt(s,10);

  3. }

  4. // a: the first method of encapsulation,

  5. // a:

  6. public static int parseInt(String s, int radix)throws NumberFormatException

valueOf (String s) method

 
  1. public static Integer valueOf(String s) throws NumberFormatException {

  2. return Integer.valueOf(parseInt(s, 10));

  3. }

The last method,

, actually calls the parseInt() method (the second)

Abstract method and static method of java interface

introduction

before JDK1.8, interface did not provide any concrete implementation. It was described in JAVA programming ideas as follows: “interface is a keyword that produces a completely abstract class. It does not provide any concrete implementation at all. It allows the author to determine the method name, argument list, and return type, but does not have any method body. The interface provides the form, not any concrete implementation.

but this limitation is broken in JDK1.8, where interfaces allow you to define default methods and static methods.

code example

defines an IHello interface

public interface IHello {
	//抽象方法
	void sayHi();
	//静态方法
	static void sayHello() {
		System.out.println("static method : say Hello!");
	}
	//默认方法
	default void sayByebye() {
		System.out.println("default method : say bye!");
	}

}

defines an IHello interface implementation class

public class HelloImpl implements IHello {
	//实现抽象方法
	@Override
	public void sayHi() {
		
		System.out.println("normal method:say hi");
		
	}
}

The

method call

public class App 
{
	public static void main(String[] args)
    {
    	HelloImpl helloImpl = new HelloImpl();
    	//对于abstract抽象方法通过实例对象来调用
    	helloImpl.sayHi();
    	//default 方法只能通过实例对象来调用
    	helloImpl.sayByebye();
    	//静态方法通过 接口名.方法名()来调用
    	IHello.sayHello();
    	
    	//接口是不允许new的,如果使用new后面必须跟上一对花括号用于实现抽象方法,这种方式被称为匿名实现类,匿名实现类是一种没有名称的实现类
    	//匿名实现类的好处:不用再单独声明一个类 缺点:由于没有名称。不能重复使用,只能使用一次
    	new IHello() {			
			@Override
			public void sayHi() {
				System.out.println("normal method;say hi 新定义");			
			}
		}.sayHi();
    }
}

normal method: say hi
default method: say bye!> static method: say Hello!
normal method: say hi new definition

Python automatically generates the requirements file for the current project

python automatically generates the requirements file for the current project

there are several ways:

1. Use PIP freeze

< pre style=”margin: 0px 0px 15px; padding: 0px; font-family: “Courier New” ! important; font-size: 12px ! important; The line – height: 1.72222; color: inherit; white-space: pre; border-radius: 6px; overflow-wrap: break-word;” > pip freeze > requirements.txt< /pre>

this way is to list the packages in the entire environment, if it is a virtual environment. In general, we only need to export the current project’s requirement.txt, and pipreqs

is recommended
2. Use pipreqs

is a helpful tool that scans the project directory to automatically discover which libraries are used, automatically generates dependency listings, and only generates project-related dependencies to requirements. TXT

installation

< pre style=”margin: 0px 0px 15px; padding: 0px; font-family: “Courier New” ! important; font-size: 12px ! important; The line – height: 1.72222; color: inherit; white-space: pre; border-radius: 6px; overflow-wrap: break-word;” > pip install pipreqs< /pre>

Use

also easy to use pipreqs pathname
here goes directly to the project root directory, so it is./
error

File "c:\users\devtao\appdata\local\programs\python\python36-32\lib\site-packages\pipreqs\pipreqs.py", line 341, in init
    extra_ignore_dirs=extra_ignore_dirs)

  File "c:\users\devtao\appdata\local\programs\python\python36-32\lib\site-packages\pipreqs\pipreqs.py", line 75, in get_all_imports
    contents = f.read()

UnicodeDecodeError: 'gbk' codec can't decode byte 0xa6 in position 186: illegal multibyte sequence

UnicodeDecodeError: ‘GBK’ codec can’t decode byte 0xa6 in position 186: illegal multibyte sequence
directly modify line 407 of pipreqs.py, change encoding to utf-8, save, and run on pipreqs./
pipreqs.py path

C:\Users\linxiao\AppData\Local\Programs\Python\Python37\Lib\site-packages\pipreqs