1. Length of the string
String str = new String ("ABCD");
int length = Str.length ();
2. Length of the array
2.1 for a[][] A.length represents the number of rows of a a[i].length represents a column count of 2.2 for a[] A.length represents the length of a
3. Conversion of strings and character arrays
String str = new String ("ABCD");
char [] a = Str.tochararray ();
4. Conversion of string numbers and numbers
4.1 String->int
String str = "1234";
int i = integer.parseint (str); An exception may be thrown, (if an error) is appended to main (string[] args) with throws Exception
4.2 int->string
int i=235;
String s = string.valueof (i); of uppercase
5. Importing data from an. in file
Import java. io.*;
public static void Main (String [] args) throws IOException{//throws an exception
FileReader a = new FileReader ("d-small-attempt1.in");//file and source in the same directory
BufferedReader read = new BufferedReader (a);
String textline= "";
String str= "";
while ((Textline=read.readline ())!=null) {
Str+=textline+ "";
}
String[] numbersarray=str.split(""); Separates Str into a character array by a space
}
6. Guide data to a. txt file
Import java. io.*;
FileWriter FW = new FileWriter ("output.txt");
BufferedWriter BUFW = new BufferedWriter (FW);
String line = null;
for (int i=1;i<n+1;i++) {
Bufw.write ("Case #" +i+ ":" +result[i]);
bufw.newline (); Next line
Bufw.flush (); Update
}
Bufw.close ();
7. Reserved limited decimal places
Import java.text.*
DecimalFormat sim = new DecimalFormat ("0.000000");//reserved How many digits after the decimal point there are several
Double six = Sim.format (source); Keep Source Six decimal places
A simpler approach (preserving a limited number of decimal places for output results)
System.out.printf ("%.3f", a);
ACM Contest Java Programming Summary