I have a text which is on a website. I am scanning that page and counting the number of several characters, including spaces caused by a line break or "enter press" and "tabs".
I have found an answer for counting the number of lines and such.
How can I do this in java? Counting whitespace is easy, there's a method for it, but not the line breaks or tabs as far as I know.
The website is this http://homepage.lnu.se/staff/jlnmsi/java1/HistoryOfProgramming.txt and I'm counting uppercase and lowercase letters, as well as spaces of any sort.
So far my output is correct for upper and lowercases but not spaces. I'm missing 15, which is exactly the number of line breaks.
public class CountChar
{
public static void main(String[] args) throws IOException
{
int upperCase = 0;
int lowerCase = 0;
int whitespace = 0;
int others = 0;
String url = "http://homepage.lnu.se/staff/jlnmsi/java1/HistoryOfProgramming.txt";
URL page = new URL(url);
Scanner in = new Scanner(page.openStream());
while (in.hasNextLine())
{
whitespace++; // THIS IS THE SOLUTION FOR THOSE WHO COME LATER <<<<<
String line = in.nextLine();
for (int i = 0; i < line.length(); i++)
{
if (Character.isUpperCase(line.charAt(i)))
{
upperCase++;
}
else if (Character.isLowerCase(line.charAt(i)))
{
lowerCase++;
}
else if (Character.isWhitespace(line.charAt(i)))
{
whitespace++;
}
else
{
others++;
}
}
}
System.out.print(lowerCase + " " + upperCase + " " + whitespace + " " + others);
}
}