can you help me with this i want print out all of the words in alphabetical orde
ID: 3598853 • Letter: C
Question
can you help me with this i want print out all of the words in alphabetical order in output below but i try it doesnt show alphabetical i had attack my code bellow can you help me fix my code :
OUTPUT
run:
projects ---> 1
software ---> 2
developers ---> 1
maintain ---> 1
intervals, ---> 1
changing ---> 1
Give ---> 1
reflects ---> 1
they ---> 1
shorter ---> 1
indefinitely. ---> 1
method ---> 1
in ---> 1
work ---> 2
====== THis is my code ======
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class ReadWords {
public static void analyzeList(ArrayList<String> words) {
Collections.sort(words);
// printing words and count
Map<String, Integer> map = new HashMap<>();
// maping all words and out them in count .. key as word and count as value
for (String wrd : words) {
Integer num = map.get(wrd);
num = (num == null) ? 1 : ++num;
map.put(wrd, num);
}
// printing results
for (Map.Entry<String, Integer> each_entry : map.entrySet()) {
String word = each_entry.getKey().toString();
Integer count = each_entry.getValue();
System.out.println(word + " ---> " + count);
}
}
public static void main(String args[]) {
//arraylist data structure to store words
ArrayList<String> words = new ArrayList<>();
// open file
try {
File file = new File("agile_manifesto.txt");
Scanner sc = new Scanner(file);
while (sc.hasNext()) {
String line = sc.nextLine();
// convert line to array with delimiter as space
String[] tempWords = line.split(" ");
// storing words into arraylists
words.addAll(Arrays.asList(tempWords));
}
sc.close();
analyzeList(words);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
======== agile_manifesto.txt =========
Our highest priority is to satisfy the customer
through early and continuous delivery
of valuable software.
Welcome changing requirements, even late in
development. Agile processes harness change for
the customer's competitive advantage.
Deliver working software frequently, from a
couple of weeks to a couple of months, with a
preference to the shorter timescale.
Business people and developers must work
together daily throughout the project.
Build projects around motivated individuals.
Give them the environment and support they need,
and trust them to get the job done.
The most efficient and effective method of
conveying information to and within a development
team is face-to-face conversation.
Working software is the primary measure of progress.
Agile processes promote sustainable development.
The sponsors, developers, and users should be able
to maintain a constant pace indefinitely.
Continuous attention to technical excellence
and good design enhances agility.
Simplicity--the art of maximizing the amount
of work not done--is essential.
The best architectures, requirements, and designs
emerge from self-organizing teams.
At regular intervals, the team reflects on how
to become more effective, then tunes and adjusts
its behavior accordingly.
Explanation / Answer
In order to preserve the order of insertion in the map use LinkedHashMap.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class ReadWords {
public static void analyzeList(ArrayList<String> words) {
Collections.sort(words);
// printing words and count
// use LinkedHashMap instead of HashMap to preserve the insertion order
Map<String, Integer> map = new LinkedHashMap<>();
// maping all words and out them in count .. key as word and count as value
for (String wrd : words) {
Integer num = map.get(wrd);
num = (num == null) ? 1 : ++num;
map.put(wrd, num);
}
// printing results
for (Map.Entry<String, Integer> each_entry : map.entrySet()) {
String word = each_entry.getKey().toString();
Integer count = each_entry.getValue();
System.out.println(word + " ---> " + count);
}
}
public static void main(String args[]) {
//arraylist data structure to store words
ArrayList<String> words = new ArrayList<>();
// open file
try {
File file = new File("agile_manifesto.txt");
Scanner sc = new Scanner(file);
while (sc.hasNext()) {
String line = sc.nextLine();
// convert line to array with delimiter as space
String[] tempWords = line.split(" ");
// storing words into arraylists
words.addAll(Arrays.asList(tempWords));
}
sc.close();
analyzeList(words);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.