Reputation: 699
I would like to key in my nirc
number e.g. S1234567I
and then put 1234567
individualy as a integer as indiv1
as charAt(1)
, indiv2
as charAt(2)
, indiv
as charAt(3)
, etc. However, when I use the code below, I can't seem to get even the first number out? Any idea?
Scanner console = new Scanner(System.in);
System.out.println("Enter your NRIC number: ");
String nric = console.nextLine();
int indiv1 = nric.charAt(1);
System.out.println(indiv1);
Upvotes: 10
Views: 76212
Reputation: 340138
The modern solution uses Unicode code point numbers rather than the outmoded char
type.
Here is an IntStream
, a successive stream of each character’s code point number, printing each of those numbers to console:
"S1234567I"
.codePoints()
.forEach( System.out :: println )
83
49
50
51
52
53
54
55
73
Show each character along with its code point number. To convert a code point number back into a character, call Character.toString
while passing the integer: Character.toString( codePoint )
.
String s = Character.toString( 49 ) ; // Returns "1".
…and…
String s = Character.toString( 128_567 ) ; // Returns "😷" FACE WITH MEDICAL MASK.
Example:
"S1234567I".codePoints().forEach( ( int codePoint ) -> {
String message = Character.toString( codePoint ) + " → " + codePoint;
System.out.println( message );
} );
S → 83
1 → 49
2 → 50
3 → 51
4 → 52
5 → 53
6 → 54
7 → 55
I → 73
The char
type is obsolete, unable to represent even half of the 143,859 characters defined in Unicode. The char
type is a 16-bit number underneath, capable of representing a range of numbers of about ± 64,000. Unicode characters are assigned numbers along a range of about a million, too big for char
to handle.
Instead, use Unicode code point integer numbers to represent individual characters.
We can get a stream of int
primitive values (IntStream
) from a string, each number representing the Unicode code point of each successive character.
IntStream intStream = "S1234567I".codePoints() ;
Process each code point number. Here we simply print each number.
intStream.forEach( System.out :: println );
When run.
83
49
50
51
52
53
54
55
73
Or perhaps you want an array of the int
numbers.
int[] codePoints = "S1234567I".codePoints().toArray();
Dump to console.
System.out.println( "codePoints = " + Arrays.toString( codePoints ) );
codePoints = [83, 49, 50, 51, 52, 53, 54, 55, 73]
Or perhaps you want a List
object containing all those code point numbers. Here is a non modifiable list made by List.of
. We call boxed
to invoke auto-boxing feature to convert int
primitives into Integer
objects. Then a Collector
implementation gathers the output of the stream into a List
.
List < Integer > codePoints = "S1234567I".codePoints().boxed().collect( Collectors.toList() );
Explaining those parts:
List < Integer > codePoints = // Desired result is a `List` collection of `Integer` objects.
"S1234567I" // Your input string.
.codePoints() // Generate an `IntStream`, a succession of `int` integer numbers representing the Unicode code point number of each character in the `String` object.
.boxed() // Convert each `int` primitive to an `Integer` object.
.collect( // Collect the produced `Integer` objects together.
Collectors.toList() // Specify a `Collector` implementation that knows how to make a `List` object, containing our `Integer` objects.
) // Returns a `List` of `Integer` objects.
;
Perhaps you want to filter out the alphabetic characters to leave only the digits found in your input string. The Character
class offers tests such as isDigit
.
For an input of "S1234567I"
, that means dropping the S
and the I
, leaving 1234567
, producing the integer number 1,234,567.
List < Integer > codePointsOfDigitsFromInput = "S1234567I".codePoints().filter( ( int codePoint ) -> Character.isDigit( codePoint ) ).boxed().collect( Collectors.toList() );
Break that out to multiple lines.
List < Integer > codePointsOfDigitsFromInput =
"S1234567I"
.codePoints()
.filter(
( int codePoint ) -> Character.isDigit( codePoint )
)
.boxed()
.collect( Collectors.toList() );
codePointsOfDigitsFromInput = [49, 50, 51, 52, 53, 54, 55]
We can modify that code to generate a String
containing only digits taken from that input. See Question, Make a string from an IntStream of code point numbers?. And then we make an int
integer number of that text.
String numberComponentFromInput =
"S1234567I"
.codePoints()
.filter(
( int codePoint ) -> Character.isDigit( codePoint )
)
.collect( // Collect the results of processing each code point.
StringBuilder :: new , // Supplier<R> supplier
StringBuilder :: appendCodePoint , // ObjIntConsumer<R> accumulator
StringBuilder :: append // BiConsumer<R,R> combiner
)
.toString();
int x = Integer.valueOf( numberComponentFromInput );
numberComponentFromInput = 1234567
x = 1234567
Upvotes: 1
Reputation: 9581
I know question is about char to int but this worth mentioning because there is negative in char too ))
From JavaHungry you must note the negative numbers for integer if you dont wana use Character.
Converting String to Integer : Pseudo Code
1. Start number at 0
2. If the first character is '-'
Set the negative flag
Start scanning with the next character
For each character in the string
Multiply number by 10
Add( digit number - '0' ) to number
If negative flag set
Negate number
Return number
public class StringtoInt {
public static void main (String args[])
{
String convertingString="123456";
System.out.println("String Before Conversion : "+ convertingString);
int output= stringToint( convertingString );
System.out.println("");
System.out.println("");
System.out.println("int value as output "+ output);
System.out.println("");
}
public static int stringToint( String str ){
int i = 0, number = 0;
boolean isNegative = false;
int len = str.length();
if( str.charAt(0) == '-' ){
isNegative = true;
i = 1;
}
while( i < len ){
number *= 10;
number += ( str.charAt(i++) - '0' );
}
if( isNegative )
number = -number;
return number;
}
}
Upvotes: 0
Reputation: 66216
try {
int indiv1 = Integer.parseInt ("" + nric.charAt(1));
System.out.println(indiv1);
} catch (NumberFormatException npe) {
handleException (npe);
}
Upvotes: 1
Reputation: 1503539
You'll be getting 49, 50, 51 etc out - those are the Unicode code points for the characters '1', '2', '3' etc.
If you know that they'll be Western digits, you can just subtract '0':
int indiv1 = nric.charAt(1) - '0';
However, you should only do this after you've already validated elsewhere that the string is of the correct format - otherwise you'll end up with spurious data - for example, 'A' would end up returning 17 instead of causing an error.
Of course, one option is to take the values and then check that the results are in the range 0-9. An alternative is to use:
int indiv1 = Character.digit(nric.charAt(1), 10);
This will return -1 if the character isn't an appropriate digit.
I'm not sure if this latter approach will cover non-Western digits - the first certainly won't - but it sounds like that won't be a problem in your case.
Upvotes: 23