Advertisement
Guest User

NumberParser.java

a guest
Aug 27th, 2013
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Scanner;
  2.  
  3. /**
  4.  * Input: One String with numbers. numbers are separated by a single space
  5.  * Output: numbers recognized and their sum Allowed: isDigit() substring()
  6.  * charAt() Integer.parseInt() length() Forbidden: split(), arrays or
  7.  * collections
  8.  */
  9. public class NumberParser {
  10.  
  11.     public static void main(String[] args) {
  12.         // print out the welcome header that gives the user instructions:  
  13.         System.out.println("Welcome to the Number Parser! Please enter one line"
  14.                 + " of numbers, separated by a single space between each,"
  15.                 + " followed by a return.");
  16.  
  17.         // get the user input
  18.         Scanner stdin = new Scanner(System.in);
  19.         String line = stdin.nextLine();
  20.  
  21.         // make sure the user input isn't null
  22.         if (line == null) {
  23.             System.out.println("Invalid input. Input was null");
  24.             System.exit(0);
  25.         }
  26.    
  27.     // make sure there's a non-digit character at the end for easier parsing later
  28.     line = line + " ";
  29.  
  30.         /*
  31.          * Now we have a valid input, and can parse out the numbers
  32.          */
  33.  
  34.         // set up variables that will keep track of things as we go
  35.         int sum = 0;
  36.         int beginIndex = 0;
  37.  
  38.         // step through the input String one character at a time.
  39.         for (int currentIndex = 0; currentIndex < line.length(); ++currentIndex) {
  40.             // get the character at the current index
  41.             char currentChar = line.charAt(currentIndex);
  42.  
  43.             // if the character is NOT a digit, then we have reached the end of a number.
  44.             if (!Character.isDigit(currentChar)) {
  45.                
  46.                 // make sure the number substring has at least one character in it
  47.                 if (currentIndex - beginIndex > 0) {
  48.                    
  49.                     // pull out the number substring.
  50.                     String numberSubstring = line.substring(beginIndex, currentIndex);
  51.                    
  52.                     // convert that number substring into an integer
  53.                     int currentNumber = Integer.parseInt(numberSubstring);
  54.                    
  55.                     // print out the number
  56.                     System.out.print(currentNumber + ", ");
  57.                    
  58.                     // add the number to the running total
  59.                     sum = sum + currentNumber;
  60.                 }
  61.                
  62.                 // we're on a non-digit character, so a new number substring might begin next character
  63.                 beginIndex = currentIndex + 1;
  64.             }
  65.         }
  66.  
  67.         // print out the sum
  68.         if (sum > 0) {
  69.             System.out.println("sum: " + sum);
  70.         } else {
  71.             System.out.println("No numbers present.");
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement