// // File: /home/cs255001/demo/atoi/JavaInput_4.java // // Read in negative number of 1 digit and add 1 to the input // import java.util.Scanner; public class ConvStr1DigitNeg { 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() != 2 || s.charAt(0) != '-' ) { System.out.println("Error: you must enter a 1 digit neg number !"); System.out.println("Try again\n"); continue; } System.out.println("ASCII codes (in decimal) = " + (int) s.charAt(1) ); x = s.charAt(1) - '0'; // '0' = 48, so we subtract 48 x = -x; // Get the negative value 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"); } } }