Showing posts with label EOF. Show all posts
Showing posts with label EOF. Show all posts

Tuesday, December 21, 2021

JAVA End Of File

 The challenge here is to read  lines of input until you reach EOF, then number and print all  lines of content.

Hint: Java's Scanner.hasNext() method is helpful for this problem.

Input Format

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

Output Format

For each line, print the line number, followed by a single space, and then the line content received as input.

Sample Input

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

Sample Output

1 Hello world
2 I am a file
3 Read me until end-of-file.
Answer:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        int count =0;
        Scanner sc = new Scanner(System.in);
        //String s = sc.readLine();
        
        while(sc.hasNext())
        {
            count ++;
            System.out.println(count+" "+sc.nextLine());
        }
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
    }
}

Java Regex

You are updating the username policy on your company's internal networking platform. According to the policy, a username is considered v...