Created
July 28, 2018 23:19
-
-
Save AbdullahMagat/0179e84a540279f48a85ad75cbc307c9 to your computer and use it in GitHub Desktop.
Hackerrank Java End-of-file Solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.*; | |
public class Solution { | |
public static void main(String[] args) { | |
Scanner scan = new Scanner(System.in); | |
int i=0; | |
while(scan.hasNextLine()){ | |
String str=scan.nextLine(); | |
if (str.contains("end-of-file")){ | |
i++; | |
System.out.println(i+" "+str); | |
break; | |
}else{ | |
i++; | |
System.out.println(i+" " +str); | |
} | |
} | |
// I don't know why hackerrank didn't accept my below solution. when I run it in eclipse it worked as well as it is required. anyway it is the 2. solution. | |
// Scanner scan = new Scanner(System.in); | |
// int i = 0; | |
// while(true){ | |
// String str=scan.nextLine(); | |
// if (str.contains("end-of-file")){ | |
// i++; | |
// System.out.println(i+" "+str); | |
// break; | |
// }else{ | |
// i++; | |
// System.out.println(i+" " +str); | |
// } | |
// } | |
} | |
} |
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 scan = new Scanner(System.in);
int i=1;
while(scan.hasNextLine()){
String s=scan.nextLine();
System.out.println(i+" " +s);
i++;
}
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
import java.io.;
import java.util.;
import java.text.;
import java.math.;
import java.util.regex.*;
public class Solution {
}
Here is the solution same as you. I guess the str variable should introduct after main not when you get it from user