VerdommeMan

here fix

Aug 21st, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.00 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class inputStrings {
  4.  
  5. public static void main(String[ ] args) {
  6.         Scanner userInput = new Scanner(System.in);
  7.        
  8.         List<Integer> intArray = new ArrayList<>();
  9.         List<String> stringArray = new ArrayList<>();
  10.         List<Double> doubleArray = new ArrayList<>();
  11.        
  12.         while(true) {
  13.             System.out.println("Please enter a string: ");
  14.             String text=userInput.nextLine();
  15.             if(text.isEmpty()) {
  16.                 Collections.sort(intArray);
  17.                 Collections.sort(doubleArray);
  18.                 Collections.sort(stringArray);
  19.  
  20.                 printLists(intArray.toArray());
  21.                 printLists(doubleArray.toArray());
  22.                 printLists(stringArray.toArray());
  23.                 return;
  24.             }
  25.            else if(isInteger(text) ){
  26.                intArray.add(Integer.parseInt(text));
  27.            }
  28.             else if(isDouble(text)) {
  29.                doubleArray.add(Double.parseDouble(text));
  30.            }
  31.             else {
  32.                   stringArray.add(text);
  33.             }
  34.         }
  35.     }
  36.     public static < E > void printLists( E[] inputArray ) {
  37.         // Display array elements
  38.         for(E element : inputArray) {
  39.             System.out.printf("%s ", element);
  40.         }
  41.         System.out.println();
  42.     }
  43.     public static boolean isInteger(String s) {
  44.           boolean isValidInteger = false;
  45.           try
  46.           {
  47.              Integer.parseInt(s);
  48.      
  49.              // s is a valid integer
  50.      
  51.              isValidInteger = true;
  52.           }
  53.           catch (NumberFormatException ex)
  54.           {
  55.              // s is not an integer
  56.           }
  57.      
  58.           return isValidInteger;
  59.     }
  60.     public static boolean isDouble(String s) {
  61.           boolean isValidDouble = false;
  62.           try
  63.           {
  64.              Double.parseDouble(s);
  65.      
  66.              // s is a valid double
  67.      
  68.              isValidDouble = true;
  69.           }
  70.           catch (NumberFormatException ex)
  71.           {
  72.              // s is not an double
  73.           }
  74.      
  75.           return isValidDouble;
  76.     }
  77. }
Add Comment
Please, Sign In to add comment