how do you count letters in a string using java?
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
100%
how do you count letters in a string using java?
![I t ILO5
1 import java.io.*;
2 import java.util.*;
4
5 public class Hangman
6 {
7 public static void main(String[] args)
8.
6.
String [L loadWords();
System.out.println("Welcome to the game, Hangman!");
Scanner kbd = new Scanner(System.in);
//System.out.print("I am thinking of a word that is "+
String letter;
System.out.println("You have 5 guesses left.");
System.out.print("Please guess a letter: ");
letter = kbd.nextLine();
//String [] word "leo";
10
11
12
13
+ "letters long");
14
15
16
17
18
19
char firstletter l;
20
char secondletter 'e';
char thirdletter = 'o';
21
22
23
} // End of main method
24
25
26
#Create methods here
27
public static int count(char first)
28
29
int count = 0;
30
31
for(int i = 0; i < word.length; i++)
NOV
22
étv A E
MacBook Air](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F0a1176e8-b134-4ee8-8529-e24f63674f53%2F45db3573-80b9-4f06-a005-589e04fdae32%2Fnyy3qrb_processed.jpeg&w=3840&q=75)
Transcribed Image Text:I t ILO5
1 import java.io.*;
2 import java.util.*;
4
5 public class Hangman
6 {
7 public static void main(String[] args)
8.
6.
String [L loadWords();
System.out.println("Welcome to the game, Hangman!");
Scanner kbd = new Scanner(System.in);
//System.out.print("I am thinking of a word that is "+
String letter;
System.out.println("You have 5 guesses left.");
System.out.print("Please guess a letter: ");
letter = kbd.nextLine();
//String [] word "leo";
10
11
12
13
+ "letters long");
14
15
16
17
18
19
char firstletter l;
20
char secondletter 'e';
char thirdletter = 'o';
21
22
23
} // End of main method
24
25
26
#Create methods here
27
public static int count(char first)
28
29
int count = 0;
30
31
for(int i = 0; i < word.length; i++)
NOV
22
étv A E
MacBook Air
![Last edit was 17 minutes ago
nat Tools Add-ons Help
BIUA
Times New.
11
ormal text
1
2 I I 3
* Helper Code
* You do not have to understand the provided helper methods
50
51
52
* But you will have to know how/when to call these methods
53
* Make sure to read the instructions
* DO NOT make any changes to the methods below UNLESS specified
* by the directions.
54
55
56
*/
57
58
public static String[] loadWords()
59
60
* Returns a String array of valid words.
* Also prints out the total number of words (Strings) in the array.
61
62
63
*/
64
ArrayList<String> wordList = new ArrayList<String>(%3B
File f= new File("words.txt");
65
66
67
String[] wordsArr = new String[wordList.size()];
68
try
69
Scanner in = new Scanner(f);
while(in.hasNext())
70
71
72
String word
wordList.add(word);
73
in.next();
74
75
76
in.close();
System.out.println("Loading words from the file..");
System.out.println(wordList.size()+" words loaded.");
System.out.println("-
wordsArr = (String[])wordList.toArray(wordsArr);
} catch (FileNotFoundException ex) {
77
78
79
--------- ")3;
80
81
NOV
tv
22
MacBook Air](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F0a1176e8-b134-4ee8-8529-e24f63674f53%2F45db3573-80b9-4f06-a005-589e04fdae32%2Fuvckx4u_processed.jpeg&w=3840&q=75)
Transcribed Image Text:Last edit was 17 minutes ago
nat Tools Add-ons Help
BIUA
Times New.
11
ormal text
1
2 I I 3
* Helper Code
* You do not have to understand the provided helper methods
50
51
52
* But you will have to know how/when to call these methods
53
* Make sure to read the instructions
* DO NOT make any changes to the methods below UNLESS specified
* by the directions.
54
55
56
*/
57
58
public static String[] loadWords()
59
60
* Returns a String array of valid words.
* Also prints out the total number of words (Strings) in the array.
61
62
63
*/
64
ArrayList<String> wordList = new ArrayList<String>(%3B
File f= new File("words.txt");
65
66
67
String[] wordsArr = new String[wordList.size()];
68
try
69
Scanner in = new Scanner(f);
while(in.hasNext())
70
71
72
String word
wordList.add(word);
73
in.next();
74
75
76
in.close();
System.out.println("Loading words from the file..");
System.out.println(wordList.size()+" words loaded.");
System.out.println("-
wordsArr = (String[])wordList.toArray(wordsArr);
} catch (FileNotFoundException ex) {
77
78
79
--------- ")3;
80
81
NOV
tv
22
MacBook Air
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 2 steps

Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you

Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education

Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON

Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education

Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON

C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON

Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning

Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education