Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Homework EXercise 2
- -------------------------
- import java.util.Scanner;
- public class NumberParser {
- public static void main(String[] args) {
- Scanner s = new Scanner(System.in);
- char a, b, c; // Variables for holding the digits as characters
- int n1, n2, n3; // int variables for holding the digits and calculating the desired number
- System.out.print("Please enter 3 digits between 0 and 9: ");
- a = s.next().charAt(0);
- b = s.next().charAt(0);
- c = s.next().charAt(0);
- n1 = a - '0';
- n2 = b - '0';
- n3 = c - '0';
- // Validating the input characters. If the input is not a number character the system will print an error message
- if((n1 > 9 || n1 < 0 ) || (n2 > 9 || n2 < 0 ) || (n3 > 9 || n3 < 0 )) {
- System.out.println("Bad input!!");
- } else {
- // Calculating the number and printing it to the console
- System.out.println("The number is: " + (n1 + n2*10 + n3*100));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement