What is a string in a computer program:
|
public class CharAt
{
public static void main(String[] args) throws Exception
{
String s = "ABC123";
for ( int i = 0; i < s.length(); i++)
{
System.out.println( s.charAt(i) );
System.out.println( Integer.toBinaryString(s.charAt(i)) );
System.out.println( Integer.toString(s.charAt(i)) );
}
}
}
|
DEMO program: /home/cs255001/demo/atoi/CharAt.java
In CS170, you have learned this method to read integer inputs from the console (keyboard):
Scanner in = new Scanner(System.in); int x = in.nextInt( ); |
The nextInt( ) method
in Java consists of
2 method calls.
This is an
alternate way to
read integer input
in Java:
Scanner in = new Scanner(System.in); String s = in.next( ); // Read input as a String int x = Integer.parseInt(s); // Conv String to 2s compl |
The next( ) method:
|
DEMO: /home/cs255001/demo/atoi/Next.java
The parseInt( ) method:
|
DEMO: /home/cs255001/demo/atoi/parseInt.java
The parseInt( ) method is an example of a conversion method needed in computer programming that convert between different kinds of representations!!!