// // File: /home/cs255001/demo/atoi/JavaInput_4.java // // Read in 1 character and add 1 to the input // import java.util.Scanner; public class ConvStr1Digit { public static void main(String[] args) throws Exception { Scanner in = new Scanner(System.in); String s = null; int x = 0; while ( true ) { System.out.print("Enter input: "); s = in.next( ); // Read a String from key board if ( s.length() > 1 ) { System.out.println("Error: you must type 1 digit !"); System.out.println("Try again\n"); continue; } System.out.println("ASCII codes (in decimal) = " + (int) s.charAt(0) ); x = s.charAt(0) - '0'; // '0' = 48, so we subtract 48 System.out.println("integer value x = " + x); /* ============================================================= Proof this is an integer that you can use in computation ============================================================= */ x = x + 1; System.out.println("\nPerforming a calculation with the output:"); System.out.println("After adding 1, x = " + x + "\n"); } } }