Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.45 KB | None | 0 0
  1. -------------------- DigitExtractor.Java --------------------
  2. /**
  3.  * @(#)DigitExtractor.java
  4.  *
  5.  * DigitExtractor application
  6.  *
  7.  * @author Tim Hill
  8.  * @version 1.00 2010/5/8
  9.  */
  10.  
  11.  import java.util.*;
  12.  
  13. public class DigitExtractor {
  14.    
  15.     public static void main(String[] args) {
  16.        
  17.         //Initialize variables
  18.         Scanner input = new Scanner(System.in);
  19.         String choice = "";
  20.         Num num = new Num();
  21.        
  22.         //Ask for integer -> store in object
  23.         System.out.print("Please enter an integer: ");
  24.         try{
  25.             num.nummer = input.nextInt();
  26.         }
  27.         catch(Exception exc){
  28.             System.out.println("Please put in an integer! (number from -inf to inf no decimals and no letters)");
  29.         }
  30.        
  31.         //Start program look
  32.         while(choice.compareToIgnoreCase("q") != 0){
  33.            
  34.         //Print interface -> get choice
  35.         System.out.print(
  36.             "Show (W)hole number.\n" +
  37.             "Show (O)nes place number.\n" +
  38.             "Show (T)ens place number.\n" +
  39.             "Show (H)undreds place number.\n" +
  40.             "(Q)uit.\n" +
  41.             "Enter your choice: "
  42.             );
  43.                
  44.             choice = input.next();
  45.        
  46.             num.Decision(choice);
  47.         }
  48.     }
  49. }
  50.  
  51. -------------------- Num.java ---------------------
  52.  
  53. import java.util.*;
  54.  
  55. class Num {
  56.     int nummer;
  57.     String sNummer;
  58.     int snLength;
  59.    
  60.     public Num(){
  61.         nummer = 0;
  62.         sNummer = "";
  63.     }
  64.    
  65.     public void Decision(String choice){
  66.        
  67.         //Store nummer into string -> get string length -> store into int
  68.         sNummer = Integer.toString(nummer);
  69.         snLength = sNummer.length();
  70.        
  71.         //Handle Whole Number
  72.         if(choice.compareToIgnoreCase("w") == 0){
  73.             System.out.println("The whole integer is: " + nummer);
  74.         }
  75.        
  76.         //Handle Ones Place
  77.         if(choice.compareToIgnoreCase("o") == 0){
  78.             System.out.println("The ones place digit is: " + sNummer.substring(snLength - 1, snLength));
  79.         }
  80.        
  81.         //Handle Tens Place
  82.         if(choice.compareToIgnoreCase("t") == 0){
  83.             if(snLength >= 2){
  84.                 System.out.println("The tens place digit is: " + sNummer.substring(snLength - 2, snLength));
  85.             }
  86.            
  87.             else{
  88.                 System.out.println("You can't have a \"Tens\" place digit with the number being less than 10!");
  89.             }
  90.         }
  91.        
  92.         //Handle Hundreds Place
  93.         if(choice.compareToIgnoreCase("h") == 0){
  94.             if(snLength >= 3){
  95.                 System.out.println("The hunderds digit is: " + sNummer.substring(snLength - 3, snLength));
  96.             }
  97.            
  98.             else{
  99.                 System.out.println("You can't have a \"Hunderds\" place digit with the number being less than 100!");
  100.             }
  101.         }
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement