import java.awt.*; import java.awt.event.*; import java.util.*; //MapTest public
ID: 668480 • Letter: I
Question
import java.awt.*;
import java.awt.event.*;
import java.util.*;
//MapTest
public class MapTest {
private HashMap map;
public MapTest() {
map = new HashMap();
createMap();
System.out.println(createOutput());
}
private void createMap()
{
String input;
//create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
//inputting first string
System.out.println("Enter first string: ");
input=keyboard.nextLine();
input.toLowerCase();
for(int i = 0; i < input.length(); i++)
{
char letter = input.charAt(i);
if (Character.isLetter(letter))
{
String key = String.valueOf(letter);
//if the map contains the work
if(map.containsKey(key));
{
//get the value
Integer count = (Integer) map.get(key);
//and increment it
map.put(key, new Integer(count.intValue() + 1));
}
else //otherwise add the word with a value of 1
map.put(key,new Integer(1));
}
}//end for
}// end method createMap
private String createOutput() {
Set keys = map.keySet();
Iterator keyIterator = keys.iterator();
String output = " ";
// iterate through the keys
while (keyIterator.hasNext()) {
Object currentKey = keyIterator.next();
// output the key value pairs
output += currentKey + " " + map.get(currentKey) + " ";
}
return output;
}// end method createOutput
public static void main(String args[]) {
MapTest application = new MapTest();
}
}// end class MapTest
I get a syntax error on line 42 "else", the application won't run. Can you fix the error?
Explanation / Answer
import java.awt.*;
import java.awt.event.*;
import java.util.*;
//MapTest
public class MapTest {
private HashMap map;
public MapTest() {
map = new HashMap();
createMap();
System.out.println(createOutput());
}
private void createMap()
{
String input;
//create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
//inputting first string
System.out.println("Enter first string: ");
input=keyboard.nextLine();
input.toLowerCase();
for(int i = 0; i < input.length(); i++)
{
char letter = input.charAt(i);
if (Character.isLetter(letter))
{
String key = String.valueOf(letter);
//if the map contains the work
if(map.containsKey(key)) //Removed this semi colon
{
//get the value
Integer count = (Integer) map.get(key);
//and increment it
map.put(key, new Integer(count.intValue() + 1));
}
else //otherwise add the word with a value of 1
map.put(key,new Integer(1));
}
}//end for
}// end method createMap
private String createOutput() {
Set keys = map.keySet();
Iterator keyIterator = keys.iterator();
String output = " ";
// iterate through the keys
while (keyIterator.hasNext()) {
Object currentKey = keyIterator.next();
// output the key value pairs
output += currentKey + " " + map.get(currentKey) + " ";
}
return output;
}// end method createOutput
public static void main(String args[]) {
MapTest application = new MapTest();
}
}// end class MapTest
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.