johnbarrett
08/04/2023, 2:09 AMpublic class PrintWord {
// The program prints a word by characters in a phrase using a for loop
// Declare Scanner
// Start with Main Method
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(<http://System.in|System.in>);
// Prompt for user input
System.out.print("Enter a phrase: ");
String s = input.nextLine();
System.out.print(s);
int numOfI = 0;
int numOfC = 0;
int numOfS = 0;
for (int j = 0; j < s.length(); j++) {
// Calculate counts of each character
if (s.charAt(j) == 'I' || s.charAt(j) == 'i') {
numOfI += 1;
}
else if (s.charAt(j) == 'C' || s.charAt(j) == 'c') {
numOfC += 1;
}
else if (s.charAt(j) == 'S' || s.charAt(j) == 's') {
numOfS += 1;
}
}
// Print out character counts
System.out.println("This phrase contains: ");
System.out.println(numOfI + "'I's");
System.out.println(numOfC + "'C's");
System.out.println(numOfS + "'S's");
}
}
seancorfield
s
?seancorfield
seancorfield
> javac PrintWord.java
> ls
PrintWord.class PrintWord.java build.clj deps.edn src target
> java PrintWord
Enter a phrase: Hawaii
HawaiiThis phrase contains:
2'I's
0'C's
0'S's
Do you want a newline in there?johnbarrett
08/04/2023, 2:36 AMSystem.out.print(s);
does not print what the user enters, I am stuck on this one. Thanks so much for your help!seancorfield
Enter a phrase: Hawaii
HawaiiThis phrase contains:
☝🏻 Yes, it does.johnbarrett
08/04/2023, 2:40 AM