Advertisement
Guest User

jebaneprzecinki

a guest
Oct 23rd, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. package chuj;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.util.InputMismatchException;
  6. import java.util.Scanner;
  7.  
  8. public class Zad_07 {
  9. public static void main(String[] args) throws IOException {
  10. Scanner scan = new Scanner(new File("tekst2.txt")).useDelimiter(" ");
  11.  
  12. int total = 0;
  13. boolean foundInts = false; // flag to see if there are any integers
  14.  
  15. int totalFloats = 0;
  16. boolean foundFloats = false;
  17.  
  18. while (scan.hasNextLine()) { // Note change
  19. String currentLine = scan.nextLine();
  20. // split into words
  21. String words[] = currentLine.split(" ");
  22.  
  23. // For each word in the line
  24. for (String str : words) {
  25. try {
  26. int num = Integer.parseInt(str);
  27. total++;
  28. foundInts = true;
  29. System.out.println("Found Int: " + num);
  30. } catch (NumberFormatException nfe) {
  31. }
  32. ; // word is not an integer, do nothing
  33.  
  34. try {
  35. if (str.contains(",")) {
  36. str = str.replace(',', '.');
  37. float num = Float.parseFloat(str);
  38. totalFloats++;
  39. foundFloats = true;
  40. System.out.println("Found Float: " + num);
  41. }
  42. } catch (NumberFormatException nfe) {
  43. }
  44. ; // word is not an integer, do nothing
  45. }
  46. }
  47. // end while
  48.  
  49. if (!foundInts)
  50. System.out.println("No numbers found.");
  51. else {
  52. System.out.println("Total Ints: " + total);
  53. System.out.println("Total Floats: " + totalFloats);
  54. }
  55.  
  56. // close the scanner
  57. scan.close();
  58. System.out.println("Koniec pliku");
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement