Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package chuj;
- import java.io.File;
- import java.io.IOException;
- import java.util.InputMismatchException;
- import java.util.Scanner;
- public class Zad_07 {
- public static void main(String[] args) throws IOException {
- Scanner scan = new Scanner(new File("tekst2.txt")).useDelimiter(" ");
- int total = 0;
- boolean foundInts = false; // flag to see if there are any integers
- int totalFloats = 0;
- boolean foundFloats = false;
- while (scan.hasNextLine()) { // Note change
- String currentLine = scan.nextLine();
- // split into words
- String words[] = currentLine.split(" ");
- // For each word in the line
- for (String str : words) {
- try {
- int num = Integer.parseInt(str);
- total++;
- foundInts = true;
- System.out.println("Found Int: " + num);
- } catch (NumberFormatException nfe) {
- }
- ; // word is not an integer, do nothing
- try {
- if (str.contains(",")) {
- str = str.replace(',', '.');
- float num = Float.parseFloat(str);
- totalFloats++;
- foundFloats = true;
- System.out.println("Found Float: " + num);
- }
- } catch (NumberFormatException nfe) {
- }
- ; // word is not an integer, do nothing
- }
- }
- // end while
- if (!foundInts)
- System.out.println("No numbers found.");
- else {
- System.out.println("Total Ints: " + total);
- System.out.println("Total Floats: " + totalFloats);
- }
- // close the scanner
- scan.close();
- System.out.println("Koniec pliku");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement