Java 使用 Scanner 从用户输入中计算单词和行数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28712565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 14:03:04  来源:igfitidea点击:

Java use Scanner to count words and lines from user input

javawhile-loopcountjava.util.scanner

提问by yhsdygdyusgdysgdsudsd

I'm trying to use the Scanner class to calculate the number of words and lines from a user input, and this is my attempt:

我正在尝试使用 Scanner 类来计算来自用户输入的单词和行数,这是我的尝试:

import java.util.Scanner;

public class newCounter{
  public static void main(String [ ] args){

    Scanner input = new Scanner(System.in);

    printWords(input);
    printLines(input);

  }

  public static void printWords(Scanner x){
    int words = 0;
    while(x.hasNext()){
      words++;
      x.next();
    }
    System.out.println("words: " + words);
  }



  public static void printLines(Scanner x){
    int lines = 0;
    while(x.hasNextLine()){
      lines++;
      x.nextLine();
    }
    System.out.println("lines: " + lines);

  }
}

I've found that both methods work 100% fine individually, but only the first one called works when together (The printWords method in this situation). Is there any way of combining these methods so that it might work as one loop?

我发现这两种方法单独工作 100%,但只有第一个调用的方法一起工作(在这种情况下是 printWords 方法)。有没有什么方法可以组合这些方法,使其可以作为一个循环工作?

采纳答案by Balayesu Chilakalapudi

you can calculate the words,lines with the following code

您可以使用以下代码计算单词,行

 public static void printWordsOfLines(Scanner x){
 Scanner line=x;
 Scanner word=x;
int lines = 0;
System.out.println("Enter q to quite input");
int words=0;

while(line.hasNextLine()){
   String xx=line.next();

   String w[]=xx.split("  ");
   String l[]=xx.split("\n");

   words+=w.length;
   lines+=l.length;

   if(xx.equals("q"))
       break;
  else   
      x.nextLine();
    }
  System.out.println("lines: " + lines);
  System.out.println("words: " + words);
}

回答by Jens

Every method will read a line from you console. So you have to change to read the line once in a string and then work with it.

每个方法都会从您的控制台读取一行。因此,您必须更改为在字符串中读取该行一次,然后再使用它。

  public static void main(String [ ] args){

    Scanner input = new Scanner(System.in);

    while(input.hasNext()){
        String line =input.next();

        printWords(new Scanner(line));
        printLines(new Scanner(line));
    }
  }

  public static void printWords(Scanner x){
    int words = 0;
    while(x.hasNext()){
      words++;
      x.next();
    }
    System.out.println("words: " + words);
  }



  public static void printLines(Scanner x){
    int lines = 0;
    while(x.hasNextLine()){
      lines++;
      x.nextLine();
    }
    System.out.println("lines: " + lines);

  }

回答by Gowtham Murugesan

Hope this helps you in finding the word count, line count & word count excluding white spaces.

希望这可以帮助您找到不包括空格的字数、行数和字数。

String i=null;
Scanner input = new Scanner(System.in);
int line=0;
i= input.nextLine();
if(i!=null)
{
    line++;
}
StringBuffer sb = new StringBuffer();
sb.append(i);   

int wordcount=sb.length();

System.out.println("Content"+i);
System.out.println("wordsCount:"+wordcount);
System.out.println("LineCount"+line);
System.out.println("Excluding white spaces"+i.replaceAll(" ", "").length());

回答by Yaniv Ben David

You got good answers here. Just wanted to sharpen things a little. In your example, by completing the words counting, the scanner pointer was located at the end of the last word entered - so when you initiated the lines counting, you had nothing to count.

你在这里得到了很好的答案。只是想锐化一点。在您的示例中,通过完成单词计数,扫描仪指针位于输入的最后一个单词的末尾 - 因此当您启动行计数时,您没有什么可计数的。

By grasping it, now you better count each lines first. Then count the words via the split method as @Balayesu Chilakalapudi suggested or creating another scanner, running on each lines - as @Jens advised.

通过掌握它,现在您最好先计算每一行。然后按照@Balayesu Chilakalapudi 建议的拆分方法计算单词,或者创建另一个扫描仪,在每一行上运行 - 正如@Jens 建议的那样。