Advertisement
Guest User

NumberParser.java

a guest
Aug 27th, 2013
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.68 KB | None | 0 0
  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 empty or null
  22.         if (line == null) {
  23.             System.out.println("Invalid input. Input was null");
  24.             System.exit(0);
  25.         }
  26.  
  27.         /*
  28.          * Now we have a valid input, and can parse out the numbers
  29.          */
  30.  
  31.         // set up variables that will keep track of things as we go
  32.         int sum = 0;
  33.         int beginIndex = 0;
  34.  
  35.         // step through the input String one character at a time.
  36.         for (int currentIndex = 0; currentIndex < line.length(); ++currentIndex) {
  37.             // get the character at the current index
  38.             char currentChar = line.charAt(currentIndex);
  39.  
  40.             // if the character is NOT a digit, then we have reached the end of a number.
  41.             if (!Character.isDigit(currentChar)) {
  42.                
  43.                 // make sure the number substring has at least one character in it
  44.                 if (currentIndex - beginIndex > 0) {
  45.                    
  46.                     // pull out the number substring.
  47.                     String numberSubstring = line.substring(beginIndex, currentIndex);
  48.                    
  49.                     // convert that number substring into an integer
  50.                     int currentNumber = Integer.parseInt(numberSubstring);
  51.                    
  52.                     // print out the number
  53.                     System.out.print(currentNumber + ", ");
  54.                    
  55.                     // add the number to the running total
  56.                     sum = sum + currentNumber;
  57.                 }
  58.                
  59.                 // we're on a non-digit character, so a new number substring might begin next character
  60.                 beginIndex = currentIndex + 1;
  61.             }
  62.         }
  63.  
  64.         // print out the sum
  65.         if (sum > 0) {
  66.             System.out.println("sum: " + sum);
  67.         } else {
  68.             System.out.println("No numbers present.");
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement