for(int I = 0; I & str . length(); i++)
{
char temp _ char = str . charat(I);
//Method of converting characters into numbers 1
int temp _ int = temp _ char-' 0 ';
//Method 2 for converting characters into numbers
int temp _ int = integer . parse int(string . value of(temp _ char));
}
The first method: through charT (I), change every bit of the string into CHAR type, and then subtract the character 0(temp_char-'0') from the current character to get the int value of the current character.
The second method: convert characters into strings and then into int.
2. Split the string into one bit.
The first method: after Charat (I) cycle;
Note: charAt(i) gets each character corresponding to the string, but it cannot be directly converted into int, and it is still an ASCII value.
The second method: char [] temp = str. tochararray ();
Note: the content in char[] is not every character of the string, but the ASCII value of each character.
Details are as follows:
Bao cjl
Import java.util.scanner;
/**
One one-dimensional code is ean 13, which is a series of numbers of 13. 13 bit is a check code, which is used to check whether the previous 12 bit is correct.
The verification method is as follows:
1 and the first 12 bits. Starting from the left, all odd numbers are added to get a number A, and all even numbers are added to get a number B..
2. multiply the number b by 3 and add it to a to get the number C.
3. Subtract the single digits of the number c with 10. If the result is not 10, the check code is the result itself. If it is 10, the check code is 0.
Please enter a number of 12 at will in the console, and then output the check code.
Author ff
/
Common class check code (
Common void check code (string str)
{
int checkCode = 0;
int a = 0; //sum of odd bits
int b = 0; //sum of even numbers
for(int I = 0; I & str . length(); i++)
{
char temp _ char = str . charat(I);
//Method of converting characters into numbers 1
int temp _ int = temp _ char-' 0 ';
//Method 2 for converting characters into numbers
//int temp _ int = integer . parse int(string . value of(temp _ char));
//system . out . println(" temp _ char = "+temp _ char ");
//system . out . println(" temp _ _ int = "+temp _ int);
If((i+ 1)%2==0)// even number
{
b+ =(int)temp _ int;
}
Else// odd bits
{
a = a+(int)temp _ int;
}
}
int c = a+b * 3;
int c _ GW = c % 10;
int d = 10-c _ GW;
//system . out . println(" a = "+a+" b = "+b+" c = "+c+" c _ GW = "+c _ GW+" d = "+d);
If (d== 10)
{
checkCode = 0;
}
other
{
checkCode = d;
}
system . out . println(" check code = "+check code ");
}
Public void input ()
{
while(true){
Scanner scanner = new scanner (system. in);
System.out.println ("Please enter a 12 digit number. . . . . 。" );
string str = scanner . nextline();
if((str . length()= = 12)& amp; & (string matching ("[0-9]+"))
{
Check code;
Break;
}
}
}
/**
param args
/
Public static void main(String[]args){
CheckCode codeVo = new CheckCode();
Codvo. input();
}
}
Running results:
Please enter a number of 12. . . . . .
1 1 1 1 1 1 1 1 1 1 1 1
Check code =6
Extended data:
Char is a basic data type, which can accommodate a single character in computer programming languages (C, c++, java, VFP, etc.). ).
Char is a data type that defines a character variable (single or string).
For example, int is plastic data, and int a = 3;; Here int is type, a is integer variable, and 3 is assignment;
Char s =' AChar is a character type, S is a character variable, and A is assigned to the variable S;
Char s[]="c program "; Char is a character type, s[] is a character array, and "C program" is a value assigned to the array.