Question:
I/O text file help?
R9.
2014-05-12 23:53:45 UTC
FileReader file= new FileReader ("C:/Users/KT/Pictures/ Clipart/mytextFile.txt");
BufferedReader reader = new BufferedReader(file);

String text="";
String line= reader.readLine();

while(line != null){
text+=line;
text = text + "\n";
line =reader.readLine();
}

System.out.println(text);

This is a sample of my program , it reads my text file that contains numbers
23,44,12,11
12,45,44,10
11,9,8,3
2,1,44,3

how can I count each number in my file and display which number occurs most often ?
I think its going to be a loop that counts each number, but I dont know how.
Three answers:
KRANTI
2014-05-13 01:29:12 UTC
Basically, u need to store every number you get and implement hashing. In case of hash match, just increment the count. In the end, display the number with max count.



i = read_number();

if (var=hashMap.get(i) and null!= var)

put(key=i, value=var+1) /// increament the counter, store the reference for easy retrieval

else

put(key=i, 1)

Then you may go through the entire list finding out the highest count
Ninja
2014-05-13 06:27:22 UTC
import java.io.*;

import java.util.*;

public class Max

{

File fr=new File("abcd.txt");//to read file name

int z[]=new int[(int)fr.length()];//file length will be in the form of long , convert to int

int k=0;// to store number of words in a file

int numbers;//this will count number of copies of number in a file

int maximum;//this is to check which is Max number

int maxx;//this is store index position of Maximum number i.e max[] see blw

int max[]=new int[(int)fr.length()];//this will store number of copies(numbers see abv)

public void main()

{

try

{

Scanner sc=new Scanner(fr);

//reading the File and converting String to int

while(sc.hasNext())

{

z[k]=Integer.parseInt(sc.next().trim());

k++;

}

//this is checking number of copies

for(int i=0;i
{

for(int j=0;j
{

if(z[i]==z[j])

{

numbers++;

max[i]=numbers;

System.out.println(z[i] +" "+ z[j] +" "+ numbers);

}

}

numbers=0;

}

//this is checking which is most repeted copies

for(int i=0;i
{

if(max[i]>=maximum)

{

maximum=max[i];

maxx=i;

}

}

System.out.println("Maximum Repeted is :"+ max[maxx]+" Thats is Number"+z[maxx]);

}

catch(Exception e)

{

System.out.println("Found String insted of Numbers");

}

}

}

just make sure no comma or special chars in a file except numbes
?
2014-05-13 00:01:13 UTC
a linux user as it appears and



Note that puts the numbers first. If you were fussy about the order, you could do: uniq -c filename.txt | sed 's/[^0-9]*\\([0-9]\+\\) \\(.*\\)/\2 \1/



use "uniq (removes duplicate lines from a uniqed file)" command



uniq [-cdu] [-f skip-fields] [-s skip-chars] [-w check-chars] [-#skip-fields] [+#skip-chars] [--count] [--repeated] [--unique] [--skip-fields=skip-fields] [--skip-chars=skip-chars] [--check-hars=check-chars] [--help] [--version] [infile] [outfile]


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...