Advertisement
eranseg

Number Parser

Jul 21st, 2019
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.00 KB | None | 0 0
  1. Homework EXercise 2
  2. -------------------------
  3. import java.util.Scanner;
  4.  
  5. public class NumberParser {
  6.     public static void main(String[] args) {
  7.         Scanner s = new Scanner(System.in);
  8.         char a, b, c; // Variables for holding the digits as characters
  9.         int n1, n2, n3; // int variables for holding the digits and calculating the desired number
  10.         System.out.print("Please enter 3 digits between 0 and 9: ");
  11.         a = s.next().charAt(0);
  12.         b = s.next().charAt(0);
  13.         c = s.next().charAt(0);
  14.         n1 = a - '0';
  15.         n2 = b - '0';
  16.         n3 = c - '0';
  17.         // Validating the input characters. If the input is not a number character the system will print an error message
  18.         if((n1 > 9 || n1 < 0 ) || (n2 > 9 || n2 < 0 ) || (n3 > 9 || n3 < 0 )) {
  19.             System.out.println("Bad input!!");
  20.         } else {
  21.             // Calculating the number and printing it to the console
  22.             System.out.println("The number is: " + (n1 + n2*10 + n3*100));
  23.         }
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement