简体   繁体   English

Java使用Scanner来计数用户输入中的单词和行数

[英]Java use Scanner to count words and lines from user input

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). 我发现这两种方法分别可以100%正常工作,但只有第一种方法一起使用时才可以工作(在这种情况下,printWords方法)。 Is there any way of combining these methods so that it might work as one loop? 有什么方法可以组合这些方法,使其成为一个循环?

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);

  }

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);
}

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());

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的建议通过split方法对单词进行计数,或者按照@Jens的建议在每行上运行另一个扫描仪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM