Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class inputStrings {
- public static void main(String[ ] args) {
- Scanner userInput = new Scanner(System.in);
- List<Integer> intArray = new ArrayList<>();
- List<String> stringArray = new ArrayList<>();
- List<Double> doubleArray = new ArrayList<>();
- while(true) {
- System.out.println("Please enter a string: ");
- String text=userInput.nextLine();
- if(text.isEmpty()) {
- Collections.sort(intArray);
- Collections.sort(doubleArray);
- Collections.sort(stringArray);
- printLists(intArray.toArray());
- printLists(doubleArray.toArray());
- printLists(stringArray.toArray());
- return;
- }
- else if(isInteger(text) ){
- intArray.add(Integer.parseInt(text));
- }
- else if(isDouble(text)) {
- doubleArray.add(Double.parseDouble(text));
- }
- else {
- stringArray.add(text);
- }
- }
- }
- public static < E > void printLists( E[] inputArray ) {
- // Display array elements
- for(E element : inputArray) {
- System.out.printf("%s ", element);
- }
- System.out.println();
- }
- public static boolean isInteger(String s) {
- boolean isValidInteger = false;
- try
- {
- Integer.parseInt(s);
- // s is a valid integer
- isValidInteger = true;
- }
- catch (NumberFormatException ex)
- {
- // s is not an integer
- }
- return isValidInteger;
- }
- public static boolean isDouble(String s) {
- boolean isValidDouble = false;
- try
- {
- Double.parseDouble(s);
- // s is a valid double
- isValidDouble = true;
- }
- catch (NumberFormatException ex)
- {
- // s is not an double
- }
- return isValidDouble;
- }
- }
Add Comment
Please, Sign In to add comment