AStackClient.java 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import java.io.FileInputStream;
  2. import java.io.FileNotFoundException;
  3. import java.util.Scanner;
  4. public class AStackClient {
  5. public static void main(String[] args) throws FileNotFoundException {
  6. Scanner console = new Scanner(System.in);
  7. System.out.println("Enter file name:");
  8. String fileName = console.nextLine();
  9. console.close();
  10. Scanner in = new Scanner(new FileInputStream(fileName));
  11. in.useDelimiter("\\s+");
  12. final int n = 5;
  13. AStack<Integer> intStack = new AStack<Integer>(n);
  14. AStack<Float> fltStack = new AStack<Float>(n);
  15. AStack<String> strStack = new AStack<String>(n);
  16. while (in.hasNext())
  17. {
  18. if (in.hasNextFloat())
  19. {
  20. fltStack.push(in.nextFloat());
  21. }
  22. else if (in.hasNextInt())
  23. {
  24. intStack.push(in.nextInt());
  25. }
  26. else
  27. {
  28. strStack.push(in.next());
  29. }
  30. }
  31. in.close();
  32. System.out.println("Abort mission");
  33. System.out.print("Strings:");
  34. while (!strStack.isEmpty())
  35. {
  36. System.out.print(" " + strStack.pop());
  37. }
  38. System.out.print("\nFloats:");
  39. while (!fltStack.isEmpty())
  40. {
  41. System.out.print(" " + fltStack.pop());
  42. }
  43. System.out.print("\nIntegers:");
  44. while (!intStack.isEmpty())
  45. {
  46. System.out.print(" " + intStack.pop());
  47. }
  48. System.out.println();
  49. }
  50. }