相关疑难解决方法(0)

使用Java中的BufferedReader获取输入

我这里有一点烦人的情况; 其中我无法正确接受输入.我总是接受输入Scanner,而不习惯BufferedReader.


输入格式


First line contains T, which is an integer representing the number of test cases.
T cases follow. Each case consists of two lines.

First line has the string S. 
The second line contains two integers M, P separated by a space.
Run Code Online (Sandbox Code Playgroud)

Input:
2
AbcDef
1 2
abcabc
1 1
Run Code Online (Sandbox Code Playgroud)

我的代码到目前为止:


public static void main (String[] args) throws java.lang.Exception
{
    BufferedReader inp = new BufferedReader (new InputStreamReader(System.in));
    int T= Integer.parseInt(inp.readLine());

    for(int i=0;i<T;i++) { …
Run Code Online (Sandbox Code Playgroud)

java input

7
推荐指数
1
解决办法
18万
查看次数

在文件中搜索String,如果找到则返回该String

如何在txt文件中搜索用户输入的String,然后将该String返回到控制台.我写了一些下面不起作用的代码,但我希望它可以说明我的观点......

public static void main(String[] args) {
  searchforName();
}

   private static void searchForName() throws FileNotFoundException {
    File file = new File("leaders.txt");
    Scanner kb = new Scanner(System.in);
    Scanner input = new Scanner(file);

    System.out.println("Please enter the name you would like to search for: ");
    String name = kb.nextLine();


    while(input.hasNextLine()) {
        System.out.println(input.next(name));
    }
}
Run Code Online (Sandbox Code Playgroud)

"leaders.txt"文件包含名称列表.

java io java.util.scanner

7
推荐指数
1
解决办法
7万
查看次数

用Java读取CSV文件的最快方法

我注意到java.util.Scanner在读取大文件时使用非常慢(在我的例子中是CSV文件).

我想改变我目前正在阅读文件的方式,以提高性能.以下是我目前的情况.请注意,我正在为Android开发:

InputStreamReader inputStreamReader;
    try {
        inputStreamReader = new InputStreamReader(context.getAssets().open("MyFile.csv"));
        Scanner inputStream = new Scanner(inputStreamReader);
        inputStream.nextLine(); // Ignores the first line
        while (inputStream.hasNext()) {
            String data = inputStream.nextLine(); // Gets a whole line
            String[] line = data.split(","); // Splits the line up into a string array

            if (line.length > 1) {
                // Do stuff, e.g:
                String value = line[1];
            }
        }
        inputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
Run Code Online (Sandbox Code Playgroud)

使用Traceview,我设法发现主要的性能问题,特别是:java.util.Scanner.nextLine()java.util.Scanner.hasNext().

我已经看过其他问题了(比如这个 …

java csv performance android

5
推荐指数
2
解决办法
8424
查看次数

为什么java.util.Scanner存在?

可能重复:
扫描程序与BufferedReader

是否有任何情况下使用java.util.Scanner来读取某种类型的输入?在我的小测试中,我发现它比java.util.Bufferedreader或从java.util.InputStreamReader实现自己的阅读器要慢得多.

那么为什么我想要使用扫描仪有什么理由呢?

java java.util.scanner

4
推荐指数
1
解决办法
3985
查看次数

BufferedReader如何在java中工作

我想知道BufferedReader的工作原理吗?为什么使用InputStreamReader?它与Scanner类有何不同,后者也用于输入用户?哪两个更好?

java bufferedreader inputstreamreader

0
推荐指数
1
解决办法
1769
查看次数