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
icon
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
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
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
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Problems on Dynamic Programming
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
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)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education