Question

In: Computer Science

In Java, Using ArrayList and HashSet data structures, as well as their methods, obtain following from...

In Java, Using ArrayList and HashSet data structures, as well as their methods, obtain following from some input text file:
Total number of characters used,without counting spaces and punctuation, total number of words used; Number of words, counting each word only once; Total number of punctuation characters;Number of words that are of size six or more;Number of words that are used only once

Solutions

Expert Solution

import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;

/**
*
* @author paramesh
*/
public class CountWords {
public static int totalChars(ArrayList<String> words){
int totalChars = 0;
for(int i=0;i<words.size();i++){
totalChars = totalChars +words.get(i).length();
}
return totalChars;
}
public static int totalCountWithoutSpaces(ArrayList<String> words){
return words.size();
}
public static int totalWords(ArrayList<String> words){
int totalWords = 0;
ArrayList<String> alreadyVisited = new ArrayList<String>();
//counting starts from here
for(int i=0;i<words.size();i++){
boolean visited = false;
for(int j=0;j<alreadyVisited.size();j++){
if(words.get(i).equals(alreadyVisited.get(j))){
visited = true;
}
}
if(!visited){
totalWords++;
alreadyVisited.add(words.get(i));
}
}
return totalWords;
}
public static void main(String args[]) {
System.out.println("Oppgave A");
ArrayList<String> words = new ArrayList<String>();
try {
File fr = new File("Alice.txt");
Scanner sc = new Scanner(fr);

while (sc.hasNext()) {
String line = sc.nextLine();
String[] space = line.split(" ");
for(int i=0;i<space.length;i++){
words.add(space[i]);
}
}
} catch (Exception e) {
System.out.println("File not found");
}
//calling methods
System.out.println("Total words: "+totalWords(words));
}
}


Related Solutions

A Java question. Write the class Staff. It contains methods that manipulate an ArrayList of Strings...
A Java question. Write the class Staff. It contains methods that manipulate an ArrayList of Strings representing the names of staff members. The constructor takes an ArrayList of String names as a parameter. In addition to the constructor, you need to implement the following methods The methods 1. public boolean equals(Staff other) - Determines if the other Staff contains all the same elements in the same order as this Staff 2. public boolean sameContents(Staff other) - Determines if the other...
Using Java with no imports,    Method arrayOfSums returns an ArrayList of the same size as...
Using Java with no imports,    Method arrayOfSums returns an ArrayList of the same size as the largest ArrayList        This method needs to be overloaded so that it works for 2 to 4 sent Arraylists.        arrayOfSums([2,4,7], [3,7,14]) returns [5,11,21]        arrayOfSums([6,13,4,8], [7,5,9], [2,5,8,3]) returns [15, 23, 21, 11]        arrayOfSums([6,13,4,8], [7,5,9], [2,5,8,3], [3,5,4,1,2]) returns [18, 28, 25, 12, 2]        arrayOfSums([8,6], [2,4,10]) returns [10,10,10]        arrayOfSums([2,2,2,2,2], [3,2,5,2,3], [1,3,1,3,1]) returns [6, 7, 8,...
Move all zeros in an arraylist to the end of the list using a temp(java). if...
Move all zeros in an arraylist to the end of the list using a temp(java). if it is an array the code would look like the code below, change it to fit an arraylist int count = 0;     int temp;     for (int i = 0; i < n; i++) {     if ((arr[i] != 0)) {         temp = arr[count];         arr[count] = arr[i];         arr[i] = temp;         count = count + 1;     }     } }
Using Java with NO imports,    Method arrayOfSums returns an ArrayList of the same size as...
Using Java with NO imports,    Method arrayOfSums returns an ArrayList of the same size as the largest ArrayList        This method needs to be overloaded so that it works for 2 to 4 sent Arraylists.        arrayOfSums([2,4,7], [3,7,14]) returns [5,11,21]        arrayOfSums([6,13,4,8], [7,5,9], [2,5,8,3]) returns [15, 23, 21, 11]        arrayOfSums([6,13,4,8], [7,5,9], [2,5,8,3], [3,5,4,1,2]) returns [18, 28, 25, 12, 2]        arrayOfSums([8,6], [2,4,10]) returns [10,10,10]        arrayOfSums([2,2,2,2,2], [3,2,5,2,3], [1,3,1,3,1]) returns [6, 7, 8,...
in java please: Create an ArrayListReview class with one data field of ArrayList and one with...
in java please: Create an ArrayListReview class with one data field of ArrayList and one with LinkedList with the generic type passed to the class. (2 point) Create a constructor that populate an array list and the LinkedList filled with the generic type through inserting new elements into the specified location index-i in the list. (2 points)
Complete the following methods in java: Consult the rest of the methods of BigInteger from its...
Complete the following methods in java: Consult the rest of the methods of BigInteger from its API documentation as needed to solve the following problems. public static List<BigInteger> fibonacciSum(BigInteger n) The unique breakdown into Fibonacci numbers can be constructed with a greedy algorithm that simply finds the largest Fibonacci number f that is less than or equal to n. Add this f to the result list, and break down the rest of the number given by the expression n-f the...
Write the following methods in java class ARM that represent state information as well as functional...
Write the following methods in java class ARM that represent state information as well as functional blocks of the ARM platform. [Go through the following 5 classes then write methods for the instructions: mov, str, ldr, add in class ARM, finally, write a print method for ARM in Main.java that can display the registers and the memory locations that have been used. (make sure to make a visualization of the print method instead of just a console dump)] --- Please...
• 2. Get all the information from the user using methods Java • B. if the...
• 2. Get all the information from the user using methods Java • B. if the inputs are not given in the proper format the program should prompt user to give the proper input (eg. Name cannot be numbers, age cannot be String)
Data Structures and Algorithm Analysis in Java by Weiss: Exercise 6.8 Show the following regarding the...
Data Structures and Algorithm Analysis in Java by Weiss: Exercise 6.8 Show the following regarding the maximum item in the heap: A) It must be at least one of the leaves. B) There are exactly [N/2] leaves. C) Every leaf must be examined to find it.
Java Programming CS 209 Data Structure 1. Create a method that takes an ArrayList of String...
Java Programming CS 209 Data Structure 1. Create a method that takes an ArrayList of String and returns a copy of that ArrayList with no duplicates. The relative ordering of elements in the new ArrayList should be the same. Sample Input: {"qwerty", "asdfgh", "qwer", "123", "qwerty", "123", "zxcvbn", "asdfgh"} Sample Output: {"qwerty", "asdfgh", "qwer", "123", "zxcvbn"}
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT