Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -------------------- DigitExtractor.Java --------------------
- /**
- * @(#)DigitExtractor.java
- *
- * DigitExtractor application
- *
- * @author Tim Hill
- * @version 1.00 2010/5/8
- */
- import java.util.*;
- public class DigitExtractor {
- public static void main(String[] args) {
- //Initialize variables
- Scanner input = new Scanner(System.in);
- String choice = "";
- Num num = new Num();
- //Ask for integer -> store in object
- System.out.print("Please enter an integer: ");
- try{
- num.nummer = input.nextInt();
- }
- catch(Exception exc){
- System.out.println("Please put in an integer! (number from -inf to inf no decimals and no letters)");
- }
- //Start program look
- while(choice.compareToIgnoreCase("q") != 0){
- //Print interface -> get choice
- System.out.print(
- "Show (W)hole number.\n" +
- "Show (O)nes place number.\n" +
- "Show (T)ens place number.\n" +
- "Show (H)undreds place number.\n" +
- "(Q)uit.\n" +
- "Enter your choice: "
- );
- choice = input.next();
- num.Decision(choice);
- }
- }
- }
- -------------------- Num.java ---------------------
- import java.util.*;
- class Num {
- int nummer;
- String sNummer;
- int snLength;
- public Num(){
- nummer = 0;
- sNummer = "";
- }
- public void Decision(String choice){
- //Store nummer into string -> get string length -> store into int
- sNummer = Integer.toString(nummer);
- snLength = sNummer.length();
- //Handle Whole Number
- if(choice.compareToIgnoreCase("w") == 0){
- System.out.println("The whole integer is: " + nummer);
- }
- //Handle Ones Place
- if(choice.compareToIgnoreCase("o") == 0){
- System.out.println("The ones place digit is: " + sNummer.substring(snLength - 1, snLength));
- }
- //Handle Tens Place
- if(choice.compareToIgnoreCase("t") == 0){
- if(snLength >= 2){
- System.out.println("The tens place digit is: " + sNummer.substring(snLength - 2, snLength));
- }
- else{
- System.out.println("You can't have a \"Tens\" place digit with the number being less than 10!");
- }
- }
- //Handle Hundreds Place
- if(choice.compareToIgnoreCase("h") == 0){
- if(snLength >= 3){
- System.out.println("The hunderds digit is: " + sNummer.substring(snLength - 3, snLength));
- }
- else{
- System.out.println("You can't have a \"Hunderds\" place digit with the number being less than 100!");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement