fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone {
  9. public static void main (String[] args) {
  10. System.out.println("--- First block ---");
  11. try {
  12. doTest("name1:surname1\nname2:surname2"); // <--- no line break, fine
  13. } catch (Exception ex) {
  14. System.out.println("First block failed: "+ex);
  15. }
  16. System.out.println("--- Second block ---");
  17. try {
  18. doTest("name3:surname3\nname4:surname4\n"); // <--- a single line break, fine
  19. } catch (Exception ex) {
  20. System.out.println("Second block failed: "+ex);
  21. }
  22. System.out.println("--- Third block ---");
  23. try {
  24. doTest("name5:surname5\nname6:surname6\n "); // <--- an actual line of a space
  25. } catch (Exception ex) {
  26. System.out.println("Third block failed: "+ex);
  27. }
  28. System.out.println("--- Fourth block ---");
  29. try {
  30. doTest("name7:surname7\nname8:surname8\n\n"); // <--- an actual empty line
  31. } catch (Exception ex) {
  32. System.out.println("Fourth block failed: "+ex);
  33. }
  34. }
  35.  
  36. public static void doTest(String string) throws Exception {
  37. Scanner scan = new Scanner(new StringReader(string));
  38. while(scan.hasNextLine()) {
  39. String descritpion = scan.nextLine();
  40. System.out.println("line" +descritpion);
  41. String []temp = descritpion.split(":");
  42. String name = temp[0];
  43. String surname = temp[1];
  44. System.out.println("name : "+ name);
  45. System.out.println("surname : "+ surname);
  46. }
  47. }
  48. }
Success #stdin #stdout 0.14s 37644KB
stdin
Standard input is empty
stdout
--- First block ---
linename1:surname1
name : name1
surname : surname1
linename2:surname2
name : name2
surname : surname2
--- Second block ---
linename3:surname3
name : name3
surname : surname3
linename4:surname4
name : name4
surname : surname4
--- Third block ---
linename5:surname5
name : name5
surname : surname5
linename6:surname6
name : name6
surname : surname6
line 
Third block failed: java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
--- Fourth block ---
linename7:surname7
name : name7
surname : surname7
linename8:surname8
name : name8
surname : surname8
line
Fourth block failed: java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1