
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
expand_more
expand_more
format_list_bulleted
Question
thumb_up100%
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](https://content.bartleby.com/qna-images/question/0a1176e8-b134-4ee8-8529-e24f63674f53/45db3573-80b9-4f06-a005-589e04fdae32/nyy3qrb_thumbnail.jpeg)
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](https://content.bartleby.com/qna-images/question/0a1176e8-b134-4ee8-8529-e24f63674f53/45db3573-80b9-4f06-a005-589e04fdae32/uvckx4u_thumbnail.jpeg)
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 nowThis is a popular solution!
Step by stepSolved 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.Similar questions
- Coding language: Javaarrow_forwardIn programming you work with text, sometimes you need to determine if a given string starts with or ends with certain characters. You can use two string methods to solve this problem: .startswith() and .endswith()?arrow_forwardWrite a function that accepts a pointer to an integer array and the array's size as arguments. The function should allocate a new array, copy the items from the original array to the new one in reverse order, and then deallocate the original array.arrow_forward
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education

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