在以下实施频率代码中出现错误

时间:2023-01-18 23:30:42

Hello EveryOne I want to Sort the text of a file according to their frequency.I am getting Error at runtime outofmemory Error.Please Give response as soon as possible.

Hello EveryOne我想根据文件的频率对文件的文本进行排序。我在运行时输出错误内存错误。请尽快给出回复。

import java.io.*; 

import java.util.*;

import java.util.Collections;   

class SortedInAlphaOrder

 {

public static void main(String a[]) throws IOException{

          Scanner input = new Scanner(System.in);

          System.out.println("Enter the filename with path : " );

          //String filename = input.next();

          File f = new File("Ashish.txt");

          BufferedReader br = new BufferedReader(new FileReader(f));

          StringBuffer buffer = new StringBuffer();

          String str =br.readLine();

          while((str!= null)){

          buffer.append(str);

          buffer.append(" ");

          }

          ArrayList<String> list = new ArrayList<String>();


       StringTokenizer st = new StringTokenizer(buffer.toString().toLowerCase());

          while(st.hasMoreTokens()){

                String s = st.nextToken();

                list.add(s);

                }

            HashSet<String> set = new HashSet<String>(list);

            List<String> arrayList = new ArrayList<String>(set);

            Collections.sort(arrayList);

            for(Object ob : arrayList){

                    System.out.println(ob.toString());

                }

            }           }

Getting Error OutofMemoryHeap Error.Help me out.

获取错误OutofMemoryHeap Error.Help me out。

2 个解决方案

#1


5  

I think I see your problem. You assign str to br.readLine() one, but your while loop only stops when str == null. I'm assuming that the first br.readLine() is returning a non-null String, and you keep checking if it is null in your while loop. As a result, your wile loop never stops and you eventually get a OutOfMemory error. Instead, you should change your code like this (untested).

我想我看到了你的问题。您将str分配给br.readLine(),但是while循环仅在str == null时停止。我假设第一个br.readLine()返回一个非null的String,你继续检查你的while循环中是否为null。结果,你的wile循环永远不会停止,你最终会得到一个OutOfMemory错误。相反,您应该像这样更改您的代码(未经测试)。

// ...other code
String str;
while((str = br.readLine()) != null){
      buffer.append(str);
      buffer.append(" ");
}
// ...other code

#2


0  

You seems to be doing very redundant stuff. You should be taking the words directly into a Collection and running the sort on them.

你似乎做了非常多余的事情。您应该将这些单词直接带入Collection并对其进行排序。

Anyways you can always execute the same program with increased memory by providing -Xmx128M option.

无论如何,通过提供-Xmx128M选项,您总是可以通过增加内存来执行相同的程序。

#1


5  

I think I see your problem. You assign str to br.readLine() one, but your while loop only stops when str == null. I'm assuming that the first br.readLine() is returning a non-null String, and you keep checking if it is null in your while loop. As a result, your wile loop never stops and you eventually get a OutOfMemory error. Instead, you should change your code like this (untested).

我想我看到了你的问题。您将str分配给br.readLine(),但是while循环仅在str == null时停止。我假设第一个br.readLine()返回一个非null的String,你继续检查你的while循环中是否为null。结果,你的wile循环永远不会停止,你最终会得到一个OutOfMemory错误。相反,您应该像这样更改您的代码(未经测试)。

// ...other code
String str;
while((str = br.readLine()) != null){
      buffer.append(str);
      buffer.append(" ");
}
// ...other code

#2


0  

You seems to be doing very redundant stuff. You should be taking the words directly into a Collection and running the sort on them.

你似乎做了非常多余的事情。您应该将这些单词直接带入Collection并对其进行排序。

Anyways you can always execute the same program with increased memory by providing -Xmx128M option.

无论如何,通过提供-Xmx128M选项,您总是可以通过增加内存来执行相同的程序。