Soluții

Find the most repeated word in a text file with Java

  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.util.ArrayList;
  4. public class MostRepeatedWord {
  5.     public static void main(String[] args) throws Exception {
  6.         String line, word = “”;
  7.         int count = 0, maxCount = 0;
  8.         ArrayList<String> words = new ArrayList<String>();
  9.         //Opens file in read mode  
  10.         FileReader file = new FileReader(“data1.txt “);
  11.         BufferedReader br = new BufferedReader(file);
  12.         //Reads each line  
  13.         while((line = br.readLine()) != null) {
  14.             String string[] = line.toLowerCase().split(“([,.\\s]+) “);
  15.             //Adding all words generated in previous step into words  
  16.             for(String s : string){
  17.                 words.add(s);
  18.             }
  19.         }
  20.         //Determine the most repeated word in a file  
  21.         for(int i = 0; i < words.size(); i++){
  22.             count = 1;
  23.             //Count each word in the file and store it in variable count  
  24.             for(int j = i+1; j < words.size(); j++){
  25.                 if(words.get(i).equals(words.get(j))){
  26.                     count++;
  27.                 }
  28.             }
  29.             //If maxCount is less than count then store value of count in maxCount   
  30.             //and corresponding word to variable word  
  31.             if(count > maxCount){
  32.                 maxCount = count;
  33.                 word = words.get(i);
  34.             }
  35.         }
  36.         System.out.println(“Most repeated word: “ + word);
  37.         br.close();
  38.     }
  39. }

Output:

Most repeated word: word
[mai mult...]

Find the frequency of characters with Java

To accomplish this task, we will maintain an array called freq with same size of the length of the string. Freq will be used to maintain the count of each character present in the string. Now, iterate through the string to compare each character with rest of the string. Increment the count of corresponding element in freq. Finally, iterate through freq to display the frequencies of characters.

For example: Frequency of p in above string is 2.

[mai mult...]

How to Reverse a String in Java

Example 1: Reverse a string word by word using recursion

  1. import java.util.Scanner;
  2. public class ReverseStringExample1
  3. {
  4. public static void main(String[] args)
  5. {
  6. String str;
  7. System.out.println(“Enter a string: “);
  8. Scanner scanner = new Scanner(System.in);
  9. str = scanner.nextLine();
  10. scanner.close();                                //closes the input stream
  11. String reversed = reverseString(str);
  12. System.out.println(“The reversed string is: “ + reversed);
  13. }
  14. public static String reverseString(String s)
  15. {
  16. if (s.isEmpty())                            //checks the string if empty
  17. return s;
  18. return reverseString(s.substring(1)) + s.charAt(0);                     //recursively called function
  19. }
  20. }

Output:

How to Reverse a String in Java Word by Word

[mai mult...]

How to Unhide All Rows in Excel

Unhiding all the rows in a Microsoft Excel spreadsheet is as easy as pressing a keyboard shortcut or using a button on the ribbon.

First, select your entire worksheet using Ctrl+A (Windows) or Command+A (Mac). Press Ctrl+Shift+9, right-click a cell, and choose “Unhide,” or select Format > Hide & Unhide > Unhide Rows from the ribbon at the top to unhide all rows.

[mai mult...]