Hello everyone,

for example, I have name.txt file. It contains three lines of text (see below):

Roses are red%
Sky is blue%
JAVA is cool%

All lines ended with % symbol.

I need to count how many every line have characters and which character is repeated max and min times.

Thanks !

I need to count how many symbols are in every line excluding spaces... and if you know how to do it.. there are another problem.. I need to show which character are repeated maximum times and which characted is repeated minimum times in every line.

import java.io.*;
 class stringlines
  public static void main(String args[])

   FileInputStream fstream = new FileInputStream("name.txt");

   DataInputStream in = new DataInputStream(fstream);
   BufferedReader br = new BufferedReader(new InputStreamReader(in));
   String strLine;

   while ((strLine = br.readLine()) != null) {
   System.out.println (strLine);

   catch (Exception e){//Catch exception if any
   System.err.println("Error: " + e.getMessage());

Where do you count the characters? Look at the String class's methods for getting at the characters in a String.
What kinds of technoques do you know for saving the counts?

Edited 4 Years Ago by NormR1

In console, I don't need to write numbers of characters in .txt file... I know line.lenght() method, but it ain't helping cuz' it counts spaces too.

Edited 4 Years Ago by EPerminas: No reason

You could use an array to save the counts (if the characters are ASCII) or a Map.
With the array, use the value of the character as the index into the array.
With the Map, the charactere would be the Map's key and the Map's value would be the count.

helpful advises

Yes, the characters are ASCII. I see your point, but can I get atleast one little example so I can see how to do it. I don't really ask you to do the whole job for me.. Thanks again.

Which technique are you going to use to save the counts?
The array technique:
get the next char
index the array with the char(char are numeric) and add one to the count for that letter: theArray[theChar]++;

Edited 4 Years Ago by NormR1

Here it is. Now, how do I need to read char by char to put them in to my hashmap?

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

public class mepas

    private static Scanner x;
    try {
    x = new Scanner(new File ("timbaland.txt"));
    catch(Exception e) 
        System.out.println("missing a file");

    public static <Char> void main(String[] args)
    while (x.hasNext())

     Char a;
     a = System.x.Read()
     HashMap<Char, Integer> map = new HashMap<Char, Integer>();

     map.put((Char) "a",5);

     int markofA = map.get("a");



It's pointless trying to add more code until the code you already have compiles and, as far as is possible, executes correctly. Stage 1: fix those compile errors.

Don't know how to do it. Thanks for your interest.

No need for sarcasm. Do you know how to compile a java program? What error messages do you get? Read them carefully. most will be self-explanatory. Then as Norm says, Post the full text of the error messages that you need help with

This article has been dead for over six months. Start a new discussion instead.