How to use hasNext () from the Scanner class?

Input format

Read some unknown n input lines from stdin(System.in)until you reach EOF; Each input line contains a non-empty line.

Output format

For each line, type the line number followed by one place, and then the contents of the line received as input:

Output result

Hello world
I am a file
Read me until end-of-file.  

Here is my solution. The problem is that I cannot act before the EOF. But the output is simple:

Hello world

Here is my code:

public class Solution {

    public static void main(String[] args) {
        check(1);  // call check method
    }

    static void check(int count) {          
        Scanner s = new Scanner(System.in);
        if(s.hasNext() == true) {
            String ns = s.nextLine();
            System.out.println(count + " " + ns);
            count++;
            check(count);
        }
    } 
}
+5
source share
10 answers

, Scanner . , .

public class Solution {

    public static void main(String[] args) {

        Scanner s = new Scanner(System.in);
        int count = 1;

        while(s.hasNext()) {
            String ns = s.nextLine();
            System.out.println(count + " " + ns);
            count++;
        }
    }
}

public class Solution {

    private Scanner s;

    public static void main(String[] args) {

        s = new Scanner(System.in); // initialize only once
        check(1);
    }

    public static void check(int count) {
        if(s.hasNext()) {
            String ns = s.nextLine();
            System.out.println(count + " " + ns);
            check(count + 1);
        }
    }   
}
+6

if (s.hasNext() == true) {
    String ns = s.nextLine();
    System.out.println(count + " " + ns);
    count++;
    System.out.print(count);
    check(count);
}

while (s.hasNext()) {
    String ns = s.nextLine();
    System.out.println(count + " " + ns);
    count++;
    System.out.print(count);
    check(count);
}

while , , if .

+2

, :

static void check(int count) {          
    Scanner s = new Scanner(System.in);
    check(count, s);
} 

static void check(int count, Scanner scanner) {
    if(!scanner.hasNext()) {
        return;
    }
    String ns = scanner.nextLine();
    System.out.println(count + " " + ns);
    check(++count, scanner);
}

, new Scanner(System.in) .

+1

Scanner BufferedReader ( - . , . Scanner ). , , read() System.in Scanner.

public static void main(String[] args) {          
    Scanner s1 = new Scanner(System.in);
    s1.hasNext();
    Scanner s2 = new Scanner(System.in);
    while(true){
        System.out.println("Read line:: " + s2.nextLine());
    }
}

Scanner:

1
2
3
4

:

:: e 1
:: 2
:: 3
:: 4

, . Scanner s1. 2 Scanner Stream.

, .

private static Scanner s;

public static void main(String[] args) {
    s = new Scanner(System.in);
    check(1);  // call check method
}

static void check(int count) {
    if (s.hasNextLine()) {
        String ns = s.nextLine();
        System.out.println(count + " " + ns);
        count++;
        check(count);
    }
}

s.hasNextLine() s.hasNext(), .

s.hasNextLine()==true, true , s.hasNextLine() true.

EOF , Ctrl+Z Windows Ctrl+D Unix. , EOF, NetBeans.

+1
public static void main(String[] args) {

    List<String> eol = new ArrayList<String>();
    Scanner in=new Scanner(System.in);
    int t=1;
    while(in.hasNext()){

        eol.add(in.nextLine());
    }

    for (String i:eol){

        System.out.println(t+" "+i);
        t++;
    }

}
0
public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int count = 1;
    while(scan.hasNext()){
        System.out.println(count++ + " " + scan.nextLine());            
    }
}
0

.

import java.util.Scanner;

public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int count = 1;
        while(scan.hasNext()) {
            String s = scan.nextLine();
            System.out.println(count + " " + s);
            count++;
        }
    }
}
0
Scanner scanner = new Scanner(System.in);
int i = 1;
while(scanner.hasNext()) {
  System.out.println(i + " " + scanner.nextLine());
  i++;
}
0

while if, . :

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner sc=new Scanner(System.in);
        {
            int i=0;

          while(sc.hasNext()==true)
            {
                i=i+1;
                 String s=sc.nextLine();
                System.out.println(i+" "+s);

            };
        }
    }
}
0

java.util.Scanner.hasNext() . true, . false. ,

 while(sc.hasNext()) {
      System.out.println(i + " " + sc.nextLine());
      i++;
 }

,

https://github.com/hetalrachh/HackerRank/blob/master/Practice/Java/Introduction/EndOfFile.java

0
source

Source: https://habr.com/ru/post/1620733/


All Articles