The validator says I'm not counting lowercase letters while I think I am. Or am using the regex the wrong way in line 23? I tried eualsIgnoreCase(), it didn't work either.
I've also tried some more complicated code, in which I used StringBuilder and Treeset, see below). In this variant the validator admitted I was counting upperCase letters, too, but my code still didn't pass the third requirement (though I thought I counted and displayed the number of alphabetic characters). Is the problem with the way my while-loop reads data from the file?..
public class Solution {
public static void main(String[] args) throws FileNotFoundException, IOException, IndexOutOfBoundsException {
String fileName = args[0];
//String fileName = "C:\\Users\\passe\\Documents\\Input.txt";
FileInputStream fis = new FileInputStream(fileName);
int x;
StringBuilder data = new StringBuilder();
while (fis.available() > 0) {
x = fis.read();
char letter = (char) x;
data.append(String.valueOf(letter));
}
String ABS = data.toString().replaceAll("[^a-zA-Z]", "");
TreeSet<Character> mySet = new TreeSet<>();
for (int i = 0; i < ABS.length(); i++) {
mySet.add(ABS.charAt(i));
}
System.out.println(mySet.size());
fis.close();
}
package com.codegym.task.task18.task1816;
/*
ABCs
*/
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.TreeSet;
public class Solution {
public static void main(String[] args) throws FileNotFoundException, IOException, IndexOutOfBoundsException {
String fileName = args[0];
//String fileName = "C:\\Users\\passe\\Documents\\Input.txt";
FileInputStream fis = new FileInputStream(fileName);
int x;
int abcCounter = 0;
while (fis.available() > 0) {
x = fis.read();
char letter = (char) x;
if (String.valueOf(letter).equals("[a-zA-Z]")) abcCounter++;
}
System.out.println(abcCounter);
fis.close();
}
}