- Count letters class. Class to test and handle exceptions.
This commit is contained in:
HeshamTB 2020-09-29 19:51:05 +03:00
parent 430e05d3d0
commit 4b8ebcf60c
Signed by: Hesham
GPG Key ID: 74876157D199B09E
6 changed files with 62 additions and 0 deletions

3
lab-03/.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

6
lab-03/.idea/misc.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="11" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
lab-03/.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/lab-03.iml" filepath="$PROJECT_DIR$/lab-03.iml" />
</modules>
</component>
</project>

6
lab-03/.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

11
lab-03/lab-03.iml Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -0,0 +1,28 @@
import java.util.Scanner;
public class CountLetters {
public static void main(String args[]){
int[] counts = new int[26];
Scanner in = new Scanner(System.in);
System.out.print("Enter a single word (Letters only, please): ");
String word = in.nextLine();
word = word.toUpperCase();
int j = 0;
try{
for (int i = 0; i < word.length(); i++) {
j++;
counts[word.charAt(i)-'A']++;
}
}
catch (IndexOutOfBoundsException e){
System.out.println("Found number: " + word.charAt(j-1) );
}
for (int i = 0; i < counts.length; i++){
if (counts[i] != 0)
System.out.println((char)(i + 'A') + ": " + counts[i]);
}
}
}